python / python/cpython

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

Offen
#146,643 15 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

3.14 3.15 stdlib topic-typing type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit _get_protocol_attrs in typing.py und dem Pfad zur Verarbeitung von Annotationen in annotationlib.py, insbesondere get_annotations und call_annotate_function. Reproduziere das CanAnnotate-Beispiel unter Python 3.14 und überprüfe anschließend, dass die Definition des Protokolls keinen TypeError mehr auslöst, ohne die Auswertung von Annotationen zu beeinträchtigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.