Skip to content

Commit 4618e9e

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

31 files changed

+624
-578
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+
:py:meth:`__init__ <object.__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,13 +349,14 @@ 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
354-
the type of a nameless positional argument, or an "argument specifier"
355-
representing a more complicated form of argument. This allows one to
356-
more closely emulate the full range of possibilities given by the
357-
``def`` statement in Python.
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+
``Callable``, you can choose to supply just the type of a nameless
356+
positional argument, or an "argument specifier" representing a
357+
more complicated form of argument. This allows one to more closely
358+
emulate the full range of possibilities given by the ``def`` statement
359+
in Python.
358360

359361
As an example, here's a complicated function definition and the
360362
corresponding ``Callable``:
@@ -434,7 +436,7 @@ purpose:
434436
In all cases, the ``type`` argument defaults to ``Any``, and if the
435437
``name`` argument is omitted the argument has no name (the name is
436438
required for ``NamedArg`` and ``DefaultNamedArg``). A basic
437-
``Callable`` such as
439+
: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: 13 additions & 10 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

2829
The type ``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
3132
(arbitrary) values and keys. ``List`` is another generic class. ``Dict`` and
32-
``List`` are aliases for the built-ins ``dict`` and ``list``, respectively.
33+
``List`` are aliases for the built-ins :py:class:`dict` and :py:class:`list`,
34+
respectively.
3335

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

docs/source/casts.rst

Lines changed: 4 additions & 4 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,8 +34,8 @@ 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.
37+
You don't need a cast for expressions with type :py:data:`~typing.Any`,
38+
or when assigning to a variable with type ``Any``, as was explained earlier.
3939
You can also use ``Any`` as the cast target type -- this lets you perform
4040
any operations on the result. For example:
4141

docs/source/class_basics.rst

Lines changed: 39 additions & 35 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:
@@ -78,15 +79,15 @@ 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 ``__init__`` methods **if at least
89+
one argument is annotated**. For example, in the following classes
90+
``__init__`` is considered fully annotated:
9091

9192
.. code-block:: python
9293
@@ -111,8 +112,9 @@ annotation, it is considered an untyped method:
111112
Class attribute annotations
112113
***************************
113114

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

117119
.. code-block:: python
118120
@@ -130,13 +132,13 @@ 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 ``ClassVar`` from ``typing_extensions`` instead (available
136+
on PyPI). If you use Python 2.7, you can import it from ``typing``.
135137

136-
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
139-
being used as an instance variable, as discussed previously:
138+
It's not necessary to annotate all class variables using ``ClassVar``.
139+
An attribute without the ``ClassVar`` annotation can still be used
140+
as a class variable. However, mypy won't prevent it from being used
141+
as an instance variable, as discussed previously:
140142

141143
.. code-block:: python
142144
@@ -149,9 +151,9 @@ being used as an instance variable, as discussed previously:
149151
a.x = 1 # Also OK
150152
151153
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).
154+
:py:func:`isinstance` or :py:func:`issubclass`.
155+
It does not change Python runtime behavior -- it's only for
156+
type checkers such as mypy (and also helpful for human readers).
155157

156158
You can also omit the square brackets and the variable type in
157159
a ``ClassVar`` annotation, but this might not do what you'd expect:
@@ -161,14 +163,15 @@ a ``ClassVar`` annotation, but this might not do what you'd expect:
161163
class A:
162164
y: ClassVar = 0 # Type implicitly Any!
163165
164-
In this case the type of the attribute will be implicitly ``Any``.
166+
In this case the type of the attribute will be implicitly
167+
:py:data:`~typing.Any`.
165168
This behavior will change in the future, since it's surprising.
166169

167170
.. 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).
171+
A :py:data:`~typing.ClassVar` type parameter cannot include
172+
type variables: ``ClassVar[T]`` and ``ClassVar[List[T]]``
173+
are both invalid if ``T`` is a type variable (see
174+
:ref:`generic-classes` for more about type variables).
172175

173176
Overriding statically typed methods
174177
***********************************
@@ -235,8 +238,9 @@ Abstract base classes and multiple inheritance
235238
Mypy supports Python abstract base classes (ABCs). Abstract classes
236239
have at least one abstract method or property that must be implemented
237240
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:
241+
classes using the :py:class:`abc.ABCMeta` metaclass and the
242+
:py:func:`@abc.abstractmethod <abc.abstractmethod>` function decorator.
243+
Example:
240244

241245
.. code-block:: python
242246
@@ -263,12 +267,12 @@ function decorator. Example:
263267
264268
.. note::
265269

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

269273
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.
274+
even if you omit the :py:class:`~abc.ABCMeta` metaclass. This can be
275+
useful if the metaclass would cause runtime metaclass conflicts.
272276

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

0 commit comments

Comments
 (0)