Skip to content

gh-103171: Document and test behaviour change in 3.11 for runtime-checkable protocols decorated with @final #103173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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
""""""""""""""""""""""""

Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down