python / python/cpython

defining a `typing.Protocol` with `__annotate__` method raises `TypeError` since Python 3.14

未關閉
#146,643 15 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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

描述

Bug report

Bug description:
from annotationlib import Format
from collections.abc import Mapping
from typing import Any, Protocol


class CanAnnotate(Protocol):
    def __annotate__(self, format: Format, /) -> Mapping[str, Any]: ...

On Python 3.14.3 and 3.15.0a7 this results in a TypeError:

Traceback (most recent call last):
  File "/home/joren/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/typing.py", line 1853, in _get_protocol_attrs
    annotations = base.__annotations__
                  ^^^^^^^^^^^^^^^^^^^^
TypeError: CanAnnotate.__annotate__() missing 1 required positional argument: 'format'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/joren/Workspace/optype/repro.py", line 6, in <module>
    class CanAnnotate(Protocol):
        def __annotate__(self, format: Format, /) -> Mapping[str, Any]: ...
  File "/home/joren/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/typing.py", line 1999, in __init__
    cls.__protocol_attrs__ = _get_protocol_attrs(cls)
                             ~~~~~~~~~~~~~~~~~~~^^^^^
  File "/home/joren/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/typing.py", line 1856, in _get_protocol_attrs
    annotations = _lazy_annotationlib.get_annotations(
        base, format=_lazy_annotationlib.Format.FORWARDREF
    )
  File "/home/joren/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/annotationlib.py", line 982, in get_annotations
    ann = _get_and_call_annotate(obj, format)
  File "/home/joren/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/annotationlib.py", line 1121, in _get_and_call_annotate
    ann = call_annotate_function(annotate, format, owner=obj)
  File "/home/joren/.local/share/uv/python/cpython-3.14.3-linux-x86_64-gnu/lib/python3.14/annotationlib.py", line 720, in call_annotate_function
    return annotate(format)
TypeError: CanAnnotate.__annotate__() missing 1 required positional argument: 'format'

On Python 3.13 (when you replace the annotationlib.Format with Any), there is no error.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-151899

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 typing.py 中的 _get_protocol_attrs 和 annotationlib.py 中的註解處理路徑開始,尤其關注 get_annotations 和 call_annotate_function。在 Python 3.14 上重現 CanAnnotate 範例,然後驗證定義 protocol 不再引發 TypeError,同時不破壞註解求值。

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

評估

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

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

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