@@ -4,7 +4,7 @@ Getting started
4
4
===============
5
5
6
6
This chapter introduces some core concepts of mypy, including function
7
- annotations, the `` typing ` ` module, library stubs, and more.
7
+ annotations, the :py:mod: ` typing ` module, library stubs, and more.
8
8
9
9
Be sure to read this chapter carefully, as the rest of the documentation
10
10
may not make much sense otherwise.
@@ -160,16 +160,16 @@ Arguments with default values can be annotated like so:
160
160
for key, value in kwargs:
161
161
print (key, value)
162
162
163
- The typing module
164
- *****************
163
+ The `` typing `` module
164
+ *********************
165
165
166
166
So far, we've added type hints that use only basic concrete types like
167
167
``str `` and ``float ``. What if we want to express more complex types,
168
168
such as "a list of strings" or "an iterable of ints"?
169
169
170
- You can find many of these more complex static types inside of the `` typing ` `
170
+ You can find many of these more complex static types inside of the :py:mod: ` typing `
171
171
module. For example, to indicate that some function can accept a list of
172
- strings, use the `` List `` type from the `` typing `` module :
172
+ strings, use the :py:class: ` ~typing. List ` type:
173
173
174
174
.. code-block :: python
175
175
@@ -185,16 +185,16 @@ strings, use the ``List`` type from the ``typing`` module:
185
185
greet_all(names) # Ok!
186
186
greet_all(ages) # Error due to incompatible types
187
187
188
- The `` List ` ` type is an example of something called a *generic type *: it can
189
- accept one or more *type parameters *. In this case, we *parameterized * `` List ` `
188
+ The :py:class: ` ~typing. List ` type is an example of something called a *generic type *: it can
189
+ accept one or more *type parameters *. In this case, we *parameterized * :py:class: ` ~typing. List `
190
190
by writing ``List[str] ``. This lets mypy know that ``greet_all `` accepts specifically
191
191
lists containing strings, and not lists containing ints or any other type.
192
192
193
193
In this particular case, the type signature is perhaps a little too rigid.
194
194
After all, there's no reason why this function must accept *specifically * a list --
195
195
it would run just fine if you were to pass in a tuple, a set, or any other custom iterable.
196
196
197
- You can express this idea using the `` Iterable `` type instead of `` List ` `:
197
+ You can express this idea using the :py:class: ` ~typing. Iterable ` type instead of :py:class: ` ~typing. List `:
198
198
199
199
.. code-block :: python
200
200
@@ -205,7 +205,7 @@ You can express this idea using the ``Iterable`` type instead of ``List``:
205
205
print (' Hello ' + name)
206
206
207
207
As another example, suppose you want to write a function that can accept *either *
208
- ints or strings, but no other types. You can express this using the `` Union ` ` type:
208
+ ints or strings, but no other types. You can express this using the :py:data: ` ~typing. Union ` type:
209
209
210
210
.. code-block :: python
211
211
@@ -217,8 +217,8 @@ ints or strings, but no other types. You can express this using the ``Union`` ty
217
217
else :
218
218
return user_id
219
219
220
- Similarly, suppose that you want the function to accept only strings or None. You can
221
- again use `` Union ` ` and use ``Union[str, None] `` -- or alternatively, use the type
220
+ Similarly, suppose that you want the function to accept only strings or `` None `` . You can
221
+ again use :py:data: ` ~typing. Union ` and use ``Union[str, None] `` -- or alternatively, use the type
222
222
``Optional[str] ``. These two types are identical and interchangeable: ``Optional[str] ``
223
223
is just a shorthand or *alias * for ``Union[str, None] ``. It exists mostly as a convenience
224
224
to help function signatures look a little cleaner:
@@ -233,7 +233,7 @@ to help function signatures look a little cleaner:
233
233
name = ' stranger'
234
234
return ' Hello, ' + name
235
235
236
- The `` typing ` ` module contains many other useful types. You can find a
236
+ The :py:mod: ` typing ` module contains many other useful types. You can find a
237
237
quick overview by looking through the :ref: `mypy cheatsheets <overview-cheat-sheets >`
238
238
and a more detailed overview (including information on how to make your own
239
239
generic types or your own type aliases) by looking through the
@@ -243,8 +243,8 @@ One final note: when adding types, the convention is to import types
243
243
using the form ``from typing import Iterable `` (as opposed to doing
244
244
just ``import typing `` or ``import typing as t `` or ``from typing import * ``).
245
245
246
- For brevity, we often omit these `` typing ` ` imports in code examples, but
247
- mypy will give an error if you use types such as `` Iterable ` `
246
+ For brevity, we often omit these :py:mod: ` typing ` imports in code examples, but
247
+ mypy will give an error if you use types such as :py:class: ` ~typing. Iterable `
248
248
without first importing them.
249
249
250
250
Local type inference
@@ -255,7 +255,7 @@ mypy will automatically type check that function's body. While doing so,
255
255
mypy will try and *infer * as many details as possible.
256
256
257
257
We saw an example of this in the ``normalize_id `` function above -- mypy understands
258
- basic `` isinstance ` ` checks and so can infer that the ``user_id `` variable was of
258
+ basic :py:func: ` isinstance <isinstance> ` checks and so can infer that the ``user_id `` variable was of
259
259
type ``int `` in the if-branch and of type ``str `` in the else-branch. Similarly, mypy
260
260
was able to understand that ``name `` could not possibly be ``None `` in the ``greeting ``
261
261
function above, based both on the ``name is None `` check and the variable assignment
@@ -315,7 +315,7 @@ For example, consider this code:
315
315
x = chr (4 )
316
316
317
317
Without a library stub, mypy would have no way of inferring the type of ``x ``
318
- and checking that the argument to `` chr ` ` has a valid type.
318
+ and checking that the argument to :py:func: ` chr ` has a valid type.
319
319
320
320
Mypy complains if it can't find a stub (or a real module) for a
321
321
library module that you import. Some modules ship with stubs that mypy
0 commit comments