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 below) - It's restricted to create an interface instance
- It's restricted to inherit from
object
andinterface
at the same time
Usage
Explicit implementation
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
strict-interfaces-0.1.1.tar.gz
(11.8 kB
view details)
Built Distribution
File details
Details for the file strict-interfaces-0.1.1.tar.gz
.
File metadata
- Download URL: strict-interfaces-0.1.1.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.20.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75dd960f6a6ec61f788587cb52e10b42edffe85da7d06823dd0e3631cf545f47 |
|
MD5 | ce02c5927f04d780b8f13d69565c0ba1 |
|
BLAKE2b-256 | 06a5fcc63e5cb1159d64abcd819669cb244bc885f3f0e9af037d9832756019ff |
File details
Details for the file strict_interfaces-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: strict_interfaces-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.20.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9032b619b952c9184a374c92aba40fa3ea80ad0a7757e530805297ab91bcf01 |
|
MD5 | f65a50a575f241089987d126e7e70da0 |
|
BLAKE2b-256 | 14efa54dc4923b7feacb432658e855b556044390151bc6e1f123b477735d419a |