[Python-checkins] [3.11] gh-103171: Revert undocumented behaviour change for runtime-checkable protocols decorated with `@final` (#105445)
AlexWaygood
webhook-mailer at python.org
Wed Jun 7 17:18:33 EDT 2023
https://github.com/python/cpython/commit/18e9fd80d956fd39ded581f91a9448aeff74b913
commit: 18e9fd80d956fd39ded581f91a9448aeff74b913
branch: 3.11
author: Alex Waygood <Alex.Waygood at Gmail.com>
committer: AlexWaygood <Alex.Waygood at Gmail.com>
date: 2023-06-07T21:18:21Z
summary:
[3.11] gh-103171: Revert undocumented behaviour change for runtime-checkable protocols decorated with `@final` (#105445)
files:
A Misc/NEWS.d/next/Library/2023-06-07-14-24-39.gh-issue-103171.b3VJMD.rst
M Lib/test/test_typing.py
M Lib/typing.py
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 4dfe45302386f..316b30076394a 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3349,6 +3349,71 @@ def __init__(self):
Foo() # Previously triggered RecursionError
+ def test_empty_protocol_decorated_with_final(self):
+ @final
+ @runtime_checkable
+ class EmptyProtocol(Protocol): ...
+
+ self.assertIsSubclass(object, EmptyProtocol)
+ self.assertIsInstance(object(), EmptyProtocol)
+
+ def test_protocol_decorated_with_final_callable_members(self):
+ @final
+ @runtime_checkable
+ class ProtocolWithMethod(Protocol):
+ def startswith(self, string: str) -> bool: ...
+
+ self.assertIsSubclass(str, ProtocolWithMethod)
+ self.assertNotIsSubclass(int, ProtocolWithMethod)
+ self.assertIsInstance('foo', ProtocolWithMethod)
+ self.assertNotIsInstance(42, ProtocolWithMethod)
+
+ def test_protocol_decorated_with_final_noncallable_members(self):
+ @final
+ @runtime_checkable
+ class ProtocolWithNonCallableMember(Protocol):
+ x: int
+
+ class Foo:
+ x = 42
+
+ only_callable_members_please = (
+ r"Protocols with non-method members don't support issubclass()"
+ )
+
+ with self.assertRaisesRegex(TypeError, only_callable_members_please):
+ issubclass(Foo, ProtocolWithNonCallableMember)
+
+ with self.assertRaisesRegex(TypeError, only_callable_members_please):
+ issubclass(int, ProtocolWithNonCallableMember)
+
+ self.assertIsInstance(Foo(), ProtocolWithNonCallableMember)
+ self.assertNotIsInstance(42, ProtocolWithNonCallableMember)
+
+ def test_protocol_decorated_with_final_mixed_members(self):
+ @final
+ @runtime_checkable
+ class ProtocolWithMixedMembers(Protocol):
+ x: int
+ def method(self) -> None: ...
+
+ class Foo:
+ x = 42
+ def method(self) -> None: ...
+
+ only_callable_members_please = (
+ r"Protocols with non-method members don't support issubclass()"
+ )
+
+ with self.assertRaisesRegex(TypeError, only_callable_members_please):
+ issubclass(Foo, ProtocolWithMixedMembers)
+
+ with self.assertRaisesRegex(TypeError, only_callable_members_please):
+ issubclass(int, ProtocolWithMixedMembers)
+
+ self.assertIsInstance(Foo(), ProtocolWithMixedMembers)
+ self.assertNotIsInstance(42, ProtocolWithMixedMembers)
+
class GenericTests(BaseTestCase):
diff --git a/Lib/typing.py b/Lib/typing.py
index e6ef344ef43a2..f01b8c38a8f19 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1883,7 +1883,7 @@ class _TypingEllipsis:
_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
- '_is_protocol', '_is_runtime_protocol']
+ '_is_protocol', '_is_runtime_protocol', '__final__']
_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
'__init__', '__module__', '__new__', '__slots__',
diff --git a/Misc/NEWS.d/next/Library/2023-06-07-14-24-39.gh-issue-103171.b3VJMD.rst b/Misc/NEWS.d/next/Library/2023-06-07-14-24-39.gh-issue-103171.b3VJMD.rst
new file mode 100644
index 0000000000000..8c424d49012b8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-06-07-14-24-39.gh-issue-103171.b3VJMD.rst
@@ -0,0 +1,4 @@
+Revert undocumented behaviour change with runtime-checkable protocols
+decorated with :func:`typing.final` in Python 3.11. The behaviour change had
+meant that objects would not be considered instances of these protocols at
+runtime unless they had a ``__final__`` attribute. Patch by Alex Waygood.
More information about the Python-checkins
mailing list