From 777f477e684b56e49b51922dd82f5187fec136bd Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 1 Apr 2023 16:35:02 +0100 Subject: [PATCH 1/2] gh-103171: Document behaviour change in 3.11 for runtime-checkable protocols decorated with `@final` --- Doc/library/typing.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 384458d3aa6618..c70f68a8efda12 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1619,6 +1619,13 @@ These are not used in annotations. They are building blocks for creating generic .. versionadded:: 3.8 + .. versionchanged:: 3.11 + Where a runtime-checkable protocol class ``X`` is decorated with + :func:`@typing.final `, an object ``y`` will now only be + considered an instance of ``X`` if the class of ``y`` is also decorated + with ``@final``. Runtime-checkable protocols decorated with ``@final`` + can also no longer be used in :func:`issubclass` calls. + Other special directives """""""""""""""""""""""" From 7acdd6043e3cbc4a59edc52e6fdf6f332821ac78 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 1 Apr 2023 17:00:02 +0100 Subject: [PATCH 2/2] Add a test, too --- Lib/test/test_typing.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 23bf2b5e183a18..91083febc60b92 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2597,6 +2597,22 @@ def meth(x): ... with self.assertRaises(TypeError): isinstance(C(), BadPG) + def test_protocols_isinstance_final(self): + @final + @runtime_checkable + class FinalHasX(Protocol): + x: int + + class Eggs: + x = 42 + + @final + class Spam: + x = 42 + + self.assertNotIsInstance(Eggs(), FinalHasX) + self.assertIsInstance(Spam(), FinalHasX) + def test_protocols_isinstance_properties_and_descriptors(self): class C: @property