Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lig/python-interfaces
A new approach to interfaces in Python
https://github.com/lig/python-interfaces
interface interfaces python python3 python37 python38
Last synced: about 2 months ago
JSON representation
A new approach to interfaces in Python
- Host: GitHub
- URL: https://github.com/lig/python-interfaces
- Owner: lig
- License: mit
- Created: 2019-04-29T01:57:06.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T05:03:40.000Z (about 2 years ago)
- Last Synced: 2024-11-01T15:51:49.486Z (about 2 months ago)
- Topics: interface, interfaces, python, python3, python37, python38
- Language: Python
- Size: 45.9 KB
- Stars: 14
- Watchers: 3
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Python Strict Interfaces
## Installation
```shell
pip install strict-interfaces
```## Design Goals
* Be as strict as possible
* Fail on import time
* Do not mess with `object` and/or `type` 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 to `issubclass`
* Partial `issubclass` support (see below)
* It's restricted to create an interface instance
* It's restricted to inherit from `object` and `interface` at the same time## Usage
### Explicit implementation
```python
class TestInterface(interfaces.interface):
def method(self, arg: typeT1) -> typeT2:
passclass TestClass(interfaces.object, implements=[TestInterface]):
def method(self, arg: typeT1) -> typeT2:
pass
```### Raises when is not implemented
```python
class TestInterface(interfaces.interface):
def method(self, arg):
passclass TestClass(interfaces.object, implements=[TestInterface]):
pass
```### Implicit implementation and run-time check
```python
class TestInterfaceA(interfaces.interface):
def method_a(arg: typeT1) -> typeT1:
passclass TestInterfaceB(interfaces.interface):
def method_b(arg: typeT2) -> typeT2:
passclass TestClass:
def method_a(arg: typeT1) -> typeT1:
passdef method_b(arg: typeT2) -> typeT2:
passassert interfaces.isimplementation(TestClass, (TestInterfaceA, TestInterfaceB))
```### `isimplementation` checks whether all interfaces are implemented
```python
class TestInterfaceA(interfaces.interface):
def method_a(arg: typeT1) -> typeT1:
passclass TestInterfaceB(interfaces.interface):
def method_b(arg: typeT2) -> typeT2:
passclass 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!
[github.com/lig/python-interfaces](https://github.com/lig/python-interfaces)
## Discussions
There are threads [on Python Ideas mailing list](https://groups.google.com/forum/#!topic/python-ideas/uZqje5A1Mcs) and [on Reddit](https://www.reddit.com/r/Python/comments/bkwtnp/a_new_approach_to_interfaces_in_python/).