Skip to content

gh-146643: Fix Protocol creation crashing on an __annotate__ member#151899

Open
iamsharduld wants to merge 1 commit into
python:mainfrom
iamsharduld:local-gh-146643-protocol-annotate
Open

gh-146643: Fix Protocol creation crashing on an __annotate__ member#151899
iamsharduld wants to merge 1 commit into
python:mainfrom
iamsharduld:local-gh-146643-protocol-annotate

Conversation

@iamsharduld

@iamsharduld iamsharduld commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

typing._get_protocol_attrs() reads base.__annotations__, and on failure falls
back to annotationlib.get_annotations(base, FORWARDREF). When a Protocol body
(or one of its bases) declares a member named __annotate__ -- a reserved name --
whose value is an ordinary method rather than a valid annotate function, both
retrieval paths invoke it with the wrong arity and raise TypeError, so merely
defining the Protocol failed.

Wrap the annotationlib.get_annotations() fallback in try/except, treating a
base whose annotations cannot be retrieved as un-annotated instead of letting the
class definition fail. This mirrors the existing try/except around
base.__annotations__.

Note: because __annotate__ is in EXCLUDED_ATTRIBUTES, such a Protocol still
collects no members; this only addresses the crash.

…mber

_get_protocol_attrs() collects annotation names via annotationlib when a
base's __annotations__ cannot be read directly. When a Protocol member is
named __annotate__ (a reserved name) but is an ordinary method rather than
a valid annotate function, both annotation-retrieval paths raised
TypeError, so merely defining the Protocol failed. Treat a base whose
annotations cannot be retrieved as un-annotated instead of propagating the
error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant