gh-146643: Fix Protocol creation crashing on an __annotate__ member#151899
Open
iamsharduld wants to merge 1 commit into
Open
gh-146643: Fix Protocol creation crashing on an __annotate__ member#151899iamsharduld wants to merge 1 commit into
iamsharduld wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
typing._get_protocol_attrs()readsbase.__annotations__, and on failure fallsback 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 merelydefining the Protocol failed.
Wrap the
annotationlib.get_annotations()fallback intry/except, treating abase whose annotations cannot be retrieved as un-annotated instead of letting the
class definition fail. This mirrors the existing
try/exceptaroundbase.__annotations__.Note: because
__annotate__is inEXCLUDED_ATTRIBUTES, such a Protocol stillcollects no members; this only addresses the crash.
typing.Protocolwith__annotate__method raisesTypeErrorsince Python 3.14 #146643