# 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
```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.<locals>.Printable requires implentation of ['to_string']
```
<!-- pyscaffold-notes -->
## Note
This project has been set up using PyScaffold 4.1.1. For details and usage
information on PyScaffold see https://pyscaffold.org/.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
protocol_implements_decorator-0.3.1.tar.gz (66个子文件)
protocol_implements_decorator-0.3.1
PKG-INFO 2KB
pyproject.toml 411B
setup.py 3KB
README.md 2KB
protocol_implements_decorator
implements.py 3KB
__init__.py 22B
.mypy_cache
CACHEDIR.TAG 193B
3.10
enum.meta.json 1KB
_typeshed
__init__.meta.json 2KB
__init__.data.json 41KB
io.data.json 80KB
builtins.data.json 796KB
types.meta.json 2KB
@plugins_snapshot.json 2B
collections
__init__.meta.json 2KB
__init__.data.json 229KB
ast.data.json 105KB
sys.data.json 68KB
io.meta.json 2KB
genericpath.data.json 16KB
posixpath.data.json 66KB
mmap.meta.json 1KB
inspect.data.json 264KB
codecs.meta.json 2KB
genericpath.meta.json 2KB
_ast.meta.json 1KB
typing_extensions.meta.json 2KB
os
__init__.meta.json 2KB
path.meta.json 2KB
path.data.json 5KB
__init__.data.json 142KB
codecs.data.json 114KB
ntpath.meta.json 2KB
types.data.json 104KB
typing.data.json 405KB
posix.data.json 82KB
typing_extensions.data.json 26KB
array.data.json 50KB
mmap.data.json 24KB
subprocess.data.json 159KB
subprocess.meta.json 2KB
ntpath.data.json 12KB
inspect.meta.json 2KB
typing.meta.json 2KB
builtins.meta.json 2KB
posix.meta.json 1KB
array.meta.json 2KB
enum.data.json 47KB
sys.meta.json 2KB
abc.meta.json 1KB
abc.data.json 13KB
posixpath.meta.json 2KB
protocol_implements_decorator
implements.data.json 4KB
__init__.meta.json 1KB
__init__.data.json 1KB
implements.meta.json 2KB
_ast.data.json 109KB
ast.meta.json 2KB
importlib
machinery.data.json 56KB
__init__.meta.json 2KB
__init__.data.json 5KB
abc.meta.json 2KB
abc.data.json 52KB
machinery.meta.json 2KB
.gitignore 36B
implements.pyi 75B
共 66 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功