Skip to content

Commit 54e3452

Browse files
hoeflingGuido van Rossum
authored and
Guido van Rossum
committed
Crossreferences to standard library in mypy docs, part 2 (#7660)
Signed-off-by: Oleg Höfling <oleg.hoefling@gmail.com>
1 parent 83748a4 commit 54e3452

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

docs/source/builtin_types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Type Description
2323
====================== ===============================
2424

2525
The type ``Any`` and type constructors such as ``List``, ``Dict``,
26-
``Iterable`` and ``Sequence`` are defined in the ``typing`` module.
26+
``Iterable`` and ``Sequence`` are defined in the :py:mod:`typing` module.
2727

2828
The type ``Dict`` is a *generic* class, signified by type arguments within
2929
``[...]``. For example, ``Dict[int, str]`` is a dictionary from integers to
@@ -35,6 +35,6 @@ strings and ``Dict[Any, Any]`` is a dictionary of dynamically typed
3535
correspond to Python protocols. For example, a ``str`` object or a
3636
``List[str]`` object is valid
3737
when ``Iterable[str]`` or ``Sequence[str]`` is expected. Note that even though
38-
they are similar to abstract base classes defined in ``collections.abc``
38+
they are similar to abstract base classes defined in :py:mod:`collections.abc`
3939
(formerly ``collections``), they are not identical, since the built-in
4040
collection type objects do not support indexing.

docs/source/common_issues.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ flagged as an error.
6969
:ref:`reveal_type() <reveal-type>` might come in handy.
7070

7171
Note that sometimes library stubs have imprecise type information,
72-
e.g. the ``pow()`` builtin returns ``Any`` (see `typeshed issue 285
72+
e.g. the :py:func:`pow` builtin returns ``Any`` (see `typeshed issue 285
7373
<https://github.com/python/typeshed/issues/285>`_ for the reason).
7474

7575
- **Some imports may be silently ignored**. Another source of
@@ -143,7 +143,7 @@ Another option is to explicitly annotate values with type ``Any`` --
143143
mypy will let you perform arbitrary operations on ``Any``
144144
values. Sometimes there is no more precise type you can use for a
145145
particular value, especially if you use dynamic Python features
146-
such as ``__getattr__``:
146+
such as :py:meth:`__getattr__ <object.__getattr__>`:
147147

148148
.. code-block:: python
149149
@@ -326,7 +326,7 @@ above example:
326326
Complex type tests
327327
------------------
328328

329-
Mypy can usually infer the types correctly when using ``isinstance()``
329+
Mypy can usually infer the types correctly when using :py:func:`isinstance <isinstance>`
330330
type tests, but for other kinds of checks you may need to add an
331331
explicit type cast:
332332

@@ -342,17 +342,17 @@ explicit type cast:
342342
343343
.. note::
344344

345-
Note that the ``object`` type used in the above example is similar
345+
Note that the :py:class:`object` type used in the above example is similar
346346
to ``Object`` in Java: it only supports operations defined for *all*
347-
objects, such as equality and ``isinstance()``. The type ``Any``,
347+
objects, such as equality and :py:func:`isinstance`. The type ``Any``,
348348
in contrast, supports all operations, even if they may fail at
349349
runtime. The cast above would have been unnecessary if the type of
350350
``o`` was ``Any``.
351351

352-
Mypy can't infer the type of ``o`` after the ``type()`` check
353-
because it only knows about ``isinstance()`` (and the latter is better
352+
Mypy can't infer the type of ``o`` after the :py:class:`type() <type>` check
353+
because it only knows about :py:func:`isinstance` (and the latter is better
354354
style anyway). We can write the above code without a cast by using
355-
``isinstance()``:
355+
:py:func:`isinstance`:
356356

357357
.. code-block:: python
358358
@@ -379,8 +379,8 @@ the targeted Python version or platform. This allows you to more effectively
379379
typecheck code that supports multiple versions of Python or multiple operating
380380
systems.
381381

382-
More specifically, mypy will understand the use of ``sys.version_info`` and
383-
``sys.platform`` checks within ``if/elif/else`` statements. For example:
382+
More specifically, mypy will understand the use of :py:data:`sys.version_info` and
383+
:py:data:`sys.platform` checks within ``if/elif/else`` statements. For example:
384384

385385
.. code-block:: python
386386
@@ -417,14 +417,14 @@ Example:
417417
# The rest of this file doesn't apply to Windows.
418418
419419
Some other expressions exhibit similar behavior; in particular,
420-
``typing.TYPE_CHECKING``, variables named ``MYPY``, and any variable
420+
:py:data:`~typing.TYPE_CHECKING`, variables named ``MYPY``, and any variable
421421
whose name is passed to ``--always-true`` or ``--always-false``.
422422
(However, ``True`` and ``False`` are not treated specially!)
423423

424424
.. note::
425425

426426
Mypy currently does not support more complex checks, and does not assign
427-
any special meaning when assigning a ``sys.version_info`` or ``sys.platform``
427+
any special meaning when assigning a :py:data:`sys.version_info` or :py:data:`sys.platform`
428428
check to a variable. This may change in future versions of mypy.
429429

430430
By default, mypy will use your current version of Python and your current
@@ -514,10 +514,10 @@ File ``bar.py``:
514514
515515
.. note::
516516

517-
The ``TYPE_CHECKING`` constant defined by the ``typing`` module
517+
The :py:data:`~typing.TYPE_CHECKING` constant defined by the :py:mod:`typing` module
518518
is ``False`` at runtime but ``True`` while type checking.
519519

520-
Python 3.5.1 doesn't have ``typing.TYPE_CHECKING``. An alternative is
520+
Python 3.5.1 doesn't have :py:data:`~typing.TYPE_CHECKING`. An alternative is
521521
to define a constant named ``MYPY`` that has the value ``False``
522522
at runtime. Mypy considers it to be ``True`` when type checking.
523523
Here's the above example modified to use ``MYPY``:
@@ -538,7 +538,7 @@ Using classes that are generic in stubs but not at runtime
538538
----------------------------------------------------------
539539

540540
Some classes are declared as generic in stubs, but not at runtime. Examples
541-
in the standard library include ``os.PathLike`` and ``queue.Queue``.
541+
in the standard library include :py:class:`os.PathLike` and :py:class:`queue.Queue`.
542542
Subscripting such a class will result in a runtime error:
543543

544544
.. code-block:: python
@@ -551,7 +551,7 @@ Subscripting such a class will result in a runtime error:
551551
results: Queue[int] = Queue() # TypeError: 'type' object is not subscriptable
552552
553553
To avoid these errors while still having precise types you can either use
554-
string literal types or ``typing.TYPE_CHECKING``:
554+
string literal types or :py:data:`~typing.TYPE_CHECKING`:
555555

556556
.. code-block:: python
557557
@@ -615,7 +615,7 @@ Consider this example:
615615
c.x << 5 # Since this will fail!
616616
617617
To work around this problem consider whether "mutating" is actually part
618-
of a protocol. If not, then one can use a ``@property`` in
618+
of a protocol. If not, then one can use a :py:class:`@property <property>` in
619619
the protocol definition:
620620

621621
.. code-block:: python

docs/source/config_file.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ For more information, see the :ref:`None and optional handling <none-and-optiona
254254
section of the command line docs.
255255

256256
``no_implicit_optional`` (bool, default False)
257-
Changes the treatment of arguments with a default value of None by not implicitly
258-
making their type Optional.
257+
Changes the treatment of arguments with a default value of ``None`` by not implicitly
258+
making their type :py:data:`~typing.Optional`.
259259

260260
``strict_optional`` (bool, default True)
261261
Enables or disables strict Optional checks. If False, mypy treats ``None``
@@ -377,8 +377,9 @@ a list of import discovery options that may be used
377377
User home directory and environment variables will be expanded.
378378

379379
``files`` (string)
380+
380381
A comma-separated list of paths which should be checked by mypy if none are given on the command
381-
line. Supports recursive file globbing using :doc:`library/glob`, where ``*`` (e.g. ``*.py``) matches
382+
line. Supports recursive file globbing using :py:mod:`glob`, where ``*`` (e.g. ``*.py``) matches
382383
files in the current directory and ``**/`` (e.g. ``**/*.py``) matches files in any directories below
383384
the current one. User home directory and environment variables will be expanded.
384385

@@ -399,7 +400,7 @@ section of the command line docs.
399400
Specifies the OS platform for the target program, for example
400401
``darwin`` or ``win32`` (meaning OS X or Windows, respectively).
401402
The default is the current platform as revealed by Python's
402-
``sys.platform`` variable.
403+
:py:data:`sys.platform` variable.
403404

404405

405406
Incremental mode
@@ -418,7 +419,7 @@ section of the command line docs.
418419
variable.
419420

420421
Note that the cache is only read when incremental mode is enabled
421-
but is always written to, unless the value is set to ``/dev/nul``
422+
but is always written to, unless the value is set to ``/dev/null``
422423
(UNIX) or ``nul`` (Windows).
423424

424425
``skip_version_check`` (bool, default False)
@@ -462,7 +463,7 @@ section of the command line docs.
462463
Shows traceback on fatal error.
463464

464465
``custom_typing_module`` (string)
465-
Specifies a custom module to use as a substitute for the ``typing`` module.
466+
Specifies a custom module to use as a substitute for the :py:mod:`typing` module.
466467

467468
``custom_typeshed_dir`` (string)
468469
Specifies an alternative directory to look for stubs instead of the

docs/source/dynamic_typing.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ less effective, unless you are careful.
5454
Any vs. object
5555
--------------
5656

57-
The type ``object`` is another type that can have an instance of arbitrary
58-
type as a value. Unlike ``Any``, ``object`` is an ordinary static type (it
57+
The type :py:class:`object` is another type that can have an instance of arbitrary
58+
type as a value. Unlike ``Any``, :py:class:`object` is an ordinary static type (it
5959
is similar to ``Object`` in Java), and only operations valid for *all*
60-
types are accepted for ``object`` values. These are all valid:
60+
types are accepted for :py:class:`object` values. These are all valid:
6161

6262
.. code-block:: python
6363
@@ -80,7 +80,7 @@ operations:
8080
n = 1 # type: int
8181
n = o # Error!
8282
83-
You can use ``cast()`` (see chapter :ref:`casts`) or ``isinstance`` to
84-
go from a general type such as ``object`` to a more specific
85-
type (subtype) such as ``int``. ``cast()`` is not needed with
83+
You can use :py:func:`~typing.cast` (see chapter :ref:`casts`) or :py:func:`isinstance` to
84+
go from a general type such as :py:class:`object` to a more specific
85+
type (subtype) such as ``int``. :py:func:`~typing.cast` is not needed with
8686
dynamically typed values (values with type ``Any``).

0 commit comments

Comments
 (0)