{"id":6207,"library":"python-interface","title":"Pythonic Interface Definitions","description":"The `python-interface` library provides a Pythonic way to define and enforce interfaces. It helps in creating clear contracts for classes, ensuring that implementations adhere to a specified structure. Currently at version 1.6.1, the library has a moderate release cadence, with updates addressing bugs, adding minor features like subinterfaces, and improving internal tooling.","status":"active","version":"1.6.1","language":"en","source_language":"en","source_url":"https://github.com/ssanderson/interface","tags":["interfaces","design patterns","type checking","abstract base classes"],"install":[{"cmd":"pip install python-interface","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Interface","correct":"from interface import Interface"},{"symbol":"abstractmethod","correct":"from interface import abstractmethod"},{"symbol":"implements","correct":"from interface import implements"}],"quickstart":{"code":"from interface import Interface, abstractmethod, implements\n\nclass MyInterface(Interface):\n    \"\"\"An example interface.\n\n    Requires a 'required_method' with specific arguments.\n    \"\"\"\n\n    @abstractmethod\n    def required_method(self, required_argument: str) -> bool:\n        \"\"\"This method must be implemented by any class implementing MyInterface.\"\"\"\n        pass\n\nclass MyImplementation(implements(MyInterface)):\n    \"\"\"A class correctly implementing MyInterface.\"\"\"\n    def required_method(self, required_argument: str) -> bool:\n        if isinstance(required_argument, str) and len(required_argument) > 0:\n            return True\n        return False\n\n# Attempting to instantiate a class that doesn't implement all methods will raise an error\nclass IncompleteImplementation(implements(MyInterface)):\n    pass\n\ntry:\n    print(\"Attempting to instantiate IncompleteImplementation...\")\n    IncompleteImplementation()\nexcept TypeError as e:\n    print(f\"Error as expected for incomplete implementation: {e}\")\n\n# Demonstrate successful implementation\nimpl = MyImplementation()\nprint(f\"MyImplementation.required_method('test'): {impl.required_method('test')}\")\nprint(f\"MyImplementation.required_method(''): {impl.required_method('')}\")\n\n# New in 1.6.0: Interface Subclassing\nclass ExtendedInterface(MyInterface):\n    \"\"\"An interface extending MyInterface with an additional method.\"\"\"\n    @abstractmethod\n    def another_required_method(self) -> int:\n        pass\n\nclass ExtendedImplementation(implements(ExtendedInterface)):\n    \"\"\"A class correctly implementing ExtendedInterface.\"\"\"\n    def required_method(self, required_argument: str) -> bool:\n        return True\n\n    def another_required_method(self) -> int:\n        return 42\n\nextended_impl = ExtendedImplementation()\nprint(f\"ExtendedImplementation.another_required_method(): {extended_impl.another_required_method()}\")\n","lang":"python","description":"This quickstart demonstrates how to define an interface using `Interface` and `abstractmethod`, and how to implement it using `implements`. It also shows the error raised for incomplete implementations and highlights the subinterface feature introduced in version 1.6.0."},"warnings":[{"fix":"Upgrade to `python-interface` version 1.4.0 or higher.","message":"Version 1.3.0 contained a bug in the implementation of the `@default` decorator, leading to incorrect behavior. Users should upgrade to 1.4.0 or later to avoid this issue.","severity":"breaking","affected_versions":"<1.4.0"},{"fix":"Upgrade to `python-interface` version 1.5.2 or higher.","message":"When combining `@default` and `@property` decorators, versions prior to 1.5.2 had a bug that could cause unexpected behavior. This was fixed in 1.5.2.","severity":"gotcha","affected_versions":"<1.5.2"},{"fix":"Always ensure `super().__new__(cls, ...)` is called within any custom `__new__` method in a class implementing an interface.","message":"Overriding a class's `__new__` method in an implementation class without explicitly calling the parent/interface's `__new__` method will bypass the interface enforcement checks.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that methods implementing an interface exactly match the method name and argument names defined in the interface, including positional and keyword arguments.","message":"The library enforces strict method signature matching (name and argument names) for interface methods. While keyword argument *values* can differ, argument *names* must be identical to the interface definition.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}