@@ -10,7 +10,7 @@ inherits class ``C``, it's also a subtype of ``C``, and instances of
10
10
``D `` can be used when ``C `` instances are expected. This form of
11
11
subtyping is used by default in mypy, since it's easy to understand
12
12
and produces clear and concise error messages, and since it matches
13
- how the native `` isinstance() ` ` check works -- based on class
13
+ how the native :py:func: ` isinstance <isinstance> ` check works -- based on class
14
14
hierarchy. *Structural * subtyping can also be useful. Class ``D `` is
15
15
a structural subtype of class ``C `` if the former has all attributes
16
16
and methods of the latter, and with compatible types.
@@ -26,10 +26,10 @@ and structural subtyping in Python.
26
26
Predefined protocols
27
27
********************
28
28
29
- The `` typing ` ` module defines various protocol classes that correspond
30
- to common Python protocols, such as `` Iterable[T] ``. If a class
31
- defines a suitable `` __iter__ ` ` method, mypy understands that it
32
- implements the iterable protocol and is compatible with `` Iterable[T] ` `.
29
+ The :py:mod: ` typing ` module defines various protocol classes that correspond
30
+ to common Python protocols, such as :py:class: ` Iterable[T] <typing.Iterable> `. If a class
31
+ defines a suitable :py:meth: ` __iter__ <object.__iter__> ` method, mypy understands that it
32
+ implements the iterable protocol and is compatible with :py:class: ` Iterable[T] <typing.Iterable> `.
33
33
For example, ``IntList `` below is iterable, over ``int `` values:
34
34
35
35
.. code-block :: python
@@ -56,7 +56,7 @@ For example, ``IntList`` below is iterable, over ``int`` values:
56
56
print_numbered([4 , 5 ]) # Also OK
57
57
58
58
The subsections below introduce all built-in protocols defined in
59
- `` typing ` ` and the signatures of the corresponding methods you need to define
59
+ :py:mod: ` typing ` and the signatures of the corresponding methods you need to define
60
60
to implement each protocol (the signatures can be left out, as always, but mypy
61
61
won't type check unannotated methods).
62
62
@@ -66,170 +66,200 @@ Iteration protocols
66
66
The iteration protocols are useful in many contexts. For example, they allow
67
67
iteration of objects in for loops.
68
68
69
- `` Iterable[T] ``
70
- ---------------
69
+ Iterable[T]
70
+ -----------
71
71
72
72
The :ref: `example above <predefined_protocols >` has a simple implementation of an
73
- `` __iter__ ` ` method.
73
+ :py:meth: ` __iter__ <object.__iter__> ` method.
74
74
75
75
.. code-block :: python
76
76
77
77
def __iter__ (self ) -> Iterator[T]
78
78
79
- `` Iterator[T]``
80
- -------------- -
79
+ See also :py:class :`~ typing.Iterable` .
80
+
81
+ Iterator[T]
82
+ -----------
81
83
82
84
.. code-block :: python
83
85
84
86
def __next__ (self ) -> T
85
87
def __iter__ (self ) -> Iterator[T]
86
88
89
+ See also :py:class :`~ typing.Iterator` .
90
+
87
91
Collection protocols
88
92
....................
89
93
90
94
Many of these are implemented by built-in container types such as
91
- `` list `` and `` dict ` ` , and these are also useful for user- defined
95
+ :py:class: ` list ` and :py:class: ` dict `, and these are also useful for user-defined
92
96
collection objects.
93
97
94
- `` Sized``
95
- -------- -
98
+ Sized
99
+ -----
96
100
97
- This is a type for objects that support `` len (x)` ` .
101
+ This is a type for objects that support :py:func: ` len(x) <len> `.
98
102
99
103
.. code-block :: python
100
104
101
105
def __len__ (self ) -> int
102
106
103
- `` Container[T]``
104
- ----------------
107
+ See also :py:class :`~ typing.Sized` .
108
+
109
+ Container[T]
110
+ ------------
105
111
106
112
This is a type for objects that support the ``in `` operator.
107
113
108
114
.. code-block :: python
109
115
110
116
def __contains__ (self , x : object ) -> bool
111
117
112
- `` Collection[T]``
113
- ---------------- -
118
+ See also :py:class :`~ typing.Container` .
119
+
120
+ Collection[T]
121
+ -------------
114
122
115
123
.. code-block :: python
116
124
117
125
def __len__ (self ) -> int
118
126
def __iter__ (self ) -> Iterator[T]
119
127
def __contains__ (self , x: object ) -> bool
120
128
129
+ See also :py:class :`~ typing.Collection` .
130
+
121
131
One-off protocols
122
132
.................
123
133
124
134
These protocols are typically only useful with a single standard
125
135
library function or class.
126
136
127
- `` Reversible[T]``
128
- ---------------- -
137
+ Reversible[T]
138
+ -------------
129
139
130
- This is a type for objects that support `` reversed (x)` ` .
140
+ This is a type for objects that support :py:func: ` reversed(x) <reversed> `.
131
141
132
142
.. code-block :: python
133
143
134
144
def __reversed__ (self ) -> Iterator[T]
135
145
136
- `` SupportsAbs[T]``
137
- ------------------
146
+ See also :py:class :`~ typing.Reversible` .
138
147
139
- This is a type for objects that support `` abs (x)`` . `` T`` is the type of
140
- value returned by `` abs (x)`` .
148
+ SupportsAbs[T]
149
+ --------------
150
+
151
+ This is a type for objects that support :py:func: `abs(x) <abs> `. ``T `` is the type of
152
+ value returned by :py:func: `abs(x) <abs> `.
141
153
142
154
.. code-block :: python
143
155
144
156
def __abs__ (self ) -> T
145
157
146
- `` SupportsBytes``
147
- ---------------- -
158
+ See also :py:class :`~ typing.SupportsAbs` .
148
159
149
- This is a type for objects that support `` bytes (x)`` .
160
+ SupportsBytes
161
+ -------------
162
+
163
+ This is a type for objects that support :py:class: `bytes(x) <bytes> `.
150
164
151
165
.. code-block :: python
152
166
153
167
def __bytes__ (self ) -> bytes
154
168
169
+ See also :py:class :`~ typing.SupportsBytes` .
170
+
155
171
.. _supports-int-etc :
156
172
157
- `` SupportsComplex``
158
- ------------------ -
173
+ SupportsComplex
174
+ ---------------
159
175
160
- This is a type for objects that support `` complex (x)` ` . Note that no arithmetic operations
176
+ This is a type for objects that support :py:class: ` complex(x) <complex> `. Note that no arithmetic operations
161
177
are supported.
162
178
163
179
.. code-block :: python
164
180
165
181
def __complex__ (self ) -> complex
166
182
167
- `` SupportsFloat``
168
- ---------------- -
183
+ See also :py:class :`~ typing.SupportsComplex` .
169
184
170
- This is a type for objects that support `` float (x)`` . Note that no arithmetic operations
185
+ SupportsFloat
186
+ -------------
187
+
188
+ This is a type for objects that support :py:class: `float(x) <float> `. Note that no arithmetic operations
171
189
are supported.
172
190
173
191
.. code-block :: python
174
192
175
193
def __float__ (self ) -> float
176
194
177
- `` SupportsInt``
178
- -------------- -
195
+ See also :py:class :`~ typing.SupportsFloat` .
179
196
180
- This is a type for objects that support `` int (x)`` . Note that no arithmetic operations
197
+ SupportsInt
198
+ -----------
199
+
200
+ This is a type for objects that support :py:class: `int(x) <int> `. Note that no arithmetic operations
181
201
are supported.
182
202
183
203
.. code-block :: python
184
204
185
205
def __int__ (self ) -> int
186
206
187
- `` SupportsRound[T]``
188
- --------------------
207
+ See also :py:class :`~ typing.SupportsInt` .
208
+
209
+ SupportsRound[T]
210
+ ----------------
189
211
190
- This is a type for objects that support `` round (x)` ` .
212
+ This is a type for objects that support :py:func: ` round(x) <round> `.
191
213
192
214
.. code-block :: python
193
215
194
216
def __round__ (self ) -> T
195
217
218
+ See also :py:class :`~ typing.SupportsRound` .
219
+
196
220
Async protocols
197
221
...............
198
222
199
223
These protocols can be useful in async code. See :ref: `async-and-await `
200
224
for more information.
201
225
202
- `` Awaitable[T]``
203
- ----------------
226
+ Awaitable[T]
227
+ ------------
204
228
205
229
.. code-block :: python
206
230
207
231
def __await__ (self ) -> Generator[Any, None , T]
208
232
209
- `` AsyncIterable[T]``
210
- --------------------
233
+ See also :py:class :`~ typing.Awaitable` .
234
+
235
+ AsyncIterable[T]
236
+ ----------------
211
237
212
238
.. code-block :: python
213
239
214
240
def __aiter__ (self ) -> AsyncIterator[T]
215
241
216
- `` AsyncIterator[T]``
217
- --------------------
242
+ See also :py:class :`~ typing.AsyncIterable` .
243
+
244
+ AsyncIterator[T]
245
+ ----------------
218
246
219
247
.. code-block :: python
220
248
221
249
def __anext__ (self ) -> Awaitable[T]
222
250
def __aiter__ (self ) -> AsyncIterator[T]
223
251
252
+ See also :py:class :`~ typing.AsyncIterator` .
253
+
224
254
Context manager protocols
225
255
.........................
226
256
227
257
There are two protocols for context managers -- one for regular context
228
258
managers and one for async ones. These allow defining objects that can
229
259
be used in ``with `` and ``async with `` statements.
230
260
231
- `` ContextManager[T]``
232
- -------------------- -
261
+ ContextManager[T]
262
+ -----------------
233
263
234
264
.. code-block :: python
235
265
@@ -239,8 +269,10 @@ be used in ``with`` and ``async with`` statements.
239
269
exc_value: Optional[BaseException ],
240
270
traceback: Optional[TracebackType]) -> Optional[bool ]
241
271
242
- `` AsyncContextManager[T]``
243
- --------------------------
272
+ See also :py:class :`~ typing.ContextManager` .
273
+
274
+ AsyncContextManager[T]
275
+ ----------------------
244
276
245
277
.. code-block :: python
246
278
@@ -250,6 +282,8 @@ be used in ``with`` and ``async with`` statements.
250
282
exc_value: Optional[BaseException ],
251
283
traceback: Optional[TracebackType]) -> Awaitable[Optional[bool ]]
252
284
285
+ See also :py:class :`~ typing.AsyncContextManager` .
286
+
253
287
Simple user-defined protocols
254
288
*****************************
255
289
@@ -278,7 +312,7 @@ class:
278
312
close_all([Resource(), open (' some/file' )]) # Okay!
279
313
280
314
``Resource `` is a subtype of the ``SupportsClose `` protocol since it defines
281
- a compatible `` close`` method. Regular file objects returned by `` open () ` ` are
315
+ a compatible ``close `` method. Regular file objects returned by :py:func: ` open ` are
282
316
similarly compatible with the protocol, as they support ``close() ``.
283
317
284
318
.. note ::
@@ -379,7 +413,7 @@ such as trees and linked lists:
379
413
Using isinstance() with protocols
380
414
*********************************
381
415
382
- You can use a protocol class with `` isinstance () ` ` if you decorate it
416
+ You can use a protocol class with :py:func: ` isinstance ` if you decorate it
383
417
with the ``@runtime_checkable `` class decorator. The decorator adds
384
418
support for basic runtime structural checks:
385
419
@@ -399,11 +433,11 @@ support for basic runtime structural checks:
399
433
if isinstance (mug, Portable):
400
434
use(mug.handles) # Works statically and at runtime
401
435
402
- `` isinstance () ` ` also works with the :ref:`predefined protocols < predefined_protocols> `
403
- in `` typing`` such as `` Iterable` ` .
436
+ :py:func: ` isinstance ` also works with the :ref: `predefined protocols <predefined_protocols >`
437
+ in :py:mod: ` typing ` such as :py:class: ` ~typing. Iterable `.
404
438
405
439
.. note ::
406
- `` isinstance () ` ` with protocols is not completely safe at runtime.
440
+ :py:func: ` isinstance ` with protocols is not completely safe at runtime.
407
441
For example, signatures of methods are not checked. The runtime
408
442
implementation only checks that all protocol members are defined.
409
443
@@ -413,8 +447,8 @@ Callback protocols
413
447
******************
414
448
415
449
Protocols can be used to define flexible callback types that are hard
416
- (or even impossible) to express using the `` Callable[... ]` ` syntax, such as variadic,
417
- overloaded, and complex generic callbacks. They are defined with a special `` __call__ ` `
450
+ (or even impossible) to express using the :py:data: ` Callable[...] <typing.Callable> ` syntax, such as variadic,
451
+ overloaded, and complex generic callbacks. They are defined with a special :py:meth: ` __call__ <object.__call__> `
418
452
member:
419
453
420
454
.. code-block :: python
@@ -438,8 +472,8 @@ member:
438
472
batch_proc([], bad_cb) # Error! Argument 2 has incompatible type because of
439
473
# different name and kind in the callback
440
474
441
- Callback protocols and `` Callable[ ... ] ` ` types can be used interchangeably.
442
- Keyword argument names in `` __call__ ` ` methods must be identical, unless
475
+ Callback protocols and :py:data: ` ~typing.Callable ` types can be used interchangeably.
476
+ Keyword argument names in :py:meth: ` __call__ <object.__call__> ` methods must be identical, unless
443
477
a double underscore prefix is used. For example:
444
478
445
479
.. code-block :: python
0 commit comments