General function to test the correctness of an abstract base class. - It should inherit from ABC. - It should define at least one method. - It should have at least one abstract method. - Abstract methods (those in __abstractmethods__) should be marked as @abstractmethod. - I
(cls: Any)
| 10 | |
| 11 | |
| 12 | def check_module_base_class(cls: Any) -> None: |
| 13 | """ |
| 14 | General function to test the correctness of an abstract base class. |
| 15 | - It should inherit from ABC. |
| 16 | - It should define at least one method. |
| 17 | - It should have at least one abstract method. |
| 18 | - Abstract methods (those in __abstractmethods__) should be marked as @abstractmethod. |
| 19 | - It should not be instantiable. |
| 20 | - All methods should have docstrings. |
| 21 | |
| 22 | Args: |
| 23 | cls: The abstract base class to test. |
| 24 | """ |
| 25 | # Check 1: Ensure this is an abstract base class |
| 26 | assert issubclass(cls, ABC), f"{cls.__name__} should inherit from ABC" |
| 27 | |
| 28 | # Get all non-excluded methods (excluding dunder methods, except for __init__) |
| 29 | all_class_methods = [name for name, _ in inspect.getmembers(cls, predicate=inspect.isfunction)] |
| 30 | |
| 31 | # Check 2: Ensure the class defines methods |
| 32 | assert all_class_methods, f"{cls.__name__} should define at least one method" |
| 33 | |
| 34 | # Check 3: Verify abstract methods |
| 35 | # Get the set of abstract methods from the class |
| 36 | abstract_methods = getattr(cls, "__abstractmethods__", set()) |
| 37 | |
| 38 | # Ensure there is at least one abstract method |
| 39 | assert len(abstract_methods) > 0, f"{cls.__name__} should have at least one abstract method" |
| 40 | |
| 41 | # Verify that all methods in __abstractmethods__ are actually marked as abstract |
| 42 | for method_name in all_class_methods: |
| 43 | method = getattr(cls, method_name) |
| 44 | # Skip private methods (starting with _) as they are typically helper methods |
| 45 | if method_name.startswith("_") and method_name != "__init__": |
| 46 | continue |
| 47 | |
| 48 | # If the method is in __abstractmethods__, it must be marked as abstract |
| 49 | if method_name in abstract_methods: |
| 50 | assert getattr(method, "__isabstractmethod__", False), ( |
| 51 | f"The method '{method_name}' in {cls.__name__} is in __abstractmethods__ " |
| 52 | f"but should be marked as @abstractmethod" |
| 53 | ) |
| 54 | |
| 55 | # Check 4: Test that the class cannot be instantiated directly |
| 56 | with pytest.raises(TypeError) as excinfo: |
| 57 | cls() |
| 58 | assert "abstract" in str(excinfo.value).lower(), ( |
| 59 | f"{cls.__name__} should not be instantiable as it's an abstract base class" |
| 60 | ) |
| 61 | |
| 62 | # Check 5: Ensure all methods have docstrings |
| 63 | for method_name in all_class_methods: |
| 64 | method = getattr(cls, method_name) |
| 65 | assert method.__doc__, f"Method '{method_name}' in {cls.__name__} should have a docstring" |
| 66 | |
| 67 | |
| 68 | def check_module_factory_class(cls: Any) -> None: |
no outgoing calls