python / python/cpython

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

オープン
#146,643 コメント 15 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

typing.py の _get_protocol_attrs と annotationlib.py のアノテーション処理パス、特に get_annotations と call_annotate_function から始めます。Python 3.14 で CanAnnotate の例を再現し、その後、アノテーションの評価を壊すことなくプロトコルの定義で TypeError が発生しなくなったことを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。