Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4554,6 +4554,18 @@ class Commentable(Protocol):
)
self.assertIs(type(exc.__cause__), CustomError)

def test_isinstance_with_deffered_evaluation_of_annotations(self):
@runtime_checkable
class P(Protocol):
x: undefined
def meth(self):
...

class DeferredClass:
x: undefined

self.assertFalse(isinstance(DeferredClass(), P))

Copy link
Member

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.

Copy link
Contributor Author

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:)


class GenericTests(BaseTestCase):

Expand Down
4 changes: 2 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ def _get_protocol_attrs(cls):
for base in cls.__mro__[:-1]: # without object
if base.__name__ in {'Protocol', 'Generic'}:
continue
annotations = getattr(base, '__annotations__', {})
annotations = _lazy_annotationlib.get_annotations(base, format=_lazy_annotationlib.Format.FORWARDREF)
for attr in (*base.__dict__, *annotations):
if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
attrs.add(attr)
Expand Down Expand Up @@ -2018,7 +2018,7 @@ def _proto_hook(cls, other):
break

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