Registry / testing / python-interface

python-interface

JSON →
library1.6.1pypypi✓ verified 87d ago

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.

pip install python-interface
INSTALL
IMPORT
SIG · PYTHON-INTERFACE
P
python-interface
testingpythonv1.6.1
Install
2.5s avg
Import
28ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.6.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.025s · 19.5MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.5s · import 0.020s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Interface
from interface import Interface
abstractmethod
from interface import abstractmethod
implements
from interface import implements

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.

from interface import Interface, abstractmethod, implements class MyInterface(Interface): """An example interface. Requires a 'required_method' with specific arguments. """ @abstractmethod def required_method(self, required_argument: str) -> bool: """This method must be implemented by any class implementing MyInterface.""" pass class MyImplementation(implements(MyInterface)): """A class correctly implementing MyInterface.""" def required_method(self, required_argument: str) -> bool: if isinstance(required_argument, str) and len(required_argument) > 0: return True return False # Attempting to instantiate a class that doesn't implement all methods will raise an error class IncompleteImplementation(implements(MyInterface)): pass try: print("Attempting to instantiate IncompleteImplementation...") IncompleteImplementation() except TypeError as e: print(f"Error as expected for incomplete implementation: {e}") # Demonstrate successful implementation impl = MyImplementation() print(f"MyImplementation.required_method('test'): {impl.required_method('test')}") print(f"MyImplementation.required_method(''): {impl.required_method('')}") # New in 1.6.0: Interface Subclassing class ExtendedInterface(MyInterface): """An interface extending MyInterface with an additional method.""" @abstractmethod def another_required_method(self) -> int: pass class ExtendedImplementation(implements(ExtendedInterface)): """A class correctly implementing ExtendedInterface.""" def required_method(self, required_argument: str) -> bool: return True def another_required_method(self) -> int: return 42 extended_impl = ExtendedImplementation() print(f"ExtendedImplementation.another_required_method(): {extended_impl.another_required_method()}")
Debug
Known issues
breakingVersion 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.
fix
Upgrade to `python-interface` version 1.4.0 or higher.
affects: <1.4.0
gotchaWhen 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.
fix
Upgrade to `python-interface` version 1.5.2 or higher.
affects: <1.5.2
gotchaOverriding a class's `__new__` method in an implementation class without explicitly calling the parent/interface's `__new__` method will bypass the interface enforcement checks.
fix
Always ensure `super().__new__(cls, ...)` is called within any custom `__new__` method in a class implementing an interface.
affects: All versions
gotchaThe 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.
fix
Ensure that methods implementing an interface exactly match the method name and argument names defined in the interface, including positional and keyword arguments.
affects: All versions
Errors
Common errors & fixes
TypeError: Can't instantiate class 'MyImplementation' that does not implement abstract method 'method_name'
The class intended to implement the interface failed to provide an implementation for all methods marked with @abstractmethod in the interface.
fix
class MyImplementation(MyInterface):
    def method_name(self, *args, **kwargs):
        # Your implementation logic here
        pass
ModuleNotFoundError: No module named 'interface'
The 'python-interface' library is not installed in your current Python environment.
fix
pip install python-interface
ModuleNotFoundError: No module named 'python_interface'
The library's import name is 'interface', not 'python_interface', despite its package name being 'python-interface'.
fix
from interface import interface, abstractmethod
TypeError: Can't instantiate interface 'MyInterface' directly
An interface specifies a contract for other classes to implement and cannot be instantiated directly itself.
fix
class MyConcreteClass(MyInterface):
    def abstract_method(self):
        # Implement abstract methods here
        pass

my_instance = MyConcreteClass()
Upgrade
Version history
1.6.1latest on PyPI · released Apr 21, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Resources
python-interface — pip install python-interface · libregistry