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
gh-113320: Reduce the number of dangerous getattr() calls when constructing protocol classes
#113401
gh-113320: Reduce the number of dangerous getattr() calls when constructing protocol classes
#113401
Conversation
…n constructing protocol classes
|
Runtime protocols with members that raised when accessed on the class object have never worked — not even on Python <=3.11 (see #113320 (comment)) — so I don't think there are any backwards-compatibility issues here w.r.t. raising an error if someone tries to create one |
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.
Looks good, thank you!
|
Thanks for the review! |
|
Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
|
Sorry, @AlexWaygood, I could not cleanly backport this to |
|
GH-113722 is a backport of this pull request to the 3.12 branch. |
…lls when constructing protocol classes (python#113401) - Only attempt to figure out whether protocol members are "method members" or not if the class is marked as a runtime protocol. This information is irrelevant for non-runtime protocols; we can safely skip the risky introspection for them. - Only do the risky getattr() calls in one place (the runtime_checkable class decorator), rather than in three places (_ProtocolMeta.__init__, _ProtocolMeta.__instancecheck__ and _ProtocolMeta.__subclasscheck__). This reduces the number of locations in typing.py where the risky introspection could go wrong. - For runtime protocols, if determining whether a protocol member is callable or not fails, give a better error message. I think it's reasonable for us to reject runtime protocols that have members which raise strange exceptions when you try to access them. PEP-544 clearly states that all protocol member must be callable for issubclass() calls against the protocol to be valid -- and if a member raises when we try to access it, there's no way for us to figure out whether it's a callable member or not! (cherry-picked from commit ed6ea3e)
…en constructing protocol classes (#113401) (#113722) - Only attempt to figure out whether protocol members are "method members" or not if the class is marked as a runtime protocol. This information is irrelevant for non-runtime protocols; we can safely skip the risky introspection for them. - Only do the risky getattr() calls in one place (the runtime_checkable class decorator), rather than in three places (_ProtocolMeta.__init__, _ProtocolMeta.__instancecheck__ and _ProtocolMeta.__subclasscheck__). This reduces the number of locations in typing.py where the risky introspection could go wrong. - For runtime protocols, if determining whether a protocol member is callable or not fails, give a better error message. I think it's reasonable for us to reject runtime protocols that have members which raise strange exceptions when you try to access them. PEP-544 clearly states that all protocol member must be callable for issubclass() calls against the protocol to be valid -- and if a member raises when we try to access it, there's no way for us to figure out whether it's a callable member or not! (cherry-picked from commit ed6ea3e)


getattr()calls in one place (theruntime_checkableclass decorator), rather than in three places (_ProtocolMeta.__init__,_ProtocolMeta.__instancecheck__and_ProtocolMeta.__subclasscheck__). This reduces the number of locations intyping.pywhere the risky introspection could go wrong.issubclass()calls against the protocol to be valid -- and if a member raises when we try to access it, there's no way for us to figure out whether it's a callable member or not!Cc. @Gobot1234