Skip to content

Commit a9669fa

Browse files
hoeflinggvanrossum
authored andcommitted
revisited generics.rst, installed_packages.rst, introduction.rst, literal_types.rst (#7685)
Signed-off-by: Oleg Höfling <[email protected]>
1 parent 34fe00b commit a9669fa

File tree

5 files changed

+53
-48
lines changed

5 files changed

+53
-48
lines changed

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,5 @@
273273
'attrs': ('http://www.attrs.org/en/stable', None),
274274
'cython': ('http://docs.cython.org/en/latest', None),
275275
'monkeytype': ('https://monkeytype.readthedocs.io/en/latest', None),
276+
'setuptools': ('https://setuptools.readthedocs.io/en/latest', None),
276277
}

docs/source/generics.rst

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,27 @@ You may wonder what happens at runtime when you index
8181
of ``Stack`` that returns instances of the original class on
8282
instantiation:
8383

84-
>>> print(Stack)
85-
__main__.Stack
86-
>>> print(Stack[int])
87-
__main__.Stack[int]
88-
>>> print(Stack[int]().__class__)
89-
__main__.Stack
90-
91-
Note that built-in types ``list``, ``dict`` and so on do not support
92-
indexing in Python. This is why we have the aliases ``List``, ``Dict``
93-
and so on in the ``typing`` module. Indexing these aliases gives
84+
.. code-block:: python
85+
86+
>>> print(Stack)
87+
__main__.Stack
88+
>>> print(Stack[int])
89+
__main__.Stack[int]
90+
>>> print(Stack[int]().__class__)
91+
__main__.Stack
92+
93+
Note that built-in types :py:class:`list`, :py:class:`dict` and so on do not support
94+
indexing in Python. This is why we have the aliases :py:class:`~typing.List`, :py:class:`~typing.Dict`
95+
and so on in the :py:mod:`typing` module. Indexing these aliases gives
9496
you a class that directly inherits from the target class in Python:
9597

96-
>>> from typing import List
97-
>>> List[int]
98-
typing.List[int]
99-
>>> List[int].__bases__
100-
(<class 'list'>, typing.MutableSequence)
98+
.. code-block:: python
99+
100+
>>> from typing import List
101+
>>> List[int]
102+
typing.List[int]
103+
>>> List[int].__bases__
104+
(<class 'list'>, typing.MutableSequence)
101105
102106
Generic types could be instantiated or subclassed as usual classes,
103107
but the above examples illustrate that type variables are erased at
@@ -111,7 +115,7 @@ operator.
111115
Defining sub-classes of generic classes
112116
***************************************
113117

114-
User-defined generic classes and generic classes defined in ``typing``
118+
User-defined generic classes and generic classes defined in :py:mod:`typing`
115119
can be used as base classes for another classes, both generic and
116120
non-generic. For example:
117121

@@ -148,13 +152,13 @@ non-generic. For example:
148152
149153
.. note::
150154

151-
You have to add an explicit ``Mapping`` base class
155+
You have to add an explicit :py:class:`~typing.Mapping` base class
152156
if you want mypy to consider a user-defined class as a mapping (and
153-
``Sequence`` for sequences, etc.). This is because mypy doesn't use
157+
:py:class:`~typing.Sequence` for sequences, etc.). This is because mypy doesn't use
154158
*structural subtyping* for these ABCs, unlike simpler protocols
155-
like ``Iterable``, which use :ref:`structural subtyping <protocol-types>`.
159+
like :py:class:`~typing.Iterable`, which use :ref:`structural subtyping <protocol-types>`.
156160

157-
``Generic[...]`` can be omitted from bases if there are
161+
:py:class:`Generic <typing.Generic>` can be omitted from bases if there are
158162
other base classes that include type variables, such as ``Mapping[KT, VT]``
159163
in the above example. If you include ``Generic[...]`` in bases, then
160164
it should list all type variables present in other bases (or more,
@@ -276,7 +280,7 @@ In this way, for example, you can typecheck chaining of setter methods:
276280
Without using generic ``self``, the last two lines could not be type-checked properly.
277281

278282
Other uses are factory methods, such as copy and deserialization.
279-
For class methods, you can also define generic ``cls``, using ``Type[T]``:
283+
For class methods, you can also define generic ``cls``, using :py:class:`Type[T] <typing.Type>`:
280284

281285
.. code-block:: python
282286
@@ -328,12 +332,12 @@ a subtype of ``A``, these are defined as follows:
328332

329333
Let us illustrate this by few simple examples:
330334

331-
* ``Union`` is covariant in all variables: ``Union[Cat, int]`` is a subtype
335+
* :py:data:`~typing.Union` is covariant in all variables: ``Union[Cat, int]`` is a subtype
332336
of ``Union[Animal, int]``,
333337
``Union[Dog, int]`` is also a subtype of ``Union[Animal, int]``, etc.
334-
Most immutable containers such as ``Sequence`` and ``FrozenSet`` are also
338+
Most immutable containers such as :py:class:`~typing.Sequence` and :py:class:`~typing.FrozenSet` are also
335339
covariant.
336-
* ``Callable`` is an example of type that behaves contravariant in types of
340+
* :py:data:`~typing.Callable` is an example of type that behaves contravariant in types of
337341
arguments, namely ``Callable[[Employee], int]`` is a subtype of
338342
``Callable[[Manager], int]``. To understand this, consider a function:
339343

@@ -345,7 +349,7 @@ Let us illustrate this by few simple examples:
345349
This function needs a callable that can calculate a salary for managers, and
346350
if we give it a callable that can calculate a salary for an arbitrary
347351
employee, it's still safe.
348-
* ``List`` is an invariant generic type. Naively, one would think
352+
* :py:class:`~typing.List` is an invariant generic type. Naively, one would think
349353
that it is covariant, but let us consider this code:
350354

351355
.. code-block:: python
@@ -364,7 +368,7 @@ Let us illustrate this by few simple examples:
364368
add_one(my_things) # This may appear safe, but...
365369
my_things[0].rotate() # ...this will fail
366370
367-
Another example of invariant type is ``Dict``. Most mutable containers
371+
Another example of invariant type is :py:class:`~typing.Dict`. Most mutable containers
368372
are invariant.
369373

370374
By default, mypy assumes that all user-defined generics are invariant.
@@ -406,10 +410,10 @@ as its value. A typical example is a type variable that can only have values
406410
407411
AnyStr = TypeVar('AnyStr', str, bytes)
408412
409-
This is actually such a common type variable that ``AnyStr`` is
410-
defined in ``typing`` and we don't need to define it ourselves.
413+
This is actually such a common type variable that :py:data:`~typing.AnyStr` is
414+
defined in :py:mod:`typing` and we don't need to define it ourselves.
411415

412-
We can use ``AnyStr`` to define a function that can concatenate
416+
We can use :py:data:`~typing.AnyStr` to define a function that can concatenate
413417
two strings or bytes objects, but it can't be called with other
414418
argument types:
415419

@@ -462,9 +466,9 @@ this is correct for ``concat``, since ``concat`` actually returns a
462466
>>> print(type(ss))
463467
<class 'str'>
464468
465-
You can also use a ``TypeVar`` with a restricted set of possible
469+
You can also use a :py:class:`~typing.TypeVar` with a restricted set of possible
466470
values when defining a generic class. For example, mypy uses the type
467-
``typing.Pattern[AnyStr]`` for the return value of ``re.compile``,
471+
:py:class:`Pattern[AnyStr] <typing.Pattern>` for the return value of :py:func:`re.compile`,
468472
since regular expressions can be based on a string or a bytes pattern.
469473

470474
.. _type-variable-upper-bound:
@@ -475,7 +479,7 @@ Type variables with upper bounds
475479
A type variable can also be restricted to having values that are
476480
subtypes of a specific type. This type is called the upper bound of
477481
the type variable, and is specified with the ``bound=...`` keyword
478-
argument to ``TypeVar``.
482+
argument to :py:class:`~typing.TypeVar`.
479483

480484
.. code-block:: python
481485
@@ -557,15 +561,15 @@ non-function (e.g. ``my_decorator(1)``) will be rejected.
557561

558562
Also note that the ``wrapper()`` function is not type-checked. Wrapper
559563
functions are typically small enough that this is not a big
560-
problem. This is also the reason for the ``cast()`` call in the
564+
problem. This is also the reason for the :py:func:`~typing.cast` call in the
561565
``return`` statement in ``my_decorator()``. See :ref:`casts`.
562566

563567
Generic protocols
564568
*****************
565569

566570
Mypy supports generic protocols (see also :ref:`protocol-types`). Several
567571
:ref:`predefined protocols <predefined_protocols>` are generic, such as
568-
``Iterable[T]``, and you can define additional generic protocols. Generic
572+
:py:class:`Iterable[T] <typing.Iterable>`, and you can define additional generic protocols. Generic
569573
protocols mostly follow the normal rules for generic classes. Example:
570574

571575
.. code-block:: python

docs/source/installed_packages.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Using installed packages
44
========================
55

66
:pep:`561` specifies how to mark a package as supporting type checking.
7-
Below is a summary of how to create :pep:`561` compatible packages and have
7+
Below is a summary of how to create PEP 561 compatible packages and have
88
mypy use them in type checking.
99

10-
Using :pep:`561` compatible packages with mypy
11-
**********************************************
10+
Using PEP 561 compatible packages with mypy
11+
*******************************************
1212

1313
Generally, you do not need to do anything to use installed packages that
1414
support typing for the Python executable used to run mypy. Note that most
@@ -36,8 +36,8 @@ to find the package, it must be installed. For a package ``foo``, the name of
3636
the stub-only package (``foo-stubs``) is not a legal package name, so mypy
3737
will not find it, unless it is installed.
3838

39-
Making :pep:`561` compatible packages
40-
*************************************
39+
Making PEP 561 compatible packages
40+
**********************************
4141

4242
:pep:`561` notes three main ways to distribute type information. The first is a
4343
package that has only inline type annotations in the code itself. The second is
@@ -60,7 +60,7 @@ structure as follows
6060
lib.py
6161
py.typed
6262
63-
the setup.py might look like
63+
the ``setup.py`` might look like
6464

6565
.. code-block:: python
6666
@@ -76,7 +76,7 @@ the setup.py might look like
7676
7777
.. note::
7878

79-
If you use setuptools, you must pass the option ``zip_safe=False`` to
79+
If you use :doc:`setuptools <setuptools:index>`, you must pass the option ``zip_safe=False`` to
8080
``setup()``, or mypy will not be able to find the installed package.
8181

8282
Some packages have a mix of stub files and runtime files. These packages also
@@ -91,7 +91,7 @@ require a ``py.typed`` file. An example can be seen below
9191
lib.pyi
9292
py.typed
9393
94-
the setup.py might look like:
94+
the ``setup.py`` might look like:
9595

9696
.. code-block:: python
9797
@@ -121,7 +121,7 @@ had stubs for ``package_c``, we might do the following:
121121
__init__.pyi
122122
lib.pyi
123123
124-
the setup.py might look like:
124+
the ``setup.py`` might look like:
125125

126126
.. code-block:: python
127127

docs/source/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ annotations are just hints for mypy and don't interfere when running your progra
88
You run your program with a standard Python interpreter, and the annotations
99
are treated effectively as comments.
1010

11-
Using the Python 3 function annotation syntax (using the :pep`484` notation) or
11+
Using the Python 3 function annotation syntax (using the :pep:`484` notation) or
1212
a comment-based annotation syntax for Python 2 code, you will be able to
1313
efficiently annotate your code and use mypy to check the code for common
1414
errors. Mypy has a powerful and easy-to-use type system with modern features

docs/source/literal_types.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Literal types
55

66
.. note::
77

8-
Literal is an officially supported feature, but is highly experimental
8+
``Literal`` is an officially supported feature, but is highly experimental
99
and should be considered to be in alpha stage. It is very likely that future
1010
releases of mypy will modify the behavior of literal types, either by adding
1111
new features or by tuning or removing problematic ones.
@@ -111,7 +111,7 @@ are **not** assumed to be literals:
111111
reveal_type(b) # Revealed type is 'int'
112112
113113
If you find repeating the value of the variable in the type hint to be tedious,
114-
you can instead change the variable to be :ref:`Final <final_attrs>`:
114+
you can instead change the variable to be ``Final`` (see :ref:`final_attrs`):
115115

116116
.. code-block:: python
117117
@@ -124,7 +124,7 @@ you can instead change the variable to be :ref:`Final <final_attrs>`:
124124
reveal_type(c) # Revealed type is 'int'
125125
expects_literal(c) # ...but this type checks!
126126
127-
If you do not provide an explicit type in the Final, the type of ``c`` becomes
127+
If you do not provide an explicit type in the ``Final``, the type of ``c`` becomes
128128
context-sensitive: mypy will basically try "substituting" the original assigned
129129
value whenever it's used before performing type checking. So, mypy will type-check
130130
the above program almost as if it were written like so:
@@ -141,7 +141,7 @@ the above program almost as if it were written like so:
141141
This is why ``expects_literal(19)`` type-checks despite the fact that ``reveal_type(c)``
142142
reports ``int``.
143143

144-
So while changing a variable to be Final is not quite the same thing as adding
144+
So while changing a variable to be ``Final`` is not quite the same thing as adding
145145
an explicit ``Literal[...]`` annotation, it often leads to the same effect in practice.
146146

147147
Limitations

0 commit comments

Comments
 (0)