A new approach to interfaces in Python
Project description
Python Strict Interfaces
Installation
pip install strict-interfaces
Design Goals
- Be as strict as possible
- Fail on import time
- Do not mess with
object
and/ortype
inheritance - Possibility to integrate in CPython Core
- Ability to use "out of the box" regardless support in an interpreter
Features
- Special keyword
implements
on the class definition - Multiple interface implementation
- Implicit interface implementation
- Interface inheritance with overloading being restricted
- Special
isimplementation
function similar toissubclass
- Partial
issubclass
support (see above) - It's restricted to create an interface instance
- It's restricted to inherit from
object
andinterface
at the same time
Usage
Explicit implemetation
class TestInterface(interfaces.interface):
def method(self, arg: typeT1) -> typeT2:
pass
class TestClass(interfaces.object, implements=[TestInterface]):
def method(self, arg: typeT1) -> typeT2:
pass
Raises when is not implemented
class TestInterface(interfaces.interface):
def method(self, arg):
pass
class TestClass(interfaces.object, implements=[TestInterface]):
pass
Implicit implementation and run-time check
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 implemented
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
# NOTE: In this case `isimplementation` behaves different than `issubclass`
assert not interfaces.isimplementation(TestClass, (TestInterfaceA, TestInterfaceB))
assert issubclass(TestClass, (TestInterfaceA, TestInterfaceB))
Contributing
Pull requests, feature requests, and bug reports are always welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for strict-interfaces-0.1.0.dev1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f5211c35716181adff66e4b4edc45820cba104df5bd15c37b530417b8847fba |
|
MD5 | b23f9bacd0d438dac813fe1c334f92f0 |
|
BLAKE2b-256 | 061978aa76f2358cfd206437c92b75f602e1160140d12487402f2c6c9b59e79c |
Close
Hashes for strict_interfaces-0.1.0.dev1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de82d20c9afb6aa0c9861389c287c54d529be9bd37516b48705b76bea279d9f3 |
|
MD5 | 58fd558aa54d318c4fd007556e9937ad |
|
BLAKE2b-256 | 7e3c36043d9674ef834f1fb4af0b8441b90ca25c33f5e06f89ef251cb522dd1b |