python / python/cpython

Can't assign entry to `__annotations__` unless a class has existing annotation

未關閉
#95,532 21 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

topic-typing type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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?

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先,在受影響的 Python 版本上重現類別主體範例,並閱讀相關的 issue 88067,以了解註解的相關行為。追蹤類別層級的 annotations 如何初始化,並驗證在沒有預先註解的類別中指派一個項目時,該項目會被保留,同時不會改變現有的已註解類別行為。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
compilers
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。