Skip to content

[3.13] typing docs: Clarify Protocol / runtime_checkable docs (GH-132606) #132607

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

Merged
merged 1 commit into from
Apr 16, 2025
Merged
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
8 changes: 5 additions & 3 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,8 @@ types.
See :pep:`544` for more details. Protocol classes decorated with
:func:`runtime_checkable` (described later) act as simple-minded runtime
protocols that check only the presence of given attributes, ignoring their
type signatures.
type signatures. Protocol classes without this decorator cannot be used
as the second argument to :func:`isinstance` or :func:`issubclass`.

Protocol classes can be generic, for example::

Expand All @@ -2377,8 +2378,7 @@ types.
Mark a protocol class as a runtime protocol.

Such a protocol can be used with :func:`isinstance` and :func:`issubclass`.
This raises :exc:`TypeError` when applied to a non-protocol class. This
allows a simple-minded structural check, very similar to "one trick ponies"
This allows a simple-minded structural check, very similar to "one trick ponies"
in :mod:`collections.abc` such as :class:`~collections.abc.Iterable`. For example::

@runtime_checkable
Expand All @@ -2394,6 +2394,8 @@ types.
import threading
assert isinstance(threading.Thread(name='Bob'), Named)

This decorator raises :exc:`TypeError` when applied to a non-protocol class.

.. note::

:func:`!runtime_checkable` will check only the presence of the required
Expand Down
Loading