@@ -81,23 +81,27 @@ You may wonder what happens at runtime when you index
81
81
of ``Stack `` that returns instances of the original class on
82
82
instantiation:
83
83
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
94
96
you a class that directly inherits from the target class in Python:
95
97
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)
101
105
102
106
Generic types could be instantiated or subclassed as usual classes,
103
107
but the above examples illustrate that type variables are erased at
@@ -111,7 +115,7 @@ operator.
111
115
Defining sub-classes of generic classes
112
116
***************************************
113
117
114
- User-defined generic classes and generic classes defined in `` typing ` `
118
+ User-defined generic classes and generic classes defined in :py:mod: ` typing `
115
119
can be used as base classes for another classes, both generic and
116
120
non-generic. For example:
117
121
@@ -148,13 +152,13 @@ non-generic. For example:
148
152
149
153
.. note ::
150
154
151
- You have to add an explicit `` Mapping ` ` base class
155
+ You have to add an explicit :py:class: ` ~typing. Mapping ` base class
152
156
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
154
158
*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 >`.
156
160
157
- `` Generic[...] ` ` can be omitted from bases if there are
161
+ :py:class: ` Generic <typing.Generic> ` can be omitted from bases if there are
158
162
other base classes that include type variables, such as ``Mapping[KT, VT] ``
159
163
in the above example. If you include ``Generic[...] `` in bases, then
160
164
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:
276
280
Without using generic ``self ``, the last two lines could not be type-checked properly.
277
281
278
282
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> `:
280
284
281
285
.. code-block :: python
282
286
@@ -328,12 +332,12 @@ a subtype of ``A``, these are defined as follows:
328
332
329
333
Let us illustrate this by few simple examples:
330
334
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
332
336
of ``Union[Animal, int] ``,
333
337
``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
335
339
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
337
341
arguments, namely ``Callable[[Employee], int] `` is a subtype of
338
342
``Callable[[Manager], int] ``. To understand this, consider a function:
339
343
@@ -345,7 +349,7 @@ Let us illustrate this by few simple examples:
345
349
This function needs a callable that can calculate a salary for managers, and
346
350
if we give it a callable that can calculate a salary for an arbitrary
347
351
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
349
353
that it is covariant, but let us consider this code:
350
354
351
355
.. code-block :: python
@@ -364,7 +368,7 @@ Let us illustrate this by few simple examples:
364
368
add_one(my_things) # This may appear safe, but...
365
369
my_things[0 ].rotate() # ...this will fail
366
370
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
368
372
are invariant.
369
373
370
374
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
406
410
407
411
AnyStr = TypeVar(' AnyStr' , str , bytes )
408
412
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.
411
415
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
413
417
two strings or bytes objects, but it can't be called with other
414
418
argument types:
415
419
@@ -462,9 +466,9 @@ this is correct for ``concat``, since ``concat`` actually returns a
462
466
>> > print (type (ss))
463
467
< class ' str' >
464
468
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
466
470
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 `,
468
472
since regular expressions can be based on a string or a bytes pattern.
469
473
470
474
.. _type-variable-upper-bound :
@@ -475,7 +479,7 @@ Type variables with upper bounds
475
479
A type variable can also be restricted to having values that are
476
480
subtypes of a specific type. This type is called the upper bound of
477
481
the type variable, and is specified with the ``bound=... `` keyword
478
- argument to `` TypeVar ` `.
482
+ argument to :py:class: ` ~typing. TypeVar `.
479
483
480
484
.. code-block :: python
481
485
@@ -557,15 +561,15 @@ non-function (e.g. ``my_decorator(1)``) will be rejected.
557
561
558
562
Also note that the ``wrapper() `` function is not type-checked. Wrapper
559
563
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
561
565
``return `` statement in ``my_decorator() ``. See :ref: `casts `.
562
566
563
567
Generic protocols
564
568
*****************
565
569
566
570
Mypy supports generic protocols (see also :ref: `protocol-types `). Several
567
571
: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
569
573
protocols mostly follow the normal rules for generic classes. Example:
570
574
571
575
.. code-block :: python
0 commit comments