PEP 585 + PEP 604 回退版本。
项目描述
modern_types 
__modern_types__
旨在为Python <=3.10的延迟类型评估提供PEP 585 + PEP 604向后兼容性。因此,目标Python版本是3.8和3.9。
它做什么?
从技术角度来看,__modern_types__
遍历类型提示表达式的AST,以转换传递给评估例程ForwardRef._evaluate
的命名空间副本。这种转换防止在Python 3.8和3.9中使用未来版本的PEP 585和PEP 604语法评估这些类型提示时出现类型错误。这对于在Python <3.10中以现代方式编写pydantic模型可能非常有用,而无需导入typing
。
结果,在Python 3.8和Python 3.9中,以下代码
from __future__ import annotations
import collections.abc
from collections import defaultdict
from pprint import pprint
from typing import get_type_hints
import __modern_types__ # without this line it won't work!
class Foo:
a: dict[str, int]
b: list[int]
c: set[int]
d: tuple[int, ...] | None
e: frozenset[int]
f: defaultdict[str, int]
g: str | None
h: str | int
i: str | int | None
j: str
k: collections.abc.Mapping[str, int]
l: collections.abc.Mapping[str, int] | None
m: collections.abc.Mapping[str, int | None] | float | None
pprint(get_type_hints(Foo, globals(), locals()))
给出
{'a': typing.Dict[str, int],
'b': typing.List[int],
'c': typing.Set[int],
'd': typing.Union[typing.Tuple[int, ...], NoneType],
'e': typing.FrozenSet[int],
'f': typing.DefaultDict[str, int],
'g': typing.Union[str, NoneType],
'h': typing.Union[str, int],
'i': typing.Union[str, int, NoneType],
'j': <class 'str'>,
'k': typing.Mapping[str, int],
'l': typing.Union[typing.Mapping[str, int], NoneType],
'm': typing.Union[typing.Mapping[str, typing.Union[int, NoneType]], float, NoneType]}
而不是引发错误,即type
对象不可索引(Python 3.8)或GenericAlias
不支持|
运算符(Python 3.9)。
用例
通过加快迁移到现代类型来加快您的代码库更新,即使您支持Python >=3.8的版本。
停止显式使用已弃用的typing.Dict
、typing.List
、typing.Set
、typing.Tuple
、typing.FrozenSet
、typing.DefaultDict
和其他typing
类型代理!
导入 __modern_types__
将使您应用程序中所有依赖 typing.ForwardRef
的部分,包括 pydantic 模型,与 PEP 585 和 PEP 604 无缝工作。
__modern_types__
在生产环境中安全使用吗?
是的。它不会破坏任何现有代码库。它只使用 AST 并覆盖 typing.ForwardRef._evaluate
。 __modern_types__
不与调用者的命名空间交互,不修改内置类,也不执行任何可能导致奇怪、意外的副作用的其他可疑操作。
如何使用?
[!警告] 请记住,该库不会在运行时更改内置作用域!
因此,
dict[str, int]
不会在运行时渲染,但typing.Dict[str, int]
会。
__modern_types__
使得只能通过typing.get_type_hints
函数评估dict[str, int]
。您应该在想要利用
__modern_types__
的所有模块顶部放置from __future__ import annotations
。
只需在您的代码中导入 __modern_types__
,它将使 typing.ForwardRef
实例通过类型提示表达式 AST 尝试调整传递的全局/局部命名空间副本,以使用支持运行时 []
和 |
运算符的 typing._GenericAlias
实例。
内置作用域中发生的示例替换
旧类型 | 新类型 | 没有 __modern_types__ ,在 Python 版本...上工作 |
有 __modern_types__ ,在 Python 版本...上工作 |
向后移植 PEP |
---|---|---|---|---|
dict[KT, VT] |
typing.Dict[KT, VT] |
>=3.9 | >=3.8 | PEP 585 |
list[T] |
typing.List[T] |
>=3.9 | >=3.8 | PEP 585 |
set[T] |
typing.Set[T] |
>=3.9 | >=3.8 | PEP 585 |
tuple[T, ...] |
typing.Tuple[T, ...] |
>=3.9 | >=3.8 | PEP 585 |
frozenset[T] |
typing.FrozenSet[T] |
>=3.9 | >=3.8 | PEP 585 |
X | Y |
typing.Union[X, Y] |
>=3.10 | >=3.8 | PEP 604 |
此外,__modern_types__
还允许您使用 collections.abc
和 contextlib
通用类。
[!注意] 根据在
__modern_types__._typeshed
源代码中列出的那些,如果可能,一些可选替换将自动注册。
安装
您可以使用 pip 安装它
pip install modern-types
如果您使用 Poetry,则运行
poetry add modern-types
对于贡献者
[!注意] 如果您使用 Windows,强烈建议您通过 WSL2 完成以下安装方式。
-
Fork GitHub 上的 modern_types 仓库。
-
安装 Poetry.
Poetry 是一个用于管理依赖项和虚拟环境、构建包并将其发布的神奇工具。您可以使用 pipx 在全局范围内安装它(推荐)pipx install poetry
如果您遇到任何问题,请参阅 官方文档 获取最新的安装说明。
请确保已安装 Python 3.8——如果您使用 pyenv,只需运行
pyenv install 3.8
-
在本地克隆您的分支并安装依赖项。
git clone https://github.com/your-username/modern_types path/to/modern_types cd path/to/modern_types poetry env use $(cat .python-version) poetry install
接下来,只需激活虚拟环境并安装 pre-commit 钩子
poetry shell pre-commit install --hook-type pre-commit --hook-type pre-push
有关如何贡献的更多信息,请参阅 CONTRIBUTING.md。
总是乐意接受贡献! ❤️
法律信息
项目详情
下载文件
下载您平台所需的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。