Skip to content

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

@jorenham

Description

@jorenham

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions