@@ -10,7 +10,7 @@ Class types
10
10
11
11
Every class is also a valid type. Any instance of a subclass is also
12
12
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
14
14
below). Mypy analyzes the bodies of classes to determine which methods and
15
15
attributes are available in instances. This example uses subclassing:
16
16
@@ -135,7 +135,7 @@ purpose. Example:
135
135
.. note ::
136
136
137
137
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.
139
139
140
140
.. note ::
141
141
@@ -212,7 +212,7 @@ Use the ``Union[T1, ..., Tn]`` type constructor to construct a union
212
212
type. For example, if an argument has type ``Union[int, str] ``, both
213
213
integers and strings are valid argument values.
214
214
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
216
216
more specific type:
217
217
218
218
.. code-block :: python
@@ -235,18 +235,18 @@ more specific type:
235
235
.. note ::
236
236
237
237
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 `
239
239
check to first narrow down a union type to a non-union type. This also
240
240
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
242
242
interesting with the value.
243
243
244
244
.. _strict_optional :
245
245
246
246
Optional types and the None type
247
247
********************************
248
248
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
250
250
that allows ``None ``, such as ``Optional[int] `` (``Optional[X] `` is
251
251
the preferred shorthand for ``Union[X, None] ``):
252
252
@@ -264,7 +264,7 @@ the preferred shorthand for ``Union[X, None]``):
264
264
return None # Error: None not compatible with int
265
265
return len (s)
266
266
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 `
268
268
values:
269
269
270
270
.. code-block :: python
@@ -415,7 +415,7 @@ Disabling strict optional checking
415
415
Mypy also has an option to treat ``None `` as a valid value for every
416
416
type (in case you know Java, it's useful to think of it as similar to
417
417
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
419
419
not required.
420
420
421
421
The mode is enabled through the ``--no-strict-optional `` command-line
@@ -455,7 +455,7 @@ but it's not obvious from its signature:
455
455
print (greeting(' Python' )) # Okay!
456
456
print (greeting(None )) # Also okay!
457
457
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
459
459
valid argument type, even if strict ``None `` checking is not
460
460
enabled:
461
461
@@ -566,9 +566,9 @@ missing attribute:
566
566
p = Point(x = 1 , y = 2 )
567
567
print (p.z) # Error: Point has no attribute 'z'
568
568
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
570
570
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
572
572
item types:
573
573
574
574
.. code-block :: python
@@ -600,10 +600,10 @@ The type of class objects
600
600
<484#the-type-of-class-objects >`.)
601
601
602
602
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
604
604
class. In other words, when ``C `` is the name of a class, using ``C ``
605
605
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
607
607
argument annotation declares that the argument is a class object
608
608
deriving from ``C `` (or ``C `` itself).
609
609
@@ -634,7 +634,7 @@ you pass it the right class object:
634
634
# (Here we could write the user object to a database)
635
635
return user
636
636
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
638
638
could do would be:
639
639
640
640
.. code-block :: python
@@ -650,7 +650,7 @@ doesn't see that the ``buyer`` variable has type ``ProUser``:
650
650
buyer = new_user(ProUser)
651
651
buyer.pay() # Rejected, not a method on User
652
652
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
654
654
:ref: `type-variable-upper-bound `) we can do better:
655
655
656
656
.. code-block :: python
@@ -670,7 +670,7 @@ Now mypy will infer the correct type of the result when we call
670
670
671
671
.. note ::
672
672
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
674
674
object that's a subtype of ``C ``. Its constructor must be
675
675
compatible with the constructor of ``C ``. If ``C `` is a type
676
676
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
688
688
both Python 2 and Python 3 since ``str `` means something different in both
689
689
versions and ``unicode `` is not a keyword in Python 3.
690
690
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
692
692
``unicode `` in Python 2 and to ``str `` in Python 3. This allows you to
693
693
indicate that a function should accept only unicode strings in a
694
694
cross-compatible way:
@@ -702,7 +702,7 @@ cross-compatible way:
702
702
703
703
In other cases, you may want to write a function that will work with any
704
704
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 `:
706
706
707
707
.. code-block :: python
708
708
@@ -728,17 +728,17 @@ Generators
728
728
**********
729
729
730
730
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:
732
732
733
733
.. code-block :: python
734
734
735
735
def squares (n : int ) -> Iterator[int ]:
736
736
for i in range (n):
737
737
yield i * i
738
738
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
740
740
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:
742
742
743
743
.. code-block :: python
744
744
@@ -749,7 +749,7 @@ a value, you should use the
749
749
return ' Done'
750
750
751
751
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.
753
753
754
754
If you do not plan on receiving or returning values, then set the ``SendType ``
755
755
or ``ReturnType `` to ``None ``, as appropriate. For example, we could have
@@ -762,6 +762,6 @@ annotated the first example as the following:
762
762
yield i * i
763
763
764
764
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
766
766
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 `.
0 commit comments