python / python/cpython

Metaclasses with type annotated attributes do not add `__annotations__ = {}` to derived classes.

未關閉
#149,208 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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

描述

Bug report

Bug description:

This can cause odd behaviour for subclasses which then propagate their parent's annotations rather than their own.

class MyType(type):
    METACLASS_VAR: int = 1

class A(metaclass=MyType):
    value: str = "HI"

class B(A):
    pass

B.__annotations__  # {"value": str} instead of {}; but if you remove the type annotation in `MyType`, you get {}

This issue only seems to affect versions prior to 3.14

CPython versions tested on:

3.10, 3.13, 3.14

Operating systems tested on:

Linux

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

先在 CPython 3.10 和 3.13 上运行已回报的 MyType、A 和 B 重现案例,并与 3.14 进行比较。追踪元类上的类型标注属性如何影响衍生类的 annotations。完成标准是:B 这样的子类取得自己的空标注字典,而不是继承 A 的标注,并且已在 Linux 上验证其行为。

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

評估

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

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

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