X Tutup
The Wayback Machine - https://web.archive.org/web/20240109171018/https://github.com/python/cpython/pull/113401
Skip to content
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

Merged
merged 4 commits into from Jan 5, 2024

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Dec 22, 2023

  • 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!

Cc. @Gobot1234

Lib/test/test_typing.py Outdated Show resolved Hide resolved
@AlexWaygood
Copy link
Member Author

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

Copy link
Contributor

@carljm carljm left a 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!

Lib/test/test_typing.py Outdated Show resolved Hide resolved
@AlexWaygood
Copy link
Member Author

Thanks for the review!

@AlexWaygood AlexWaygood enabled auto-merge (squash) January 5, 2024 00:45
@AlexWaygood AlexWaygood merged commit ed6ea3e into python:main Jan 5, 2024
29 checks passed
@AlexWaygood AlexWaygood deleted the classproperties-protocols-2 branch January 5, 2024 01:01
@miss-islington-app
Copy link

Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app
Copy link

Sorry, @AlexWaygood, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker ed6ea3ea79fac68b127c7eb457c7ecb996461010 3.12

@bedevere-app
Copy link

bedevere-app bot commented Jan 5, 2024

GH-113722 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Jan 5, 2024
AlexWaygood added a commit to AlexWaygood/cpython that referenced this pull request Jan 5, 2024
…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)
AlexWaygood added a commit that referenced this pull request Jan 5, 2024
…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)
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.

None yet

2 participants
X Tutup