Skip to content

Commit 829e8fc

Browse files
committed
revisited getting_started.rst
Signed-off-by: Oleg Höfling <[email protected]>
1 parent a4263dc commit 829e8fc

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

docs/source/getting_started.rst

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Getting started
44
===============
55

66
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.
88

99
Be sure to read this chapter carefully, as the rest of the documentation
1010
may not make much sense otherwise.
@@ -160,16 +160,16 @@ Arguments with default values can be annotated like so:
160160
for key, value in kwargs:
161161
print(key, value)
162162
163-
The typing module
164-
*****************
163+
The ``typing`` module
164+
*********************
165165

166166
So far, we've added type hints that use only basic concrete types like
167167
``str`` and ``float``. What if we want to express more complex types,
168168
such as "a list of strings" or "an iterable of ints"?
169169

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`
171171
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:
173173

174174
.. code-block:: python
175175
@@ -185,16 +185,16 @@ strings, use the ``List`` type from the ``typing`` module:
185185
greet_all(names) # Ok!
186186
greet_all(ages) # Error due to incompatible types
187187
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`
190190
by writing ``List[str]``. This lets mypy know that ``greet_all`` accepts specifically
191191
lists containing strings, and not lists containing ints or any other type.
192192

193193
In this particular case, the type signature is perhaps a little too rigid.
194194
After all, there's no reason why this function must accept *specifically* a list --
195195
it would run just fine if you were to pass in a tuple, a set, or any other custom iterable.
196196

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`:
198198

199199
.. code-block:: python
200200
@@ -205,7 +205,7 @@ You can express this idea using the ``Iterable`` type instead of ``List``:
205205
print('Hello ' + name)
206206
207207
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:
209209

210210
.. code-block:: python
211211
@@ -217,8 +217,8 @@ ints or strings, but no other types. You can express this using the ``Union`` ty
217217
else:
218218
return user_id
219219
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
222222
``Optional[str]``. These two types are identical and interchangeable: ``Optional[str]``
223223
is just a shorthand or *alias* for ``Union[str, None]``. It exists mostly as a convenience
224224
to help function signatures look a little cleaner:
@@ -233,7 +233,7 @@ to help function signatures look a little cleaner:
233233
name = 'stranger'
234234
return 'Hello, ' + name
235235
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
237237
quick overview by looking through the :ref:`mypy cheatsheets <overview-cheat-sheets>`
238238
and a more detailed overview (including information on how to make your own
239239
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
243243
using the form ``from typing import Iterable`` (as opposed to doing
244244
just ``import typing`` or ``import typing as t`` or ``from typing import *``).
245245

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`
248248
without first importing them.
249249

250250
Local type inference
@@ -255,7 +255,7 @@ mypy will automatically type check that function's body. While doing so,
255255
mypy will try and *infer* as many details as possible.
256256

257257
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
259259
type ``int`` in the if-branch and of type ``str`` in the else-branch. Similarly, mypy
260260
was able to understand that ``name`` could not possibly be ``None`` in the ``greeting``
261261
function above, based both on the ``name is None`` check and the variable assignment
@@ -315,7 +315,7 @@ For example, consider this code:
315315
x = chr(4)
316316
317317
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.
319319

320320
Mypy complains if it can't find a stub (or a real module) for a
321321
library module that you import. Some modules ship with stubs that mypy

0 commit comments

Comments
 (0)