Skip to content
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

Fix isinstance check for Generic classes #188

Merged
merged 2 commits into from
May 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

- Fix failing `type object ... has no attribute '_is_protocol'` on Python 3.7
dolfinus marked this conversation as resolved.
Show resolved Hide resolved

# Release 4.6.1 (May 23, 2023)

- Change deprecated `@runtime` to formal API `@runtime_checkable` in the error
Expand Down
20 changes: 20 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4320,6 +4320,24 @@ class Y(Generic[T], NamedTuple):
self.assertEqual(Y.__orig_bases__, (Generic[T], NamedTuple))
self.assertEqual(Y.__mro__, (Y, Generic, tuple, object))

class Z(Y[int]):
...
self.assertEqual(Z.__bases__, (Y,))
self.assertEqual(Z.__orig_bases__, (Y[int],))
self.assertEqual(Z.__mro__, (Z, Y, Generic, tuple, object))

@runtime_checkable
class Proto(Protocol):
@property
def x(self):
...

z = Z(3)
self.assertIs(type(z), Z)
self.assertIsInstance(z, Z)
self.assertIsInstance(z, Proto)
self.assertEqual(z.x, 3)
dolfinus marked this conversation as resolved.
Show resolved Hide resolved

for G in X, Y:
with self.subTest(type=G):
self.assertEqual(G.__parameters__, (T,))
Expand All @@ -4330,6 +4348,8 @@ class Y(Generic[T], NamedTuple):

a = A(3)
self.assertIs(type(a), G)
self.assertIsInstance(a, G)
self.assertIsInstance(a, Proto)
self.assertEqual(a.x, 3)

things = "arguments" if sys.version_info >= (3, 11) else "parameters"
Expand Down
5 changes: 3 additions & 2 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import typing
import warnings


__all__ = [
# Super-special typing primitives.
'Any',
Expand Down Expand Up @@ -659,7 +658,9 @@ def _proto_hook(cls, other):
isinstance(annotations, collections.abc.Mapping)
and attr in annotations
and issubclass(other, (typing.Generic, _ProtocolMeta))
and other._is_protocol
# All subclasses of Generic have an _is_proto attribute on 3.8+
# But not on 3.7
and getattr(other, "_is_protocol", False)
dolfinus marked this conversation as resolved.
Show resolved Hide resolved
):
break
else:
Expand Down