Skip to content

Commit c09c276

Browse files
committed
added crossrefs to standard library
Signed-off-by: Oleg Höfling <[email protected]>
1 parent 47dab6a commit c09c276

28 files changed

+533
-498
lines changed

docs/source/additional_features.rst

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ 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
30-
define dataclasses. For example:
29+
Mypy will detect special methods (such as :py:meth:`__lt__ <object.__lt__>`)
30+
depending on the flags used to define dataclasses. For example:
3131

3232
.. code-block:: python
3333
@@ -72,10 +72,11 @@ 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
76+
:py:func:`~dataclasses.replace` and :py:func:`~dataclasses.asdict`,
7677
have imprecise (too permissive) types. This will be fixed in future releases.
7778

78-
Mypy does not yet recognize aliases of ``dataclasses.dataclass``, and will
79+
Mypy does not yet recognize aliases of :py:func:`dataclasses.dataclass`, and will
7980
probably never recognize dynamically computed decorators. The following examples
8081
do **not** work:
8182

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

147148
.. code-block:: python
148149
@@ -175,7 +176,7 @@ Caveats/Known Issues
175176
176177
* Currently, ``converter`` only supports named functions. If mypy finds something else it
177178
will complain about not understanding the argument and the type annotation in
178-
``__init__`` will be replaced by ``Any``.
179+
``__init__`` will be replaced by :py:data:`~typing.Any`.
179180

180181
* :ref:`Validator decorators <attrs:examples_validators>`
181182
and `default decorators <http://www.attrs.org/en/stable/examples.html#defaults>`_
@@ -249,7 +250,7 @@ Your CI script might work like this:
249250
* Create a tarball from the ``.mypy_cache`` directory.
250251

251252
* Determine the current git master branch commit id (say, using
252-
``git rev-parse HEAD``).
253+
:code:`git rev-parse HEAD`).
253254

254255
* Upload the tarball to the shared repository with a name derived from the
255256
commit id.
@@ -348,16 +349,17 @@ Extended Callable types
348349
This feature is deprecated. You can use
349350
:ref:`callback protocols <callback_protocols>` as a replacement.
350351

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

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

362364
.. code-block:: python
363365
@@ -431,10 +433,10 @@ purpose:
431433
# A single KwArg() specifier represents all remaining
432434
# keyword arguments.
433435
434-
In all cases, the ``type`` argument defaults to ``Any``, and if the
435-
``name`` argument is omitted the argument has no name (the name is
436-
required for ``NamedArg`` and ``DefaultNamedArg``). A basic
437-
``Callable`` such as
436+
In all cases, the ``type`` argument defaults to :py:data:`~typing.Any`,
437+
and if the ``name`` argument is omitted the argument has no name
438+
(the name is required for ``NamedArg`` and ``DefaultNamedArg``).
439+
A basic :py:data:`~typing.Callable` such as
438440

439441
.. code-block:: python
440442
@@ -446,7 +448,7 @@ is equivalent to the following:
446448
447449
MyFunc = Callable[[Arg(int), Arg(str), Arg(int)], float]
448450
449-
A ``Callable`` with unspecified argument types, such as
451+
A :py:data:`~typing.Callable` with unspecified argument types, such as
450452

451453
.. code-block:: python
452454

docs/source/builtin_types.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,22 @@ Type Description
2222
``Any`` dynamically typed value with an arbitrary type
2323
====================== ===============================
2424

25-
The type ``Any`` and type constructors such as ``List``, ``Dict``,
26-
``Iterable`` and ``Sequence`` are defined in the ``typing`` module.
25+
The type :py:data:`~typing.Any` and type constructors such as :py:class:`~typing.List`,
26+
:py:class:`~typing.Dict`, :py:class:`~typing.Iterable` and :py:class:`~typing.Sequence`
27+
are defined in the :py:mod:`typing` module.
2728

28-
The type ``Dict`` is a *generic* class, signified by type arguments within
29+
The type :py:class:`~typing.Dict` is a *generic* class, signified by type arguments within
2930
``[...]``. For example, ``Dict[int, str]`` is a dictionary from integers to
3031
strings and ``Dict[Any, Any]`` is a dictionary of dynamically typed
31-
(arbitrary) values and keys. ``List`` is another generic class. ``Dict`` and
32-
``List`` are aliases for the built-ins ``dict`` and ``list``, respectively.
32+
(arbitrary) values and keys. :py:class:`~typing.List` is another generic class.
33+
:py:class:`~typing.Dict` and :py:class:`~typing.List` are aliases for the
34+
built-ins :py:class:`dict` and :py:class:`list`, respectively.
3335

34-
``Iterable``, ``Sequence``, and ``Mapping`` are generic types that
36+
:py:class:`~typing.Iterable`, :py:class:`~typing.Sequence`, and
37+
:py:class:`~typing.Mapping` are generic types that
3538
correspond to Python protocols. For example, a ``str`` object or a
3639
``List[str]`` object is valid
3740
when ``Iterable[str]`` or ``Sequence[str]`` is expected. Note that even though
38-
they are similar to abstract base classes defined in ``collections.abc``
41+
they are similar to abstract base classes defined in :py:mod:`collections.abc`
3942
(formerly ``collections``), they are not identical, since the built-in
4043
collection type objects do not support indexing.

docs/source/casts.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ 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
10-
cast:
9+
don't perform a runtime type check. Use the function :py:func:`~typing.cast`
10+
to perform a cast:
1111

1212
.. code-block:: python
1313
@@ -34,10 +34,11 @@ quite understand what is going on.
3434
assert isinstance(o, int)
3535
print(o + 5) # OK: type of 'o' is 'int' here
3636
37-
You don't need a cast for expressions with type ``Any``, or when
38-
assigning to a variable with type ``Any``, as was explained earlier.
39-
You can also use ``Any`` as the cast target type -- this lets you perform
40-
any operations on the result. For example:
37+
You don't need a cast for expressions with type :py:data:`~typing.Any`,
38+
or when assigning to a variable with type :py:data:`~typing.Any`,
39+
as was explained earlier.
40+
You can also use :py:data:`~typing.Any` as the cast target type --
41+
this lets you perform any operations on the result. For example:
4142

4243
.. code-block:: python
4344

docs/source/class_basics.rst

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
Class basics
22
============
33

4+
45
This section will help get you started annotating your
5-
classes. Built-in classes such as ``int`` also follow these same
6-
rules.
6+
classes. Built-in classes such as :py:class:`int` also
7+
follow these same rules.
78

89
Instance and class attributes
910
*****************************
@@ -24,8 +25,8 @@ initialized within the class. Mypy infers the types of attributes:
2425
a.y = 3 # Error: 'A' has no attribute 'y'
2526
2627
This is a bit like each class having an implicitly defined
27-
``__slots__`` attribute. This is only enforced during type
28-
checking and not when your program is running.
28+
:py:data:`__slots__ <object.__slots__>` attribute. This is only
29+
enforced during type checking and not when your program is running.
2930

3031
You can declare types of variables in the class body explicitly using
3132
a type annotation:
@@ -40,7 +41,7 @@ a type annotation:
4041
4142
As in Python generally, a variable defined in the class body can be used
4243
as a class or an instance variable. (As discussed in the next section, you
43-
can override this with a ``ClassVar`` annotation.)
44+
can override this with a :py:data:`~typing.ClassVar` annotation.)
4445

4546
Type comments work as well, if you need to support Python versions earlier
4647
than 3.6:
@@ -51,9 +52,9 @@ than 3.6:
5152
x = None # type: List[int] # Declare attribute 'x' of type List[int]
5253
5354
Note that attribute definitions in the class body that use a type comment
54-
are special: a ``None`` value is valid as the initializer, even though
55+
are special: a :py:data:`None` value is valid as the initializer, even though
5556
the declared type is not optional. This should be used sparingly, as this can
56-
result in ``None``-related runtime errors that mypy can't detect.
57+
result in :py:data:`None`-related runtime errors that mypy can't detect.
5758

5859
Similarly, you can give explicit types to instance variables defined
5960
in a method:
@@ -78,15 +79,16 @@ to it explicitly using ``self``:
7879
a = self
7980
a.x = 1 # Error: 'x' not defined
8081
81-
Annotating `__init__` methods
82-
*****************************
82+
Annotating ``__init__`` methods
83+
*******************************
8384

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

9193
.. code-block:: python
9294
@@ -98,8 +100,8 @@ annotated:
98100
def __init__(self, arg: int):
99101
self.var = arg
100102
101-
However, if ``__init__`` has no annotated arguments and no return type
102-
annotation, it is considered an untyped method:
103+
However, if :py:meth:`__init__ <object.__init__>` has no annotated
104+
arguments and no return type annotation, it is considered an untyped method:
103105

104106
.. code-block:: python
105107
@@ -130,12 +132,14 @@ particular attribute should not be set on instances:
130132
.. note::
131133

132134
If you need to support Python 3 versions 3.5.2 or earlier, you have
133-
to import ``ClassVar`` from ``typing_extensions`` instead (available on
134-
PyPI). If you use Python 2.7, you can import it from ``typing``.
135+
to import :py:data:`~typing.ClassVar` from ``typing_extensions``
136+
instead (available on PyPI). If you use Python 2.7, you can import it
137+
from :py:mod:`typing`.
135138

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

141145
.. code-block:: python
@@ -148,27 +152,29 @@ being used as an instance variable, as discussed previously:
148152
a = A()
149153
a.x = 1 # Also OK
150154
151-
Note that ``ClassVar`` is not a class, and you can't use it with
152-
``isinstance()`` or ``issubclass()``. It does not change Python
153-
runtime behavior -- it's only for type checkers such as mypy (and
154-
also helpful for human readers).
155+
Note that :py:data:`~typing.ClassVar` is not a class, and
156+
you can't use it with :py:func:`isinstance` or :py:func:`issubclass`.
157+
It does not change Python runtime behavior -- it's only for
158+
type checkers such as mypy (and also helpful for human readers).
155159

156160
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:
161+
a :py:data:`~typing.ClassVar` annotation, but this might not do
162+
what you'd expect:
158163

159164
.. code-block:: python
160165
161166
class A:
162167
y: ClassVar = 0 # Type implicitly Any!
163168
164-
In this case the type of the attribute will be implicitly ``Any``.
169+
In this case the type of the attribute will be implicitly
170+
:py:data:`~typing.Any`.
165171
This behavior will change in the future, since it's surprising.
166172

167173
.. note::
168-
A ``ClassVar`` type parameter cannot include type variables:
169-
``ClassVar[T]`` and ``ClassVar[List[T]]``
170-
are both invalid if ``T`` is a type variable (see :ref:`generic-classes`
171-
for more about type variables).
174+
A :py:data:`~typing.ClassVar` type parameter cannot include
175+
type variables: ``ClassVar[T]`` and ``ClassVar[List[T]]``
176+
are both invalid if ``T`` is a type variable (see
177+
:ref:`generic-classes` for more about type variables).
172178

173179
Overriding statically typed methods
174180
***********************************
@@ -235,8 +241,9 @@ Abstract base classes and multiple inheritance
235241
Mypy supports Python abstract base classes (ABCs). Abstract classes
236242
have at least one abstract method or property that must be implemented
237243
by any *concrete* (non-abstract) subclass. You can define abstract base
238-
classes using the ``abc.ABCMeta`` metaclass and the ``abc.abstractmethod``
239-
function decorator. Example:
244+
classes using the :py:class:`abc.ABCMeta` metaclass and the
245+
:py:func:`@abc.abstractmethod <abc.abstractmethod>` function decorator.
246+
Example:
240247

241248
.. code-block:: python
242249
@@ -263,12 +270,12 @@ function decorator. Example:
263270
264271
.. note::
265272

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

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

273280
Since you can't create instances of ABCs, they are most commonly used in
274281
type annotations. For example, this method accepts arbitrary iterables

0 commit comments

Comments
 (0)