@@ -24,7 +24,7 @@ initialized within the class. Mypy infers the types of attributes:
24
24
a.y = 3 # Error: 'A' has no attribute 'y'
25
25
26
26
This is a bit like each class having an implicitly defined
27
- `` __slots__ ` ` attribute. This is only enforced during type
27
+ :py:data: ` __slots__ <object.__slots__> ` attribute. This is only enforced during type
28
28
checking and not when your program is running.
29
29
30
30
You can declare types of variables in the class body explicitly using
@@ -40,7 +40,7 @@ a type annotation:
40
40
41
41
As in Python generally, a variable defined in the class body can be used
42
42
as a class or an instance variable. (As discussed in the next section, you
43
- can override this with a `` ClassVar ` ` annotation.)
43
+ can override this with a :py:data: ` ~typing. ClassVar ` annotation.)
44
44
45
45
Type comments work as well, if you need to support Python versions earlier
46
46
than 3.6:
@@ -81,11 +81,11 @@ to it explicitly using ``self``:
81
81
Annotating ``__init__ `` methods
82
82
*******************************
83
83
84
- The `` __init__ ` ` method is somewhat special -- it doesn't return a
84
+ The :py:meth: ` __init__ <object.__init__> ` method is somewhat special -- it doesn't return a
85
85
value. This is best expressed as ``-> None ``. However, since many feel
86
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
87
+ on :py:meth: ` __init__ <object.__init__> ` methods **if at least one argument is annotated **. For
88
+ example, in the following classes :py:meth: ` __init__ <object.__init__> ` is considered fully
89
89
annotated:
90
90
91
91
.. code-block :: python
@@ -98,7 +98,7 @@ annotated:
98
98
def __init__ (self , arg : int ):
99
99
self .var = arg
100
100
101
- However, if `` __init__ ` ` has no annotated arguments and no return type
101
+ However, if :py:meth: ` __init__ <object.__init__> ` has no annotated arguments and no return type
102
102
annotation, it is considered an untyped method:
103
103
104
104
.. code-block :: python
@@ -111,7 +111,7 @@ annotation, it is considered an untyped method:
111
111
Class attribute annotations
112
112
***************************
113
113
114
- You can use a `` ClassVar[t] ` ` annotation to explicitly declare that a
114
+ You can use a :py:data: ` ClassVar[t] <typing.ClassVar> ` annotation to explicitly declare that a
115
115
particular attribute should not be set on instances:
116
116
117
117
.. code-block :: python
@@ -134,7 +134,7 @@ particular attribute should not be set on instances:
134
134
PyPI). If you use Python 2.7, you can import it from ``typing ``.
135
135
136
136
It's not necessary to annotate all class variables using
137
- `` ClassVar `` . An attribute without the `` ClassVar ` ` annotation can
137
+ :py:data: ` ~typing. ClassVar `. An attribute without the :py:data: ` ~typing. ClassVar ` annotation can
138
138
still be used as a class variable. However, mypy won't prevent it from
139
139
being used as an instance variable, as discussed previously:
140
140
@@ -148,13 +148,13 @@ being used as an instance variable, as discussed previously:
148
148
a = A()
149
149
a.x = 1 # Also OK
150
150
151
- Note that `` ClassVar ` ` is not a class, and you can't use it with
152
- `` isinstance() `` or `` issubclass() ` `. It does not change Python
151
+ Note that :py:data: ` ~typing. ClassVar ` is not a class, and you can't use it with
152
+ :py:func: ` isinstance ` or :py:func: ` issubclass `. It does not change Python
153
153
runtime behavior -- it's only for type checkers such as mypy (and
154
154
also helpful for human readers).
155
155
156
156
You can also omit the square brackets and the variable type in
157
- a `` ClassVar ` ` annotation, but this might not do what you'd expect:
157
+ a :py:data: ` ~typing. ClassVar ` annotation, but this might not do what you'd expect:
158
158
159
159
.. code-block :: python
160
160
@@ -165,7 +165,7 @@ In this case the type of the attribute will be implicitly ``Any``.
165
165
This behavior will change in the future, since it's surprising.
166
166
167
167
.. note ::
168
- A `` ClassVar ` ` type parameter cannot include type variables:
168
+ A :py:data: ` ~typing. ClassVar ` type parameter cannot include type variables:
169
169
``ClassVar[T] `` and ``ClassVar[List[T]] ``
170
170
are both invalid if ``T `` is a type variable (see :ref: `generic-classes `
171
171
for more about type variables).
@@ -232,10 +232,10 @@ effect at runtime:
232
232
Abstract base classes and multiple inheritance
233
233
**********************************************
234
234
235
- Mypy supports Python abstract base classes (ABCs). Abstract classes
235
+ Mypy supports Python :doc: ` abstract base classes < library/abc >` (ABCs). Abstract classes
236
236
have at least one abstract method or property that must be implemented
237
237
by any *concrete * (non-abstract) subclass. You can define abstract base
238
- classes using the `` abc.ABCMeta `` metaclass and the `` abc.abstractmethod ` `
238
+ classes using the :py:class: ` abc.ABCMeta ` metaclass and the :py:func: ` @ abc.abstractmethod <abc.abstractmethod> `
239
239
function decorator. Example:
240
240
241
241
.. code-block :: python
@@ -263,11 +263,11 @@ function decorator. Example:
263
263
264
264
.. note ::
265
265
266
- In Python 2.7 you have to use `` @abc.abstractproperty ` ` to define
266
+ In Python 2.7 you have to use :py:func: ` @abc.abstractproperty <abc.abstractproperty> ` to define
267
267
an abstract property.
268
268
269
269
Note that mypy performs checking for unimplemented abstract methods
270
- even if you omit the `` ABCMeta ` ` metaclass. This can be useful if the
270
+ even if you omit the :py:class: ` ~abc. ABCMeta ` metaclass. This can be useful if the
271
271
metaclass would cause runtime metaclass conflicts.
272
272
273
273
Since you can't create instances of ABCs, they are most commonly used in
0 commit comments