Skip to content

Commit 0e41cce

Browse files
hoeflingGuido van Rossum
authored andcommitted
Crossreferences to standard library in mypy docs, part 5 (#7689)
Signed-off-by: Oleg Höfling <[email protected]>
1 parent de7ba2b commit 0e41cce

File tree

8 files changed

+40
-41
lines changed

8 files changed

+40
-41
lines changed

docs/source/kinds_of_types.rst

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Class types
1010

1111
Every class is also a valid type. Any instance of a subclass is also
1212
compatible with all superclasses -- it follows that every value is compatible
13-
with the ``object`` type (and incidentally also the ``Any`` type, discussed
13+
with the :py:class:`object` type (and incidentally also the ``Any`` type, discussed
1414
below). Mypy analyzes the bodies of classes to determine which methods and
1515
attributes are available in instances. This example uses subclassing:
1616

@@ -135,7 +135,7 @@ purpose. Example:
135135
.. note::
136136

137137
Usually it's a better idea to use ``Sequence[T]`` instead of ``Tuple[T, ...]``, as
138-
``Sequence`` is also compatible with lists and other non-tuple sequences.
138+
:py:class:`~typing.Sequence` is also compatible with lists and other non-tuple sequences.
139139

140140
.. note::
141141

@@ -212,7 +212,7 @@ Use the ``Union[T1, ..., Tn]`` type constructor to construct a union
212212
type. For example, if an argument has type ``Union[int, str]``, both
213213
integers and strings are valid argument values.
214214

215-
You can use an ``isinstance()`` check to narrow down a union type to a
215+
You can use an :py:func:`isinstance` check to narrow down a union type to a
216216
more specific type:
217217

218218
.. code-block:: python
@@ -235,18 +235,18 @@ more specific type:
235235
.. note::
236236

237237
Operations are valid for union types only if they are valid for *every*
238-
union item. This is why it's often necessary to use an ``isinstance()``
238+
union item. This is why it's often necessary to use an :py:func:`isinstance`
239239
check to first narrow down a union type to a non-union type. This also
240240
means that it's recommended to avoid union types as function return types,
241-
since the caller may have to use ``isinstance()`` before doing anything
241+
since the caller may have to use :py:func:`isinstance` before doing anything
242242
interesting with the value.
243243

244244
.. _strict_optional:
245245

246246
Optional types and the None type
247247
********************************
248248

249-
You can use the ``Optional`` type modifier to define a type variant
249+
You can use the :py:data:`~typing.Optional` type modifier to define a type variant
250250
that allows ``None``, such as ``Optional[int]`` (``Optional[X]`` is
251251
the preferred shorthand for ``Union[X, None]``):
252252

@@ -264,7 +264,7 @@ the preferred shorthand for ``Union[X, None]``):
264264
return None # Error: None not compatible with int
265265
return len(s)
266266
267-
Most operations will not be allowed on unguarded ``None`` or ``Optional``
267+
Most operations will not be allowed on unguarded ``None`` or :py:data:`~typing.Optional`
268268
values:
269269

270270
.. code-block:: python
@@ -415,7 +415,7 @@ Disabling strict optional checking
415415
Mypy also has an option to treat ``None`` as a valid value for every
416416
type (in case you know Java, it's useful to think of it as similar to
417417
the Java ``null``). In this mode ``None`` is also valid for primitive
418-
types such as ``int`` and ``float``, and ``Optional[...]`` types are
418+
types such as ``int`` and ``float``, and :py:data:`~typing.Optional` types are
419419
not required.
420420

421421
The mode is enabled through the ``--no-strict-optional`` command-line
@@ -455,7 +455,7 @@ but it's not obvious from its signature:
455455
print(greeting('Python')) # Okay!
456456
print(greeting(None)) # Also okay!
457457
458-
You can still use ``Optional[t]`` to document that ``None`` is a
458+
You can still use :py:data:`Optional[t] <typing.Optional>` to document that ``None`` is a
459459
valid argument type, even if strict ``None`` checking is not
460460
enabled:
461461

@@ -566,9 +566,9 @@ missing attribute:
566566
p = Point(x=1, y=2)
567567
print(p.z) # Error: Point has no attribute 'z'
568568
569-
If you use ``namedtuple`` to define your named tuple, all the items
569+
If you use :py:func:`namedtuple <collections.namedtuple>` to define your named tuple, all the items
570570
are assumed to have ``Any`` types. That is, mypy doesn't know anything
571-
about item types. You can use ``typing.NamedTuple`` to also define
571+
about item types. You can use :py:class:`~typing.NamedTuple` to also define
572572
item types:
573573

574574
.. code-block:: python
@@ -600,10 +600,10 @@ The type of class objects
600600
<484#the-type-of-class-objects>`.)
601601

602602
Sometimes you want to talk about class objects that inherit from a
603-
given class. This can be spelled as ``Type[C]`` where ``C`` is a
603+
given class. This can be spelled as :py:class:`Type[C] <typing.Type>` where ``C`` is a
604604
class. In other words, when ``C`` is the name of a class, using ``C``
605605
to annotate an argument declares that the argument is an instance of
606-
``C`` (or of a subclass of ``C``), but using ``Type[C]`` as an
606+
``C`` (or of a subclass of ``C``), but using :py:class:`Type[C] <typing.Type>` as an
607607
argument annotation declares that the argument is a class object
608608
deriving from ``C`` (or ``C`` itself).
609609

@@ -634,7 +634,7 @@ you pass it the right class object:
634634
# (Here we could write the user object to a database)
635635
return user
636636
637-
How would we annotate this function? Without ``Type[]`` the best we
637+
How would we annotate this function? Without :py:class:`~typing.Type` the best we
638638
could do would be:
639639

640640
.. code-block:: python
@@ -650,7 +650,7 @@ doesn't see that the ``buyer`` variable has type ``ProUser``:
650650
buyer = new_user(ProUser)
651651
buyer.pay() # Rejected, not a method on User
652652
653-
However, using ``Type[]`` and a type variable with an upper bound (see
653+
However, using :py:class:`~typing.Type` and a type variable with an upper bound (see
654654
:ref:`type-variable-upper-bound`) we can do better:
655655

656656
.. code-block:: python
@@ -670,7 +670,7 @@ Now mypy will infer the correct type of the result when we call
670670
671671
.. note::
672672

673-
The value corresponding to ``Type[C]`` must be an actual class
673+
The value corresponding to :py:class:`Type[C] <typing.Type>` must be an actual class
674674
object that's a subtype of ``C``. Its constructor must be
675675
compatible with the constructor of ``C``. If ``C`` is a type
676676
variable, its upper bound must be a class object.
@@ -688,7 +688,7 @@ strings. This can be challenging to do in a codebase intended to run in
688688
both Python 2 and Python 3 since ``str`` means something different in both
689689
versions and ``unicode`` is not a keyword in Python 3.
690690

691-
To help solve this issue, use ``typing.Text`` which is aliased to
691+
To help solve this issue, use :py:class:`~typing.Text` which is aliased to
692692
``unicode`` in Python 2 and to ``str`` in Python 3. This allows you to
693693
indicate that a function should accept only unicode strings in a
694694
cross-compatible way:
@@ -702,7 +702,7 @@ cross-compatible way:
702702
703703
In other cases, you may want to write a function that will work with any
704704
kind of string but will not let you mix two different string types. To do
705-
so use ``typing.AnyStr``:
705+
so use :py:data:`~typing.AnyStr`:
706706

707707
.. code-block:: python
708708
@@ -728,17 +728,17 @@ Generators
728728
**********
729729

730730
A basic generator that only yields values can be annotated as having a return
731-
type of either ``Iterator[YieldType]`` or ``Iterable[YieldType]``. For example:
731+
type of either :py:class:`Iterator[YieldType] <typing.Iterator>` or :py:class:`Iterable[YieldType] <typing.Iterable>`. For example:
732732

733733
.. code-block:: python
734734
735735
def squares(n: int) -> Iterator[int]:
736736
for i in range(n):
737737
yield i * i
738738
739-
If you want your generator to accept values via the ``send`` method or return
739+
If you want your generator to accept values via the :py:meth:`~generator.send` method or return
740740
a value, you should use the
741-
``Generator[YieldType, SendType, ReturnType]`` generic type instead. For example:
741+
:py:class:`Generator[YieldType, SendType, ReturnType] <typing.Generator>` generic type instead. For example:
742742

743743
.. code-block:: python
744744
@@ -749,7 +749,7 @@ a value, you should use the
749749
return 'Done'
750750
751751
Note that unlike many other generics in the typing module, the ``SendType`` of
752-
``Generator`` behaves contravariantly, not covariantly or invariantly.
752+
:py:class:`~typing.Generator` behaves contravariantly, not covariantly or invariantly.
753753

754754
If you do not plan on receiving or returning values, then set the ``SendType``
755755
or ``ReturnType`` to ``None``, as appropriate. For example, we could have
@@ -762,6 +762,6 @@ annotated the first example as the following:
762762
yield i * i
763763
764764
This is slightly different from using ``Iterable[int]`` or ``Iterator[int]``,
765-
since generators have ``close()``, ``send()``, and ``throw()`` methods that
765+
since generators have :py:meth:`~generator.close`, :py:meth:`~generator.send`, and :py:meth:`~generator.throw` methods that
766766
generic iterables don't. If you will call these methods on the returned
767-
generator, use the ``Generator`` type instead of ``Iterable`` or ``Iterator``.
767+
generator, use the :py:class:`~typing.Generator` type instead of :py:class:`~typing.Iterable` or :py:class:`~typing.Iterator`.

docs/source/metaclasses.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Metaclasses
66
A :ref:`metaclass <python:metaclasses>` is a class that describes
77
the construction and behavior of other classes, similarly to how classes
88
describe the construction and behavior of objects.
9-
The default metaclass is ``type``, but it's possible to use other metaclasses.
10-
Metaclasses allows one to create "a different kind of class", such as Enums,
11-
NamedTuples and singletons.
9+
The default metaclass is :py:class:`type`, but it's possible to use other metaclasses.
10+
Metaclasses allows one to create "a different kind of class", such as
11+
:py:class:`~enum.Enum`\s, :py:class:`~typing.NamedTuple`\s and singletons.
1212

13-
Mypy has some special understanding of ``ABCMeta`` and ``EnumMeta``.
13+
Mypy has some special understanding of :py:class:`~abc.ABCMeta` and ``EnumMeta``.
1414

1515
.. _defining:
1616

@@ -32,7 +32,7 @@ In Python 2, the syntax for defining a metaclass is different:
3232
class A(object):
3333
__metaclass__ = M
3434
35-
Mypy also supports using :py:func:`six.with_metaclass`
35+
Mypy also supports using :py:func:`six.with_metaclass` and :py:func:`@six.add_metaclass <six.add_metaclass>`
3636
to define metaclass in a portable way:
3737

3838
.. code-block:: python
@@ -103,4 +103,4 @@ so it's better not to combine metaclasses and class hierarchies:
103103
* Mypy does not understand dynamically-computed metaclasses,
104104
such as ``class A(metaclass=f()): ...``
105105
* Mypy does not and cannot understand arbitrary metaclass code.
106-
* Mypy only recognizes subclasses of ``type`` as potential metaclasses.
106+
* Mypy only recognizes subclasses of :py:class:`type` as potential metaclasses.

docs/source/python2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Run mypy in Python 2 mode by using the ``--py2`` option::
1515
To run your program, you must have the ``typing`` module in your
1616
Python 2 module search path. Use ``pip install typing`` to install the
1717
module. This also works for Python 3 versions prior to 3.5 that don't
18-
include ``typing`` in the standard library.
18+
include :py:mod:`typing` in the standard library.
1919

2020
The example below illustrates the Python 2 function type annotation
2121
syntax. This syntax is also valid in Python 3 mode:

docs/source/python36.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Mypy has supported all language features new in Python 3.6 starting with mypy
88
type checking.
99

1010
Syntax for variable annotations (:pep:`526`)
11-
---------------------------------------------------------------------------------------
11+
--------------------------------------------
1212

1313
Python 3.6 introduced a new syntax for variable annotations (in
1414
global, class and local scopes). There are two variants of the
@@ -23,7 +23,7 @@ syntax, with or without an initializer expression:
2323
.. _class-var:
2424

2525
You can also mark names intended to be used as class variables with
26-
``ClassVar``. In a pinch you can also use ClassVar in ``# type``
26+
:py:data:`~typing.ClassVar`. In a pinch you can also use :py:data:`~typing.ClassVar` in ``# type``
2727
comments. Example:
2828

2929
.. code-block:: python
@@ -49,7 +49,7 @@ Asynchronous generators (:pep:`525`) and comprehensions (:pep:`530`)
4949

5050
Python 3.6 allows coroutines defined with ``async def`` (:pep:`492`) to be
5151
generators, i.e. contain ``yield`` expressions. It also introduced a syntax for
52-
asynchronous comprehensions. This example uses the ``AsyncIterator`` type to
52+
asynchronous comprehensions. This example uses the :py:class:`~typing.AsyncIterator` type to
5353
define an async generator:
5454

5555
.. code-block:: python

docs/source/running_mypy.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ This is computed from the following items:
371371

372372
.. note::
373373

374-
You cannot point to a PEP 561 package via the MYPYPATH, it must be
374+
You cannot point to a :pep:`561` package via the ``MYPYPATH``, it must be
375375
installed (see :ref:`PEP 561 support <installed-packages>`)
376376

377377
For sources given on the command line, the path is adjusted by crawling

docs/source/stubs.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Python code -- for example, when writing methods in
103103

104104
The recommended style is to use ellipses to do so, just like in
105105
stub files. It is also considered stylistically acceptable to
106-
throw a ``NotImplementedError`` in cases where the user of the
106+
throw a :py:exc:`NotImplementedError` in cases where the user of the
107107
code may accidentally call functions with no actual logic.
108108

109109
You can also elide default arguments as long as the function body
@@ -137,5 +137,4 @@ For example:
137137
Ellipsis expressions are legal syntax in Python 3 only. This means
138138
it is not possible to elide default arguments in Python 2 code.
139139
You can still elide function bodies in Python 2 by using either
140-
the ``pass`` statement or by throwing a ``NotImplementedError``.
141-
140+
the ``pass`` statement or by throwing a :py:exc:`NotImplementedError`.

docs/source/supported_python_features.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ or module outside its definition -- but only if this is visible to the
1313
type checker. This only affects static checking, as mypy performs no
1414
additional type checking at runtime. You can easily work around
1515
this. For example, you can use dynamically typed code or values with
16-
``Any`` types, or you can use ``setattr`` or other introspection
16+
``Any`` types, or you can use :py:func:`setattr` or other introspection
1717
features. However, you need to be careful if you decide to do this. If
1818
used indiscriminately, you may have difficulty using static typing
1919
effectively, since the type checker cannot see functions defined at

docs/source/type_inference_and_annotations.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ possible with the comment syntax:
6565

6666
The best way to think about this is that the type annotation sets the
6767
type of the variable, not the type of the expression. To force the
68-
type of an expression you can use ``cast(<type>, <expression>)``.
68+
type of an expression you can use :py:func:`cast(\<type\>, \<expression\>) <typing.cast>`.
6969

7070
Explicit types for collections
7171
******************************
@@ -114,7 +114,7 @@ assignment could result in non-int values stored in a list of ``int``:
114114
l.append('x')
115115
print(k[-1]) # Ouch; a string in List[int]
116116
117-
Other container types like ``Dict`` and ``Set`` behave similarly. We
117+
Other container types like :py:class:`~typing.Dict` and :py:class:`~typing.Set` behave similarly. We
118118
will discuss how you can work around this in :ref:`variance`.
119119

120120
You can still run the above program; it prints ``x``. This illustrates

0 commit comments

Comments
 (0)