Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-132493: lazy evaluation of annotations in typing._proto_hook#132534
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-132493: lazy evaluation of annotations in typing._proto_hook#132534
Uh oh!
There was an error while loading. Please reload this page.
Conversation
felixscherz commented Apr 14, 2025 • edited by bedevere-app bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-app bot
Uh oh!
There was an error while loading. Please reload this page.
…th deferred annotations
JelleZijlstra left a comment
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.
Thank you!
Uh oh!
There was an error while loading. Please reload this page.
| x: undefined | ||
| self.assertFalse(isinstance(DeferredClass(), P)) | ||
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.
Also add a test with a class that does fulfill the protocol.
Also, please add a NEWS entry.
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.
I added a class that satisfies the protocol and a NEWS entry:)
Misc/NEWS.d/next/Library/2025-04-15-08-39-14.gh-issue-132493.V0gLkU.rst Outdated Show resolvedHide resolved
Uh oh!
There was an error while loading. Please reload this page.
Misc/NEWS.d/next/Library/2025-04-15-08-39-14.gh-issue-132493.V0gLkU.rst Outdated Show resolvedHide resolved
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: sobolevn <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
…aranteed Co-authored-by: Alex Waygood <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
Lib/typing.py Outdated
| annotations = _lazy_annotationlib.get_annotations(base, format=_lazy_annotationlib.Format.FORWARDREF) | ||
| if (attr in annotations | ||
| and issubclass(other, Generic) | ||
| and getattr(other, '_is_protocol', False)): |
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.
We can probably gain some performance by doing the issubclass() and getattr() checks before doing the annotations check, since they should be fast and for most use cases they'll return False (it's surely more common to do isinstance(<non protocol>, Protocol), not isinstance(<other protocol>, Protocol)).
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.
I moved the issubclass() and geattr() checks up:)
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.
With the new order of conditions we could also move the call to _lazy_annotationlib.get_annotations so we only do that in case the other two conditions are true, what do you think?
Like this:
if ( issubclass(other, Generic) andgetattr(other, "_is_protocol", False) andattrin_lazy_annotationlib.get_annotations(base, format=_lazy_annotationlib.Format.FORWARDREF) ):Uh oh!
There was an error while loading. Please reload this page.
Lib/test/test_typing.py Outdated
| meth: undefined | ||
| self.assertTrue(issubclass(SubProtocol, P)) |
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.
These can use helpers assertIsSubclass, assertIsInstance, assertNotIsinstance.
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.
Updated it to use the helper methods
Co-authored-by: Jelle Zijlstra <[email protected]>
71af090 into python:mainUh oh!
There was an error while loading. Please reload this page.
Hi, this is in regards to #132493. This PR avoids eager evaluation of annotations during
isinstancechecks for protocols.