{"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":"python","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,"search_vec":"'1.6.1':37 'abstract':63 'ad':49 'address':47 'adher':29 'base':64 'bug':48 'cadenc':44 'check':62 'class':25,65 'clear':22 'contract':23 'creat':21 'current':34 'defin':14 'definit':3 'design':59 'enforc':16 'ensur':26 'featur':51 'help':19 'implement':28 'improv':55 'interfac':2,7,17,58 'intern':56 'librari':8,39 'like':52 'minor':50 'moder':42 'pattern':60 'provid':9 'python':1,6,11 'python-interfac':5 'releas':43 'specifi':32 'structur':33 'subinterfac':53 'tool':57 'type':61 'updat':46 'version':36 'way':12","created_at":"2026-04-14T18:46:37.773118+00:00","updated_at":"2026-04-16T20:21:49.558789+00:00","problems":[{"fix":"class MyImplementation(MyInterface):\n    def method_name(self, *args, **kwargs):\n        # Your implementation logic here\n        pass","cause":"The class intended to implement the interface failed to provide an implementation for all methods marked with @abstractmethod in the interface.","error":"TypeError: Can't instantiate class 'MyImplementation' that does not implement abstract method 'method_name'"},{"fix":"pip install python-interface","cause":"The 'python-interface' library is not installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'interface'"},{"fix":"from interface import interface, abstractmethod","cause":"The library's import name is 'interface', not 'python_interface', despite its package name being 'python-interface'.","error":"ModuleNotFoundError: No module named 'python_interface'"},{"fix":"class MyConcreteClass(MyInterface):\n    def abstract_method(self):\n        # Implement abstract methods here\n        pass\n\nmy_instance = MyConcreteClass()","cause":"An interface specifies a contract for other classes to implement and cannot be instantiated directly itself.","error":"TypeError: Can't instantiate interface 'MyInterface' directly"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.6.1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/ssanderson/interface","docs":null,"changelog":null,"pypi":"https://pypi.org/project/python-interface/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}