Skip to content

Commit 34c6039

Browse files
committed
gh-132493: enable protocol isinstance checks against classes with deferred annotations
1 parent 6163841 commit 34c6039

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Lib/test/test_typing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4554,6 +4554,18 @@ class Commentable(Protocol):
45544554
)
45554555
self.assertIs(type(exc.__cause__), CustomError)
45564556

4557+
def test_isinstance_with_deffered_evaluation_of_annotations(self):
4558+
@runtime_checkable
4559+
class P(Protocol):
4560+
x: undefined
4561+
def meth(self):
4562+
...
4563+
4564+
class DeferredClass:
4565+
x: undefined
4566+
4567+
self.assertFalse(isinstance(DeferredClass(), P))
4568+
45574569

45584570
class GenericTests(BaseTestCase):
45594571

Lib/typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,7 +1801,7 @@ def _get_protocol_attrs(cls):
18011801
for base in cls.__mro__[:-1]: # without object
18021802
if base.__name__ in {'Protocol', 'Generic'}:
18031803
continue
1804-
annotations = getattr(base, '__annotations__', {})
1804+
annotations = _lazy_annotationlib.get_annotations(base, format=_lazy_annotationlib.Format.FORWARDREF)
18051805
for attr in (*base.__dict__, *annotations):
18061806
if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
18071807
attrs.add(attr)
@@ -2018,7 +2018,7 @@ def _proto_hook(cls, other):
20182018
break
20192019

20202020
# ...or in annotations, if it is a sub-protocol.
2021-
annotations = getattr(base, '__annotations__', {})
2021+
annotations = _lazy_annotationlib.get_annotations(base, format=_lazy_annotationlib.Format.FORWARDREF)
20222022
if (isinstance(annotations, collections.abc.Mapping) and
20232023
attr in annotations and
20242024
issubclass(other, Generic) and getattr(other, '_is_protocol', False)):

0 commit comments

Comments
 (0)