Skip to content

Commit f86b422

Browse files
hoeflingGuido van Rossum
authored and
Guido van Rossum
committed
Crossreferences to standard library in mypy docs (#7652)
Signed-off-by: Oleg Höfling <[email protected]>
1 parent beec11a commit f86b422

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

docs/source/additional_features.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ They can be defined using the :py:func:`@dataclasses.dataclass
2626
test = Application("Testing...") # OK
2727
bad = Application("Testing...", "with plugin") # Error: List[str] expected
2828
29-
Mypy will detect special methods (such as ``__lt__``) depending on the flags used to
29+
Mypy will detect special methods (such as :py:meth:`__lt__ <object.__lt__>`) depending on the flags used to
3030
define dataclasses. For example:
3131

3232
.. code-block:: python
@@ -72,10 +72,10 @@ and :pep:`557`.
7272
Caveats/Known Issues
7373
====================
7474

75-
Some functions in the ``dataclasses`` module, such as ``replace()`` and ``asdict()``,
75+
Some functions in the :py:mod:`dataclasses` module, such as :py:func:`~dataclasses.replace` and :py:func:`~dataclasses.asdict`,
7676
have imprecise (too permissive) types. This will be fixed in future releases.
7777

78-
Mypy does not yet recognize aliases of ``dataclasses.dataclass``, and will
78+
Mypy does not yet recognize aliases of :py:func:`dataclasses.dataclass <dataclasses.dataclass>`, and will
7979
probably never recognize dynamically computed decorators. The following examples
8080
do **not** work:
8181

@@ -140,7 +140,7 @@ If you're using ``auto_attribs=True`` you must use variable annotations.
140140
three: int = attr.ib(8)
141141
142142
Typeshed has a couple of "white lie" annotations to make type checking
143-
easier. ``attr.ib`` and ``attr.Factory`` actually return objects, but the
143+
easier. :py:func:`attr.ib` and :py:class:`attr.Factory` actually return objects, but the
144144
annotation says these return the types that they expect to be assigned to.
145145
That enables this to work:
146146

@@ -175,7 +175,7 @@ Caveats/Known Issues
175175
176176
* Currently, ``converter`` only supports named functions. If mypy finds something else it
177177
will complain about not understanding the argument and the type annotation in
178-
``__init__`` will be replaced by ``Any``.
178+
:py:meth:`__init__ <object.__init__>` will be replaced by ``Any``.
179179

180180
* :ref:`Validator decorators <attrs:examples_validators>`
181181
and `default decorators <http://www.attrs.org/en/stable/examples.html#defaults>`_
@@ -348,16 +348,16 @@ Extended Callable types
348348
This feature is deprecated. You can use
349349
:ref:`callback protocols <callback_protocols>` as a replacement.
350350

351-
As an experimental mypy extension, you can specify ``Callable`` types
351+
As an experimental mypy extension, you can specify :py:data:`~typing.Callable` types
352352
that support keyword arguments, optional arguments, and more. When
353-
you specify the arguments of a Callable, you can choose to supply just
353+
you specify the arguments of a :py:data:`~typing.Callable`, you can choose to supply just
354354
the type of a nameless positional argument, or an "argument specifier"
355355
representing a more complicated form of argument. This allows one to
356356
more closely emulate the full range of possibilities given by the
357357
``def`` statement in Python.
358358

359359
As an example, here's a complicated function definition and the
360-
corresponding ``Callable``:
360+
corresponding :py:data:`~typing.Callable`:
361361

362362
.. code-block:: python
363363
@@ -434,7 +434,7 @@ purpose:
434434
In all cases, the ``type`` argument defaults to ``Any``, and if the
435435
``name`` argument is omitted the argument has no name (the name is
436436
required for ``NamedArg`` and ``DefaultNamedArg``). A basic
437-
``Callable`` such as
437+
:py:data:`~typing.Callable` such as
438438

439439
.. code-block:: python
440440
@@ -446,7 +446,7 @@ is equivalent to the following:
446446
447447
MyFunc = Callable[[Arg(int), Arg(str), Arg(int)], float]
448448
449-
A ``Callable`` with unspecified argument types, such as
449+
A :py:data:`~typing.Callable` with unspecified argument types, such as
450450

451451
.. code-block:: python
452452

docs/source/casts.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Casts and type assertions
66
Mypy supports type casts that are usually used to coerce a statically
77
typed value to a subtype. Unlike languages such as Java or C#,
88
however, mypy casts are only used as hints for the type checker, and they
9-
don't perform a runtime type check. Use the function ``cast`` to perform a
9+
don't perform a runtime type check. Use the function :py:func:`~typing.cast` to perform a
1010
cast:
1111

1212
.. code-block:: python

docs/source/class_basics.rst

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ initialized within the class. Mypy infers the types of attributes:
2424
a.y = 3 # Error: 'A' has no attribute 'y'
2525
2626
This is a bit like each class having an implicitly defined
27-
``__slots__`` attribute. This is only enforced during type
27+
:py:data:`__slots__ <object.__slots__>` attribute. This is only enforced during type
2828
checking and not when your program is running.
2929

3030
You can declare types of variables in the class body explicitly using
@@ -40,7 +40,7 @@ a type annotation:
4040
4141
As in Python generally, a variable defined in the class body can be used
4242
as a class or an instance variable. (As discussed in the next section, you
43-
can override this with a ``ClassVar`` annotation.)
43+
can override this with a :py:data:`~typing.ClassVar` annotation.)
4444

4545
Type comments work as well, if you need to support Python versions earlier
4646
than 3.6:
@@ -81,11 +81,11 @@ to it explicitly using ``self``:
8181
Annotating ``__init__`` methods
8282
*******************************
8383

84-
The ``__init__`` method is somewhat special -- it doesn't return a
84+
The :py:meth:`__init__ <object.__init__>` method is somewhat special -- it doesn't return a
8585
value. This is best expressed as ``-> None``. However, since many feel
8686
this is redundant, it is allowed to omit the return type declaration
87-
on ``__init__`` methods **if at least one argument is annotated**. For
88-
example, in the following classes ``__init__`` is considered fully
87+
on :py:meth:`__init__ <object.__init__>` methods **if at least one argument is annotated**. For
88+
example, in the following classes :py:meth:`__init__ <object.__init__>` is considered fully
8989
annotated:
9090

9191
.. code-block:: python
@@ -98,7 +98,7 @@ annotated:
9898
def __init__(self, arg: int):
9999
self.var = arg
100100
101-
However, if ``__init__`` has no annotated arguments and no return type
101+
However, if :py:meth:`__init__ <object.__init__>` has no annotated arguments and no return type
102102
annotation, it is considered an untyped method:
103103

104104
.. code-block:: python
@@ -111,7 +111,7 @@ annotation, it is considered an untyped method:
111111
Class attribute annotations
112112
***************************
113113

114-
You can use a ``ClassVar[t]`` annotation to explicitly declare that a
114+
You can use a :py:data:`ClassVar[t] <typing.ClassVar>` annotation to explicitly declare that a
115115
particular attribute should not be set on instances:
116116

117117
.. code-block:: python
@@ -134,7 +134,7 @@ particular attribute should not be set on instances:
134134
PyPI). If you use Python 2.7, you can import it from ``typing``.
135135

136136
It's not necessary to annotate all class variables using
137-
``ClassVar``. An attribute without the ``ClassVar`` annotation can
137+
:py:data:`~typing.ClassVar`. An attribute without the :py:data:`~typing.ClassVar` annotation can
138138
still be used as a class variable. However, mypy won't prevent it from
139139
being used as an instance variable, as discussed previously:
140140

@@ -148,13 +148,13 @@ being used as an instance variable, as discussed previously:
148148
a = A()
149149
a.x = 1 # Also OK
150150
151-
Note that ``ClassVar`` is not a class, and you can't use it with
152-
``isinstance()`` or ``issubclass()``. It does not change Python
151+
Note that :py:data:`~typing.ClassVar` is not a class, and you can't use it with
152+
:py:func:`isinstance` or :py:func:`issubclass`. It does not change Python
153153
runtime behavior -- it's only for type checkers such as mypy (and
154154
also helpful for human readers).
155155

156156
You can also omit the square brackets and the variable type in
157-
a ``ClassVar`` annotation, but this might not do what you'd expect:
157+
a :py:data:`~typing.ClassVar` annotation, but this might not do what you'd expect:
158158

159159
.. code-block:: python
160160
@@ -165,7 +165,7 @@ In this case the type of the attribute will be implicitly ``Any``.
165165
This behavior will change in the future, since it's surprising.
166166

167167
.. note::
168-
A ``ClassVar`` type parameter cannot include type variables:
168+
A :py:data:`~typing.ClassVar` type parameter cannot include type variables:
169169
``ClassVar[T]`` and ``ClassVar[List[T]]``
170170
are both invalid if ``T`` is a type variable (see :ref:`generic-classes`
171171
for more about type variables).
@@ -232,10 +232,10 @@ effect at runtime:
232232
Abstract base classes and multiple inheritance
233233
**********************************************
234234

235-
Mypy supports Python abstract base classes (ABCs). Abstract classes
235+
Mypy supports Python :doc:`abstract base classes <library/abc>` (ABCs). Abstract classes
236236
have at least one abstract method or property that must be implemented
237237
by any *concrete* (non-abstract) subclass. You can define abstract base
238-
classes using the ``abc.ABCMeta`` metaclass and the ``abc.abstractmethod``
238+
classes using the :py:class:`abc.ABCMeta` metaclass and the :py:func:`@abc.abstractmethod <abc.abstractmethod>`
239239
function decorator. Example:
240240

241241
.. code-block:: python
@@ -263,11 +263,11 @@ function decorator. Example:
263263
264264
.. note::
265265

266-
In Python 2.7 you have to use ``@abc.abstractproperty`` to define
266+
In Python 2.7 you have to use :py:func:`@abc.abstractproperty <abc.abstractproperty>` to define
267267
an abstract property.
268268

269269
Note that mypy performs checking for unimplemented abstract methods
270-
even if you omit the ``ABCMeta`` metaclass. This can be useful if the
270+
even if you omit the :py:class:`~abc.ABCMeta` metaclass. This can be useful if the
271271
metaclass would cause runtime metaclass conflicts.
272272

273273
Since you can't create instances of ABCs, they are most commonly used in

docs/source/command_line.rst

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,17 @@ imports.
129129
``--python-executable EXECUTABLE``
130130
This flag will have mypy collect type information from :pep:`561`
131131
compliant packages installed for the Python executable ``EXECUTABLE``.
132-
If not provided, mypy will use :pep:`561` compliant packages installed for
132+
If not provided, mypy will use PEP 561 compliant packages installed for
133133
the Python executable running mypy.
134134

135-
See :ref:`installed-packages` for more on making :pep:`561` compliant packages.
135+
See :ref:`installed-packages` for more on making PEP 561 compliant packages.
136136

137137
``--no-site-packages``
138138
This flag will disable searching for :pep:`561` compliant packages. This
139139
will also disable searching for a usable Python executable.
140140

141141
Use this flag if mypy cannot find a Python executable for the version of
142-
Python being checked, and you don't need to use :pep:`561` typed packages.
142+
Python being checked, and you don't need to use PEP 561 typed packages.
143143
Otherwise, use ``--python-executable``.
144144

145145
``--no-silence-site-packages``
@@ -213,7 +213,7 @@ The following options are available:
213213
This flag disallows all expressions in the module that have type ``Any``.
214214
If an expression of type ``Any`` appears anywhere in the module
215215
mypy will output an error unless the expression is immediately
216-
used as an argument to ``cast`` or assigned to a variable with an
216+
used as an argument to :py:func:`~typing.cast` or assigned to a variable with an
217217
explicit type annotation.
218218

219219
In addition, declaring a variable of type ``Any``
@@ -230,9 +230,9 @@ The following options are available:
230230

231231
``--disallow-any-generics``
232232
This flag disallows usage of generic types that do not specify explicit
233-
type parameters. Moreover, built-in collections (such as ``list`` and
234-
``dict``) become disallowed as you should use their aliases from the typing
235-
module (such as ``List[int]`` and ``Dict[str, str]``).
233+
type parameters. Moreover, built-in collections (such as :py:class:`list` and
234+
:py:class:`dict`) become disallowed as you should use their aliases from the :py:mod:`typing`
235+
module (such as :py:class:`List[int] <typing.List>` and :py:class:`Dict[str, str] <typing.Dict>`).
236236

237237
``--disallow-subclassing-any``
238238
This flag reports an error whenever a class subclasses a value of
@@ -292,7 +292,7 @@ For more details, see :ref:`no_strict_optional`.
292292

293293
``--no-implicit-optional``
294294
This flag causes mypy to stop treating arguments with a ``None``
295-
default value as having an implicit ``Optional[...]`` type.
295+
default value as having an implicit :py:data:`~typing.Optional` type.
296296

297297
For example, by default mypy will assume that the ``x`` parameter
298298
is of type ``Optional[int]`` in the code snippet below since
@@ -312,7 +312,7 @@ For more details, see :ref:`no_strict_optional`.
312312
print(x)
313313
314314
``--no-strict-optional``
315-
This flag disables strict checking of ``Optional[...]``
315+
This flag disables strict checking of :py:data:`~typing.Optional`
316316
types and ``None`` values. With this option, mypy doesn't
317317
generally check the use of ``None`` values -- they are valid
318318
everywhere. See :ref:`no_strict_optional` for more about this feature.
@@ -362,7 +362,7 @@ potentially problematic or redundant in some way.
362362

363363
``--warn-return-any``
364364
This flag causes mypy to generate a warning when returning a value
365-
with type ``Any`` from a function declared with a non- ``Any`` return type.
365+
with type ``Any`` from a function declared with a non-``Any`` return type.
366366

367367
``--warn-unreachable``
368368
This flag will make mypy report an error whenever it encounters
@@ -387,7 +387,7 @@ potentially problematic or redundant in some way.
387387
unreachable" warning will be silenced in exactly two cases:
388388

389389
1. When the unreachable statement is a ``raise`` statement, is an
390-
``assert False`` statement, or calls a function that has the ``NoReturn``
390+
``assert False`` statement, or calls a function that has the :py:data:`~typing.NoReturn`
391391
return type hint. In other words, when the unreachable statement
392392
throws an error or terminates the program in some way.
393393
2. When the unreachable statement was *intentionally* marked as unreachable
@@ -578,7 +578,7 @@ in developing or debugging mypy internals.
578578

579579
``--custom-typing MODULE``
580580
This flag lets you use a custom module as a substitute for the
581-
``typing`` module.
581+
:py:mod:`typing` module.
582582

583583
``--custom-typeshed-dir DIR``
584584
This flag specifies the directory where mypy looks for typeshed
@@ -673,10 +673,10 @@ Miscellaneous
673673
This flag will give command line arguments that appear to be
674674
scripts (i.e. files whose name does not end in ``.py``)
675675
a module name derived from the script name rather than the fixed
676-
name ``__main__``.
676+
name :py:mod:`__main__`.
677677

678678
This lets you check more than one script in a single mypy invocation.
679-
(The default ``__main__`` is technically more correct, but if you
679+
(The default :py:mod:`__main__` is technically more correct, but if you
680680
have many scripts that import a large package, the behavior enabled
681681
by this flag is often more convenient.)
682682

0 commit comments

Comments
 (0)