protocol_implements_decorator
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
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
@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
@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
@implements(Printable, Otherable)
class Example2:
"""Test class that uses multiple protocols."""
def other(self) -> str:
return str(self)
NotImplementedError: test.<locals>.Printable requires implentation of ['to_string']
Metadata
Release files for Protocol-Implements-Decorator 1.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| protocol_implements_decorator-1.1.4.tar.gz | 6.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Protocol_Implements_Decorator-1.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.7 kB
Release files / protocol_implements_decorator-1.1.4.tar.gz
| Download URL | protocol_implements_decorator-1.1.4.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
98bd789915205199c67e44f6f7170f09206225c186ae7e0b3b293f222c90e48a
|
|
BLAKE2b-256 checksum How to use checksums |
c20bdbc004a3633ca28c6c9f6c256b30ae4c71736b47f1adb776c20872031bb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / Protocol_Implements_Decorator-1.1.4-py3-none-any.whl
| Download URL | Protocol_Implements_Decorator-1.1.4-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b8282c0e1a4ee26fad442fe4da23d8ccea53880a23d72808f16b696d1b1e3e8
|
|
BLAKE2b-256 checksum How to use checksums |
3792e45a575f52b0ab4bee17508765a56613cac1c0a3e7c78eb7e8c0ebcc3359
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|