]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-104555: Fix isinstance() and issubclass() for runtime-checkable protocols that...
authorAlex Waygood <Alex.Waygood@Gmail.com>
Tue, 16 May 2023 16:38:10 +0000 (17:38 +0100)
committerGitHub <noreply@github.com>
Tue, 16 May 2023 16:38:10 +0000 (16:38 +0000)
Fixes #104555

Lib/test/test_typing.py
Lib/typing.py

index 71aff87dcaa3c9ca44828ffd69183a505b85ea59..0cd67c51e50838eee4772ecbc1b2edbd7811befe 100644 (file)
@@ -3134,6 +3134,24 @@ class ProtocolTests(BaseTestCase):
 
         self.assertIsInstance(Test(), PSub)
 
+    def test_pep695_generic_protocol_callable_members(self):
+        @runtime_checkable
+        class Foo[T](Protocol):
+            def meth(self, x: T) -> None: ...
+
+        class Bar[T]:
+            def meth(self, x: T) -> None: ...
+
+        self.assertIsInstance(Bar(), Foo)
+        self.assertIsSubclass(Bar, Foo)
+
+        @runtime_checkable
+        class SupportsTrunc[T](Protocol):
+            def __trunc__(self) -> T: ...
+
+        self.assertIsInstance(0.0, SupportsTrunc)
+        self.assertIsSubclass(float, SupportsTrunc)
+
     def test_init_called(self):
         T = TypeVar('T')
 
index 8d132e2cbf8771db5d407853a23d2dc9cc704507..50a8f51594580412887639d8b64a27b8b68df365 100644 (file)
@@ -1663,7 +1663,7 @@ class _TypingEllipsis:
 _TYPING_INTERNALS = frozenset({
     '__parameters__', '__orig_bases__',  '__orig_class__',
     '_is_protocol', '_is_runtime_protocol', '__protocol_attrs__',
-    '__callable_proto_members_only__',
+    '__callable_proto_members_only__', '__type_params__',
 })
 
 _SPECIAL_NAMES = frozenset({