-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
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
Changes from 7 commits
34c6039
8ad1181
079cf59
998e83c
92e5ab9
3413f78
a82ac39
8c7a1e9
63e5a5b
a820f7f
19d925a
316741e
3d13c56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2020,10 +2020,10 @@ def _proto_hook(cls, other): | |
| break | ||
|
|
||
| # ...or in annotations, if it is a sub-protocol. | ||
| annotations = getattr(base, '__annotations__', {}) | ||
| if (isinstance(annotations, collections.abc.Mapping) and | ||
| attr in annotations and | ||
| issubclass(other, Generic) and getattr(other, '_is_protocol', False)): | ||
| 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)): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the issubclass() and geattr() checks up:)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 if (
issubclass(other, Generic)
and getattr(other, "_is_protocol", False)
and attr in _lazy_annotationlib.get_annotations(base, format=_lazy_annotationlib.Format.FORWARDREF)
): |
||
| break | ||
| else: | ||
| return NotImplemented | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| :class:`typing.Protocol` now uses :func:`annotationlib.get_annotations` when | ||
| checking whether or not an instance implements the protocol with | ||
| :func:`isinstance`. This enables support for ``isinstance`` checks against | ||
| classes with deferred annotations. |
Uh oh!
There was an error while loading. Please reload this page.