`isinstance` removes `cls.__dict__["__annotations__"]` in a complex setup with Protocol and ABCMeta

未關閉
#141,681 7 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

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

研究方向

在 Python 3.12 和 3.13 上執行涉及 Module.init_subclass、CheckpointableIterator、Protocol、ABCMeta 和 isinstance 的重現程式,然後與 3.11 和 3.14 進行比較。追蹤 SequenceLayer1 和 SequenceLayer2 如何接收 annotations,並找出第二個類別在哪個位置遺失了它的項目。完成的標準是已解釋該行為,且有一個回歸測試涵蓋受影響的版本範圍。

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

描述

3.13 stdlib topic-typing type-bug

Bug report

Bug description:
import abc
from collections.abc import Iterator
from typing import Protocol

# ---- Expected behavior:
class Steppable(metaclass=abc.ABCMeta):
  pass

class Module:
  @classmethod
  def __init_subclass__(cls, **kwargs) -> None:
    super().__init_subclass__(**kwargs)
    name, annotation, default = "name", str | None, "abc"
    cls.__annotations__[name] = annotation
    setattr(cls, name, default)


class SequenceLayer1(Module, Steppable):
  pass


print(SequenceLayer1.__dict__.get("__annotations__"))
# {'name': str | None}
assert SequenceLayer1.__dict__.get("__annotations__") is not None


# ---- Unexpected behavior:
class CheckpointableIterator(Iterator, Protocol):
  pass

isinstance(CheckpointableIterator, Iterator)

class SequenceLayer2(Module, Steppable):
  pass

print(SequenceLayer2.__dict__.get("__annotations__"))
# None
assert SequenceLayer2.__dict__.get("__annotations__") is not None
  • Failing on 3.12 and 3.13
  • Passing on 3.11 and 3.14 (For 3.14 it is expected to have __dict__["__annotations__"] missing)
CPython versions tested on:

3.12

Operating systems tested on:

Linux

主要語言
Python
星號
77.2k
分支
36k
平均合併
1 天 9 小時
30 天內合併 PR
558

貢獻指南

開啟貢獻指南

從這裡開始

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

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

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

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