Skip to content

Commit 1163782

Browse files
authored
gh-104555: Fix isinstance() and issubclass() for runtime-checkable protocols that use PEP 695 (#104556)
Fixes #104555
1 parent f40890b commit 1163782

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Lib/test/test_typing.py

+18
Original file line numberDiff line numberDiff line change
@@ -3134,6 +3134,24 @@ def bar(self, x: str) -> str:
31343134

31353135
self.assertIsInstance(Test(), PSub)
31363136

3137+
def test_pep695_generic_protocol_callable_members(self):
3138+
@runtime_checkable
3139+
class Foo[T](Protocol):
3140+
def meth(self, x: T) -> None: ...
3141+
3142+
class Bar[T]:
3143+
def meth(self, x: T) -> None: ...
3144+
3145+
self.assertIsInstance(Bar(), Foo)
3146+
self.assertIsSubclass(Bar, Foo)
3147+
3148+
@runtime_checkable
3149+
class SupportsTrunc[T](Protocol):
3150+
def __trunc__(self) -> T: ...
3151+
3152+
self.assertIsInstance(0.0, SupportsTrunc)
3153+
self.assertIsSubclass(float, SupportsTrunc)
3154+
31373155
def test_init_called(self):
31383156
T = TypeVar('T')
31393157

Lib/typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ class _TypingEllipsis:
16631663
_TYPING_INTERNALS = frozenset({
16641664
'__parameters__', '__orig_bases__', '__orig_class__',
16651665
'_is_protocol', '_is_runtime_protocol', '__protocol_attrs__',
1666-
'__callable_proto_members_only__',
1666+
'__callable_proto_members_only__', '__type_params__',
16671667
})
16681668

16691669
_SPECIAL_NAMES = frozenset({

0 commit comments

Comments
 (0)