FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-132493: lazy evaluation of annotations in `typing._proto_hook` by felixscherz · Pull Request #132534 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
27 changes: 27 additions & 0 deletions Lib/test/test_typing.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -4554,6 +4554,33 @@ class Commentable(Protocol):
)
self.assertIs(type(exc.__cause__), CustomError)

def test_isinstance_with_deferred_evaluation_of_annotations(self):
@runtime_checkable
class P(Protocol):
def meth(self):
...

class DeferredClass:
x: undefined

class DeferredClassImplementingP:
Comment thread
JelleZijlstra marked this conversation as resolved.
x: undefined | int

def __init__(self):
self.x = 0

def meth(self):
...

# override meth with a non-method attribute to make it part of __annotations__ instead of __dict__
class SubProtocol(P, Protocol):
meth: undefined


self.assertIsSubclass(SubProtocol, P)
self.assertNotIsInstance(DeferredClass(), P)
self.assertIsInstance(DeferredClassImplementingP(), P)

def test_deferred_evaluation_of_annotations(self):
class DeferredProto(Protocol):
x: DoesNotExist
Expand Down
11 changes: 7 additions & 4 deletions Lib/typing.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -2020,10 +2020,13 @@ 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)):
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
Expand Down
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
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.

Back | FazBrowse Home | New Git URL