Can't assign entry to `__annotations__` unless a class has existing annotation
还没有人认领这个 Issue。
评估
调研方向
首先,在受影响的 Python 版本上重现类体示例,并阅读相关的 issue 88067,以了解注解的相关行为。跟踪类级别的 annotations 是如何初始化的,并验证在没有预先注解的类中为其分配一个条目时,该条目会被保留,同时不会改变现有的已注解类行为。
由索引模型根据 Issue 内容生成。
描述
Bug report
Can't assign entry to __annotations__ unless a class has existing annotation. This is related to https://github.com/python/cpython/issues/88067.
python 3.9:
Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 20:33:18)
[Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo:
... __annotations__["b"] = int
...
>>> Foo.__annotations__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'Foo' has no attribute '__annotations__'
>>> class Foo:
... a: int
... __annotations__["b"] = int
...
>>> Foo.__annotations__
{'a': <class 'int'>, 'b': <class 'int'>}
python 3.10/python 3.11-rc:
Python 3.10.5 (main, Jul 12 2022, 11:32:11) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo:
... __annotations__["a"] = int
...
>>> Foo.__annotations__
{}
>>> class Foo:
... a: float
... __annotations__["b"] = int
...
>>> Foo.__annotations__
{'a': <class 'float'>, 'b': <class 'int'>}
Your environment
- CPython versions tested on: 3.9, 3.10, 3.11-rc
- Operating system and architecture: Mac, Linux
I'm interested in submitting a PR for this, but first would like to double check that such PR/change would be acceptable?
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
docs pending
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 72/100
-
build type-bug
难度 2/5 1-3 小时 新手友好度 76/100
-
stdlib topic-email type-feature
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
fix: inaccuracy ⚠️
难度 2/5 1-3 小时 新手友好度 72/100
uabrc/uabrc.github.io#1255 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
ethereum-optimism/factory#64 ·
-
难度 2/5 1-3 小时 新手友好度 90/100
duckdb/duckdb-python#627 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
-
documentation
难度 1/5 1 小时以内 新手友好度 78/100
Qiskit/qiskit-addon-sqd#376 ·