defining a `typing.Protocol` with `__annotate__` method raises `TypeError` since Python 3.14
Aperta
Nessuno ha ancora preso questa issue.
3.14
3.15
stdlib
topic-typing
type-bug
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con _get_protocol_attrs in typing.py e con il percorso di gestione delle annotazioni in annotationlib.py, in particolare get_annotations e call_annotate_function. Riproduci l’esempio CanAnnotate su Python 3.14, quindi verifica che la definizione del protocollo non sollevi più TypeError senza compromettere la valutazione delle annotazioni.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100