`__future__` annotations breaks `TypedDict` `__required/optional_keys__`
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
描述
Bug report
from __future__ import annotations appears to break TypedDict required/optional keys ending up in __required_keys__ and __optional_keys__. mypy works as expected though.
Using the example from https://peps.python.org/pep-0655/#usage-in-python-3-11 as the base
$ cat t.py
from __future__ import annotations
from typing_extensions import NotRequired, TypedDict
class Dog(TypedDict):
name: str
owner: NotRequired[str]
print("required", Dog.__required_keys__)
print("optional", Dog.__optional_keys__)
$ python3 t.py
required frozenset({'name', 'owner'})
optional frozenset()
With the __future__ import removed, works as expected:
$ cat t.py
from typing_extensions import NotRequired, TypedDict
class Dog(TypedDict):
name: str
owner: NotRequired[str]
print("required", Dog.__required_keys__)
print("optional", Dog.__optional_keys__)
$ python3 t.py
required frozenset({'name'})
optional frozenset({'owner'})
Note: breaks across different variations of total and Required/NotRequired and typing_extensions vs typing imports, above is just one example.
https://peps.python.org/pep-0655/#how-to-teach-this contains an example with the __future__ annotations import in place with no mention that it would not cause __required_keys__ and __optional_keys__ becoming populated as expected, so I'm assuming this is a bug.
Your environment
-
CPython versions tested on:
- 3.9.7 +
NotRequiredandTypedDictimports fromtyping_extensions - 3.10.7 + above mentioned imports from
typing_extensions - current 3.11.0rc2+ + above mentioned imports from
typing_extensions - current 3.11.0rc2+ above mentioned imports from
typing
- 3.9.7 +
-
Operating system and architecture: Linux x86_64
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,在列出的 Python 版本中分别使用和不使用 from __future__ import annotations 运行 t.py 复现,并分别使用 typing 和 typing_extensions。跟踪 TypedDict、Required 和 NotRequired 的入口点,以确定延迟注解如何影响键的分类。对于报告的各个变体,当 __required_keys__ 和 __optional_keys__ 与声明的注解一致时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 42/100