A new approach to interfaces in Python
MIT License
pip install strict-interfaces
object
and/or type
inheritanceimplements
on the class definitionisimplementation
function similar to issubclass
issubclass
support (see below)object
and interface
at the same timeclass TestInterface(interfaces.interface):
def method(self, arg: typeT1) -> typeT2:
pass
class TestClass(interfaces.object, implements=[TestInterface]):
def method(self, arg: typeT1) -> typeT2:
pass
class TestInterface(interfaces.interface):
def method(self, arg):
pass
class TestClass(interfaces.object, implements=[TestInterface]):
pass
class TestInterfaceA(interfaces.interface):
def method_a(arg: typeT1) -> typeT1:
pass
class TestInterfaceB(interfaces.interface):
def method_b(arg: typeT2) -> typeT2:
pass
class TestClass:
def method_a(arg: typeT1) -> typeT1:
pass
def method_b(arg: typeT2) -> typeT2:
pass
assert interfaces.isimplementation(TestClass, (TestInterfaceA, TestInterfaceB))
isimplementation
checks whether all interfaces are implementedclass TestInterfaceA(interfaces.interface):
def method_a(arg: typeT1) -> typeT1:
pass
class TestInterfaceB(interfaces.interface):
def method_b(arg: typeT2) -> typeT2:
pass
class TestClass:
def method_a(arg: typeT1) -> typeT1:
pass
# NOTE: In this case `isimplementation` behaves different than `issubclass`
assert not interfaces.isimplementation(TestClass, (TestInterfaceA, TestInterfaceB))
assert issubclass(TestClass, (TestInterfaceA, TestInterfaceB))
Pull requests, feature requests, and bug reports are always welcome!
github.com/lig/python-interfaces
There are threads on Python Ideas mailing list and on Reddit.