用于特定类型注解的类参数的 `singledispatch()`。
项目描述
用于特定类型注解的类参数的 `singledispatch()`。
灵感来源于https://github.com/python/cpython/issues/100623。
简单示例
虽然 functools.singledispatch
会检查第一个用户参数的类,但 class_singledispatch
使用第一个参数作为类本身,并使用它执行与 functools.singledispatch
相同的任务。
from class_singledispatch import class_singledispatch
class T:
pass
class OtherT:
pass
@class_singledispatch
def on_class(cls: type[T], /) -> None:
print("T!")
@on_class.register
def on_other_class(cls: type[OtherT], /) -> None:
print("OtherT!")
# Useful for <3.10 without eval_type_backport -- pass the class to the decorator
# not to use the annotation from the function for the targeted class resolution
@on_class.register(SomeOtherT)
def on_some_other_class(cls: type[SomeOtherT], /) -> None:
print("SomeOtherT!")
on_class(T) # T!
on_class(OtherT) # OtherT!
on_class(SomeOtherT) # SomeOtherT!
安装
您可能只需使用pip进行安装
pip install class-singledispatch
[!注意]
class-singledispatch
支持现代类型提示
。
要使用 PEP 585 或 PEP 604 注释在Python <3.10中,请安装class-singledispatch[modern-type-hints]
。
如果您使用 Poetry,则可能需要运行
poetry add class-singledispatch
对于贡献者
[!注意] 如果您使用Windows,强烈建议您通过WSL2完成以下安装方式。
-
Forkclass_singledispatch仓库在GitHub上。
-
安装Poetry.
诗是管理依赖项和虚拟环境、构建包以及发布它们的出色工具。您可能希望使用pipx将其全局安装(推荐)pipx install poetry
如果您遇到任何问题,请参考官方文档以获取最新的安装说明。
请确保已安装Python 3.8——如果您使用pyenv,只需运行
pyenv install 3.8
-
在本地克隆您的分支并安装依赖项。
git clone https://github.com/your-username/class_singledispatch path/to/class_singledispatch cd path/to/class_singledispatch poetry env use $(cat .python-version) poetry install
接下来,只需激活虚拟环境并安装pre-commit钩子
poetry shell pre-commit install
有关如何贡献的更多信息,请参阅CONTRIBUTING.md。
始终乐于接受贡献!❤️
法律信息
项目详情
关闭
class_singledispatch-1.2.3.post2.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 19a80f7c2a530e8f1909172a7b8544c552332f0194136e4bebdd725ffa104dc5 |
|
MD5 | 2bf71f5e7f1934eba368c0269a2cac5a |
|
BLAKE2b-256 | 585653eb6c5d15aab38e63da18ad55c3107779d1bcbef311e0e537c78f1e0010 |
关闭
class_singledispatch-1.2.3.post2-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a806473af00ad02cb34120b953074f3390e3a119dee872e1c251ed271c2b28ca |
|
MD5 | d915916bb9268696e4d286b1243e483e |
|
BLAKE2b-256 | dce9e59d3ec711acd41ab79691dd70453ede21dc29cf4c02d3bc50eafe2accd3 |