-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-74690: typing: Call _get_protocol_attrs
and _callable_members_only
at protocol class creation time, not during isinstance()
checks
#103160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f15c64a
gh-74690: typing: Cache results of `_get_protocol_attrs` and `_callab…
AlexWaygood f3ec3db
Simplify and inline
AlexWaygood 8e3c6f7
Use a metaclass instance method, not a classmethod on the class
AlexWaygood a34f700
Move to class-creation time
AlexWaygood 4d05074
Use `__init__`, not `__new__`
AlexWaygood d3f9ebe
Add missing `super().__init__()` call, to be safe
AlexWaygood e8a6304
Merge branch 'main' into protocol-attrs-cache-1
AlexWaygood 4816afa
Merge branch 'main' into protocol-attrs-cache-1
AlexWaygood 08af615
Update Lib/typing.py
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only reason I'm adding this method here rather than doing this work in
Protocol.__init_subclass__
is that it seems slightly more backwards-compatible. With this PR,_ProtcolMeta.__instancecheck__
assumes that all classes with_ProtocolMeta
as their metaclass will have a__protocol_attrs__
attribute. Since_ProtocolMeta
is an undocumented implementation detail, it should only beProtocol
andProtocol
subclasses using_ProtocolMeta
as their metaclass, and if we could count on that, then it would be safe to do this work inProtocol.__init_subclass__
. But it's possible users might have been reaching into the internals oftyping.py
and creating other classes that use_ProtocolMeta
as their metaclass, and I don't want to risk breaking their code unnecessarily.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed that there's at least a few uses of
_ProtocolMeta
out in the wild:https://github.com/antonagestam/phantom-types/blob/68ee857af4452a90a48669a8dbc7d1d7110b31d0/src/phantom/sized.py#L71-L82
https://github.com/protolambda/remerkleable/blob/91ed092d08ef0ba5ab076f0a34b0b371623db728/remerkleable/core.py#L18-L22
https://github.com/InterStella0/stella_bot/blob/bf5f5632bcd88670df90be67b888c282c6e83d99/utils/useful.py#L337-L343