Skip to content

Commit 91b6740

Browse files
authored
Small improvements to protocol documentation (#15460)
1 parent 6f2bfff commit 91b6740

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

docs/source/protocols.rst

+24-26
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,24 @@
33
Protocols and structural subtyping
44
==================================
55

6-
Mypy supports two ways of deciding whether two classes are compatible
7-
as types: nominal subtyping and structural subtyping.
8-
9-
*Nominal* subtyping is strictly based on the class hierarchy. If class ``D``
10-
inherits class ``C``, it's also a subtype of ``C``, and instances of
11-
``D`` can be used when ``C`` instances are expected. This form of
12-
subtyping is used by default in mypy, since it's easy to understand
13-
and produces clear and concise error messages, and since it matches
14-
how the native :py:func:`isinstance <isinstance>` check works -- based on class
6+
The Python type system supports two ways of deciding whether two objects are
7+
compatible as types: nominal subtyping and structural subtyping.
8+
9+
*Nominal* subtyping is strictly based on the class hierarchy. If class ``Dog``
10+
inherits class ``Animal``, it's a subtype of ``Animal``. Instances of ``Dog``
11+
can be used when ``Animal`` instances are expected. This form of subtyping
12+
subtyping is what Python's type system predominantly uses: it's easy to
13+
understand and produces clear and concise error messages, and matches how the
14+
native :py:func:`isinstance <isinstance>` check works -- based on class
1515
hierarchy.
1616

17-
*Structural* subtyping is based on the operations that can be performed with an object. Class ``D`` is
18-
a structural subtype of class ``C`` if the former has all attributes
19-
and methods of the latter, and with compatible types.
17+
*Structural* subtyping is based on the operations that can be performed with an
18+
object. Class ``Dog`` is a structural subtype of class ``Animal`` if the former
19+
has all attributes and methods of the latter, and with compatible types.
2020

21-
Structural subtyping can be seen as a static equivalent of duck
22-
typing, which is well known to Python programmers. Mypy provides
23-
support for structural subtyping via protocol classes described
24-
below. See :pep:`544` for the detailed specification of protocols
25-
and structural subtyping in Python.
21+
Structural subtyping can be seen as a static equivalent of duck typing, which is
22+
well known to Python programmers. See :pep:`544` for the detailed specification
23+
of protocols and structural subtyping in Python.
2624

2725
.. _predefined_protocols:
2826

@@ -60,8 +58,7 @@ For example, ``IntList`` below is iterable, over ``int`` values:
6058
6159
:ref:`predefined_protocols_reference` lists all protocols defined in
6260
:py:mod:`typing` and the signatures of the corresponding methods you need to define
63-
to implement each protocol (the signatures can be left out, as always, but mypy
64-
won't type check unannotated methods).
61+
to implement each protocol.
6562

6663
Simple user-defined protocols
6764
*****************************
@@ -89,18 +86,12 @@ class:
8986
for item in items:
9087
item.close()
9188
92-
close_all([Resource(), open('some/file')]) # Okay!
89+
close_all([Resource(), open('some/file')]) # OK
9390
9491
``Resource`` is a subtype of the ``SupportsClose`` protocol since it defines
9592
a compatible ``close`` method. Regular file objects returned by :py:func:`open` are
9693
similarly compatible with the protocol, as they support ``close()``.
9794

98-
.. note::
99-
100-
The ``Protocol`` base class is provided in the ``typing_extensions``
101-
package for Python 3.4-3.7. Starting with Python 3.8, ``Protocol``
102-
is included in the ``typing`` module.
103-
10495
Defining subprotocols and subclassing protocols
10596
***********************************************
10697

@@ -171,6 +162,13 @@ abstract:
171162
ExplicitSubclass() # error: Cannot instantiate abstract class 'ExplicitSubclass'
172163
# with abstract attributes 'attr' and 'method'
173164
165+
Similarly, explicitly assigning to a protocol instance can be a way to ask the
166+
type checker to verify that your class implements a protocol:
167+
168+
.. code-block:: python
169+
170+
_proto: SomeProto = cast(ExplicitSubclass, None)
171+
174172
Invariance of protocol attributes
175173
*********************************
176174

0 commit comments

Comments
 (0)