Skip to content

Commit 742d33a

Browse files
hoeflinggvanrossum
authored andcommitted
Crossreferences to standard library in mypy docs, part 3 (#7677)
Signed-off-by: Oleg Höfling <[email protected]>
1 parent 9614518 commit 742d33a

File tree

7 files changed

+45
-47
lines changed

7 files changed

+45
-47
lines changed

docs/source/class_basics.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ to it explicitly using ``self``:
7878
a = self
7979
a.x = 1 # Error: 'x' not defined
8080
81-
Annotating ``__init__`` methods
82-
*******************************
81+
Annotating __init__ methods
82+
***************************
8383

8484
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

docs/source/error_code_list.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ definition in an active scope, such as an assignment, function
8383
definition or an import. This can catch missing definitions, missing
8484
imports, and typos.
8585

86-
This example accidentally calls ``sort()`` instead of ``sorted()``:
86+
This example accidentally calls ``sort()`` instead of :py:func:`sorted`:
8787

8888
.. code-block:: python
8989
@@ -181,7 +181,7 @@ This example incorrectly uses the function ``log`` as a type:
181181
for x in objs:
182182
f(x)
183183
184-
You can use ``Callable`` as the type for callable objects:
184+
You can use :py:data:`~typing.Callable` as the type for callable objects:
185185

186186
.. code-block:: python
187187
@@ -418,8 +418,8 @@ Example:
418418
Check TypedDict items [typeddict-item]
419419
--------------------------------------
420420

421-
When constructing a TypedDict object, mypy checks that each key and value is compatible
422-
with the TypedDict type that is inferred from the surrounding context.
421+
When constructing a ``TypedDict`` object, mypy checks that each key and value is compatible
422+
with the ``TypedDict`` type that is inferred from the surrounding context.
423423

424424
Example:
425425

@@ -542,8 +542,7 @@ Check instantiation of abstract classes [abstract]
542542

543543
Mypy generates an error if you try to instantiate an abstract base
544544
class (ABC). An abtract base class is a class with at least one
545-
abstract method or attribute. (See also :doc:`Python
546-
abc module documentation <python:library/abc>`)
545+
abstract method or attribute. (See also :py:mod:`abc` module documentation)
547546

548547
Sometimes a class is made accidentally abstract, often due to an
549548
unimplemented abstract method. In a case like this you need to provide
@@ -572,7 +571,7 @@ Example:
572571
Check the target of NewType [valid-newtype]
573572
-------------------------------------------
574573

575-
The target of a ``NewType`` definition must be a class type. It can't
574+
The target of a :py:func:`NewType <typing.NewType>` definition must be a class type. It can't
576575
be a union type, ``Any``, or various other special types.
577576

578577
You can also get this error if the target has been imported from a
@@ -596,10 +595,10 @@ for more information.
596595
Check the return type of __exit__ [exit-return]
597596
-----------------------------------------------
598597

599-
If mypy can determine that ``__exit__`` always returns ``False``, mypy
598+
If mypy can determine that :py:meth:`__exit__ <object.__exit__>` always returns ``False``, mypy
600599
checks that the return type is *not* ``bool``. The boolean value of
601600
the return type affects which lines mypy thinks are reachable after a
602-
``with`` statement, since any ``__exit__`` method that can return
601+
``with`` statement, since any :py:meth:`__exit__ <object.__exit__>` method that can return
603602
``True`` may swallow exceptions. An imprecise return type can result
604603
in mysterious errors reported near ``with`` statements.
605604

docs/source/extending_mypy.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ what normally would have been the command line arguments to mypy.
1414

1515
Function ``run`` returns a ``Tuple[str, str, int]``, namely
1616
``(<normal_report>, <error_report>, <exit_status>)``, in which ``<normal_report>``
17-
is what mypy normally writes to ``sys.stdout``, ``<error_report>`` is what mypy
18-
normally writes to ``sys.stderr`` and ``exit_status`` is the exit status mypy normally
17+
is what mypy normally writes to :py:data:`sys.stdout`, ``<error_report>`` is what mypy
18+
normally writes to :py:data:`sys.stderr` and ``exit_status`` is the exit status mypy normally
1919
returns to the operating system.
2020

2121
A trivial example of using the api is the following
@@ -98,7 +98,7 @@ High-level overview
9898
*******************
9999

100100
Every entry point function should accept a single string argument
101-
that is a full mypy version and return a subclass of ``mypy.plugins.Plugin``:
101+
that is a full mypy version and return a subclass of ``mypy.plugin.Plugin``:
102102

103103
.. code-block:: python
104104
@@ -174,7 +174,7 @@ For example:
174174
instead of module level functions.
175175

176176
**get_method_signature_hook()** is used to adjust the signature of a method.
177-
This includes special Python methods except ``__init__()`` and ``__new__()``.
177+
This includes special Python methods except :py:meth:`~object.__init__` and :py:meth:`~object.__new__`.
178178
For example in this code:
179179

180180
.. code-block:: python
@@ -185,12 +185,12 @@ For example in this code:
185185
x[0] = 42
186186
187187
mypy will call ``get_method_signature_hook("ctypes.Array.__setitem__")``
188-
so that the plugin can mimic the ``ctypes`` auto-convert behavior.
188+
so that the plugin can mimic the :py:mod:`ctypes` auto-convert behavior.
189189

190-
**get_attribute_hook** overrides instance member field lookups and property
190+
**get_attribute_hook()** overrides instance member field lookups and property
191191
access (not assignments, and not method calls). This hook is only called for
192-
fields which already exist on the class. *Exception:* if ``__getattr__`` or
193-
``__getattribute__`` is a method on the class, the hook is called for all
192+
fields which already exist on the class. *Exception:* if :py:meth:`__getattr__ <object.__getattr__>` or
193+
:py:meth:`__getattribute__ <object.__getattribute__>` is a method on the class, the hook is called for all
194194
fields which do not refer to methods.
195195

196196
**get_class_decorator_hook()** can be used to update class definition for

docs/source/faq.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,16 @@ Structural subtyping can be thought of as "static duck typing".
110110
Some argue that structural subtyping is better suited for languages with duck
111111
typing such as Python. Mypy however primarily uses nominal subtyping,
112112
leaving structural subtyping mostly opt-in (except for built-in protocols
113-
such as ``Iterable`` that always support structural subtyping). Here are some
113+
such as :py:class:`~typing.Iterable` that always support structural subtyping). Here are some
114114
reasons why:
115115

116116
1. It is easy to generate short and informative error messages when
117117
using a nominal type system. This is especially important when
118118
using type inference.
119119

120-
2. Python provides built-in support for nominal ``isinstance()`` tests and
120+
2. Python provides built-in support for nominal :py:func:`isinstance` tests and
121121
they are widely used in programs. Only limited support for structural
122-
``isinstance()`` is available, and it's less type safe than
123-
nominal type tests.
122+
:py:func:`isinstance` is available, and it's less type safe than nominal type tests.
124123

125124
3. Many programmers are already familiar with static, nominal subtyping and it
126125
has been successfully used in languages such as Java, C++ and
@@ -164,7 +163,7 @@ monkey patching of methods.
164163
How is mypy different from Cython?
165164
**********************************
166165

167-
`Cython :doc:<cython:index>` is a variant of Python that supports
166+
:doc:`Cython <cython:index>` is a variant of Python that supports
168167
compilation to CPython C modules. It can give major speedups to
169168
certain classes of programs compared to CPython, and it provides
170169
static typing (though this is different from mypy). Mypy differs in

docs/source/final_attrs.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ from being overridden in a subclass:
5454
class ListView(Window):
5555
BORDER_WIDTH = 3 # Error: can't override a final attribute
5656
57-
You can use ``@property`` to make an attribute read-only, but unlike ``Final``,
57+
You can use :py:class:`@property <property>` to make an attribute read-only, but unlike ``Final``,
5858
it doesn't work with module attributes, and it doesn't prevent overriding in
5959
subclasses.
6060

@@ -83,11 +83,11 @@ You can use ``Final`` in one of these forms:
8383

8484
* Finally, you can write ``self.id: Final = 1`` (also optionally with
8585
a type in square brackets). This is allowed *only* in
86-
``__init__`` methods, so that the final instance attribute is
86+
:py:meth:`__init__ <object.__init__>` methods, so that the final instance attribute is
8787
assigned only once when an instance is created.
8888

89-
Details of using Final
90-
**********************
89+
Details of using ``Final``
90+
**************************
9191

9292
These are the two main rules for defining a final name:
9393

@@ -98,7 +98,7 @@ These are the two main rules for defining a final name:
9898
* There must be *exactly one* assignment to a final name.
9999

100100
A final attribute declared in a class body without an initializer must
101-
be initialized in the ``__init__`` method (you can skip the
101+
be initialized in the :py:meth:`__init__ <object.__init__>` method (you can skip the
102102
initializer in stub files):
103103

104104
.. code-block:: python
@@ -121,9 +121,9 @@ annotations. Using it in any other position is an error. In particular,
121121
def fun(x: Final[List[int]]) -> None: # Error!
122122
...
123123
124-
``Final`` and ``ClassVar`` should not be used together. Mypy will infer
124+
``Final`` and :py:data:`~typing.ClassVar` should not be used together. Mypy will infer
125125
the scope of a final declaration automatically depending on whether it was
126-
initialized in the class body or in ``__init__``.
126+
initialized in the class body or in :py:meth:`__init__ <object.__init__>`.
127127

128128
A final attribute can't be overridden by a subclass (even with another
129129
explicit final declaration). Note however that a final attribute can

docs/source/getting_started.rst

Lines changed: 14 additions & 14 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.
@@ -167,9 +167,9 @@ 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

docs/source/protocols.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ such as trees and linked lists:
376376
377377
root: TreeLike = SimpleTree(0) # OK
378378
379-
Using ``isinstance()`` with protocols
380-
*************************************
379+
Using isinstance() with protocols
380+
*********************************
381381
382382
You can use a protocol class with ``isinstance()`` if you decorate it
383383
with the ``@runtime_checkable`` class decorator. The decorator adds

0 commit comments

Comments
 (0)