Skip to content

Commit 14efcad

Browse files
typing docs: Clarify Protocol / runtime_checkable docs (#132606)
- Explicitly say that isinstance/issubclass do not work on non-runtime checkable protocols. - Move the sentence "This raises TypeError when applied to a non-protocol class". It took me quite some time to decide what "this" was here: it refers to applying the decorator, not to an isinstance() call. Co-authored-by: Alex Waygood <[email protected]>
1 parent 2925462 commit 14efcad

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Doc/library/typing.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,7 +2455,8 @@ types.
24552455
See :pep:`544` for more details. Protocol classes decorated with
24562456
:func:`runtime_checkable` (described later) act as simple-minded runtime
24572457
protocols that check only the presence of given attributes, ignoring their
2458-
type signatures.
2458+
type signatures. Protocol classes without this decorator cannot be used
2459+
as the second argument to :func:`isinstance` or :func:`issubclass`.
24592460

24602461
Protocol classes can be generic, for example::
24612462

@@ -2479,8 +2480,7 @@ types.
24792480
Mark a protocol class as a runtime protocol.
24802481

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

24862486
@runtime_checkable
@@ -2496,6 +2496,8 @@ types.
24962496
import threading
24972497
assert isinstance(threading.Thread(name='Bob'), Named)
24982498

2499+
This decorator raises :exc:`TypeError` when applied to a non-protocol class.
2500+
24992501
.. note::
25002502

25012503
:func:`!runtime_checkable` will check only the presence of the required

0 commit comments

Comments
 (0)