Skip to content

Commit 985e267

Browse files
committed
Document runtime-checkability on protocols
1 parent 0adbbff commit 985e267

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Doc/library/typing.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,18 @@ types.
25542554
import threading
25552555
assert isinstance(threading.Thread(name='Bob'), Named)
25562556

2557+
Runtime checkability of protocols is not inherited. A subclass of a runtime-checkable protocol
2558+
is only runtime-checkable if is explicitly marked as such, regardless of class hierarchy::
2559+
2560+
@runtime_checkable
2561+
class Iterable(Protocol):
2562+
def __iter__(self): ...
2563+
2564+
# Without @runtime_checkable, Reversible would no longer be runtime-checkable.
2565+
@runtime_checkable
2566+
class Reversible(Iterable, Protocol):
2567+
def __reversed__(self): ...
2568+
25572569
This decorator raises :exc:`TypeError` when applied to a non-protocol class.
25582570

25592571
.. note::

0 commit comments

Comments
 (0)