https://github.com/rbroderi/protocol_implements_decorator
https://github.com/rbroderi/protocol_implements_decorator
decorators-python implement protocol
Last synced: 16 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/rbroderi/protocol_implements_decorator
- Owner: rbroderi
- License: bsd-3-clause
- Created: 2021-10-30T02:41:54.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-12T17:47:22.000Z (over 1 year ago)
- Last Synced: 2025-09-24T22:09:52.632Z (4 months ago)
- Topics: decorators-python, implement, protocol
- Language: Python
- Homepage: https://rbroderi.github.io/protocol_implements_decorator/
- Size: 453 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Protocol Implements Decorator
================================================
[](https://github.com/rbroderi/protocol_implements_decorator/blob/master/LICENSE)
[](https://github.com/astral-sh/ruff)
[](https://pypi.python.org/pypi/protocol_implements_decorator/)
[](http://mypy-lang.org/)
[](https://github.com/beartype/beartype)
[](https://bandit.readthedocs.io/en/latest/)
[](https://github.com/astral-sh/uv)
[](https://github.com/rbroderi/protocol_implements_decorator/releases)
Adds the "implements" decorator to make using protocols easier and more explicit
## Description
Adds the @implements decorator.
This will cause a runtime NotImplementedError if the class does not implement all parts of the protocol.
Also adds the get_protocols_implemented method to the class providing a list of all protocols the decorated class adhears to.
Usage:
---
Two example protocols
```python
class Printable(Protocol):
"""A test protocol that requires a to_string method."""
def to_string(self) -> str:
return ""
class Otherable(Protocol):
"""Another example."""
def other(self) -> str:
return "
```
---
Example of one protocol
```python
@implements(Printable)
class Example2:
def to_string(self) -> str:
return str(self)
```
For multiple protocols you can chain dectorator or include in a list in one dectorator
```python
@implements(Printable)
@implements(Otherable)
class Example1:
"""Test class that uses multiple protocols."""
def to_string(self) -> str:
return str(self)
def other(self) -> str:
return str(self)
@implements(Printable, Otherable)
class Example2:
"""Test class that uses multiple protocols."""
def to_string(self) -> str:
return str(self)
def other(self) -> str:
return str(self)
```
Errors
---
This will cause a runtime error as it doesn't implement the Printable protocol
```python
@implements(Printable, Otherable)
class Example2:
"""Test class that uses multiple protocols."""
def other(self) -> str:
return str(self)
```
```text
NotImplementedError: test..Printable requires implentation of ['to_string']
```