def _check_defined_methods(cls, methods): instance_methods = [ method for method in methods if not isinstance(cls.__dict__[method], classmethod) ] if not instance_methods: raise GenericClassError("Define at least one instance method")
def _check_private_methods(methods): private_methods = [ method for method in methods if method.is_private_method() ] if private_methods: raise GenericClassError( "Do not use private methods (use composition instead)")
def _get_fields(entity): if dataclasses._is_dataclass(entity): fields = dataclasses._get_fields(entity) bases = dataclasses._get_bases() return fields, bases elif pydantic._is_pydantic(entity): fields = pydantic._get_fields(entity) bases = pydantic._get_bases() return fields, bases elif attrs._is_attrs(entity): fields = attrs._get_fields(entity) bases = attrs._get_bases() return fields, bases else: raise GenericClassError("Unknown type definition")
def _check_private_fields(fields): private_fields = [field for field in fields if field.startswith("_")] if private_fields: raise GenericClassError("Do not use private attributes")
def _check_defined_fields(fields): if not fields: raise GenericClassError("Define at least one encapsulated attribute")
def _check_defined_methods(methods): instance_methods = [method for method in methods if method.is_instance_method()] if not instance_methods: raise GenericClassError("Define at least one instance method")
def _check_bases(cls): if cls.__bases__ != (object,): raise GenericClassError("Do not use inheritance (use composition instead)")
def method(*args, **kwargs): message = "Instance methods can not be called on classes" raise GenericClassError(message)
def _deny_static_method(attribute): if isinstance(attribute, staticmethod): message = "Do not use static methods (use composition instead)" raise GenericClassError(message)
def _check_private_fields(fields): if any(filter(lambda field: field.startswith("_"), fields)): raise GenericClassError("Do not use private attributes")
def _check_private_methods(methods): if any(filter(lambda method: method.startswith("_"), methods)): raise GenericClassError( "Do not use private methods (use composition instead)")
def _check_static_methods(cls, methods): for method in methods: if isinstance(cls.__dict__[method], staticmethod): message = "Do not use static methods (use composition instead)" raise GenericClassError(message)
def _check_bases(cls, allowed_bases): if cls.__bases__ != allowed_bases: raise GenericClassError( "Do not use inheritance (use composition instead)")