From d3c3401b5fb46852b40687eeb8baeddaaaaa560f Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 16 May 2023 17:01:08 +0100 Subject: [PATCH 1/2] Fix isinstance() and issubclass() for runtime-checkable protocols that use PEP 695 --- Lib/test/test_typing.py | 18 ++++++++++++++++++ Lib/typing.py | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 71aff87dcaa3c9..9ac978533c0059 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -3134,6 +3134,24 @@ def bar(self, x: str) -> str: self.assertIsInstance(Test(), PSub) + def test_pep695_generic_protocol_callable_members(self): + @runtime_checkable + class Foo[T_co](Protocol): + def meth(self, x: T_co) -> None: ... + + class Bar[T_co]: + def meth(self, x: T_co) -> None: ... + + self.assertIsInstance(Bar(), Foo) + self.assertIsSubclass(Bar, Foo) + + @runtime_checkable + class SupportsTrunc[T_co](Protocol): + def __trunc__(self) -> T_co: ... + + self.assertIsInstance(0.0, SupportsTrunc) + self.assertIsSubclass(float, SupportsTrunc) + def test_init_called(self): T = TypeVar('T') diff --git a/Lib/typing.py b/Lib/typing.py index 8d132e2cbf8771..50a8f515945804 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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({ From 6d3c19a59702cb491a0dbac57efda3f53b7f22e6 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 16 May 2023 17:07:55 +0100 Subject: [PATCH 2/2] all type variables are now named T --- Lib/test/test_typing.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 9ac978533c0059..0cd67c51e50838 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -3136,18 +3136,18 @@ def bar(self, x: str) -> str: def test_pep695_generic_protocol_callable_members(self): @runtime_checkable - class Foo[T_co](Protocol): - def meth(self, x: T_co) -> None: ... + class Foo[T](Protocol): + def meth(self, x: T) -> None: ... - class Bar[T_co]: - def meth(self, x: T_co) -> None: ... + class Bar[T]: + def meth(self, x: T) -> None: ... self.assertIsInstance(Bar(), Foo) self.assertIsSubclass(Bar, Foo) @runtime_checkable - class SupportsTrunc[T_co](Protocol): - def __trunc__(self) -> T_co: ... + class SupportsTrunc[T](Protocol): + def __trunc__(self) -> T: ... self.assertIsInstance(0.0, SupportsTrunc) self.assertIsSubclass(float, SupportsTrunc)