1
1
Class basics
2
2
============
3
3
4
+
4
5
This section will help get you started annotating your
5
- classes. Built-in classes such as `` int `` also follow these same
6
- rules.
6
+ classes. Built-in classes such as :py:class: ` int ` also
7
+ follow these same rules.
7
8
8
9
Instance and class attributes
9
10
*****************************
@@ -24,8 +25,8 @@ initialized within the class. Mypy infers the types of attributes:
24
25
a.y = 3 # Error: 'A' has no attribute 'y'
25
26
26
27
This is a bit like each class having an implicitly defined
27
- `` __slots__ `` attribute. This is only enforced during type
28
- checking and not when your program is running.
28
+ :py:data: ` __slots__ <object.__slots__> ` attribute. This is only
29
+ enforced during type checking and not when your program is running.
29
30
30
31
You can declare types of variables in the class body explicitly using
31
32
a type annotation:
@@ -40,7 +41,7 @@ a type annotation:
40
41
41
42
As in Python generally, a variable defined in the class body can be used
42
43
as a class or an instance variable. (As discussed in the next section, you
43
- can override this with a `` ClassVar ` ` annotation.)
44
+ can override this with a :py:data: ` ~typing. ClassVar ` annotation.)
44
45
45
46
Type comments work as well, if you need to support Python versions earlier
46
47
than 3.6:
@@ -78,15 +79,15 @@ to it explicitly using ``self``:
78
79
a = self
79
80
a.x = 1 # Error: 'x' not defined
80
81
81
- Annotating `__init__ ` methods
82
- *****************************
82
+ Annotating `` __init__ ` ` methods
83
+ *******************************
83
84
84
- The `` __init__ `` method is somewhat special -- it doesn't return a
85
- value. This is best expressed as ``-> None ``. However, since many feel
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
89
- annotated:
85
+ The :py:meth: ` __init__ <object.__init__> ` method is somewhat special --
86
+ it doesn't return a value. This is best expressed as ``-> None ``.
87
+ However, since many feel this is redundant, it is allowed to omit
88
+ the return type declaration on ``__init__ `` methods **if at least
89
+ one argument is annotated **. For example, in the following classes
90
+ `` __init__ `` is considered fully annotated:
90
91
91
92
.. code-block :: python
92
93
@@ -111,8 +112,9 @@ annotation, it is considered an untyped method:
111
112
Class attribute annotations
112
113
***************************
113
114
114
- You can use a ``ClassVar[t] `` annotation to explicitly declare that a
115
- particular attribute should not be set on instances:
115
+ You can use a :py:data: `ClassVar[t] <typing.ClassVar> ` annotation
116
+ to explicitly declare that a particular attribute should not be set
117
+ on instances:
116
118
117
119
.. code-block :: python
118
120
@@ -130,13 +132,13 @@ particular attribute should not be set on instances:
130
132
.. note ::
131
133
132
134
If you need to support Python 3 versions 3.5.2 or earlier, you have
133
- to import ``ClassVar `` from ``typing_extensions `` instead (available on
134
- PyPI). If you use Python 2.7, you can import it from ``typing ``.
135
+ to import ``ClassVar `` from ``typing_extensions `` instead (available
136
+ on PyPI). If you use Python 2.7, you can import it from ``typing ``.
135
137
136
- It's not necessary to annotate all class variables using
137
- `` ClassVar ``. An attribute without the ``ClassVar `` annotation can
138
- still be used as a class variable. However, mypy won't prevent it from
139
- being used as an instance variable, as discussed previously:
138
+ It's not necessary to annotate all class variables using `` ClassVar ``.
139
+ An attribute without the ``ClassVar `` annotation can still be used
140
+ as a class variable. However, mypy won't prevent it from being used
141
+ as an instance variable, as discussed previously:
140
142
141
143
.. code-block :: python
142
144
@@ -149,9 +151,9 @@ being used as an instance variable, as discussed previously:
149
151
a.x = 1 # Also OK
150
152
151
153
Note that ``ClassVar `` is not a class, and you can't use it with
152
- `` isinstance() `` or `` issubclass() ``. It does not change Python
153
- runtime behavior -- it's only for type checkers such as mypy (and
154
- also helpful for human readers).
154
+ :py:func: ` isinstance ` or :py:func: ` issubclass `.
155
+ It does not change Python runtime behavior -- it's only for
156
+ type checkers such as mypy (and also helpful for human readers).
155
157
156
158
You can also omit the square brackets and the variable type in
157
159
a ``ClassVar `` annotation, but this might not do what you'd expect:
@@ -161,14 +163,15 @@ a ``ClassVar`` annotation, but this might not do what you'd expect:
161
163
class A :
162
164
y: ClassVar = 0 # Type implicitly Any!
163
165
164
- In this case the type of the attribute will be implicitly ``Any ``.
166
+ In this case the type of the attribute will be implicitly
167
+ :py:data: `~typing.Any `.
165
168
This behavior will change in the future, since it's surprising.
166
169
167
170
.. note ::
168
- A `` ClassVar `` type parameter cannot include type variables:
169
- ``ClassVar[T] `` and ``ClassVar[List[T]] ``
170
- are both invalid if ``T `` is a type variable (see :ref: ` generic-classes `
171
- for more about type variables).
171
+ A :py:data: ` ~typing. ClassVar ` type parameter cannot include
172
+ type variables: ``ClassVar[T] `` and ``ClassVar[List[T]] ``
173
+ are both invalid if ``T `` is a type variable (see
174
+ :ref: ` generic-classes ` for more about type variables).
172
175
173
176
Overriding statically typed methods
174
177
***********************************
@@ -235,8 +238,9 @@ Abstract base classes and multiple inheritance
235
238
Mypy supports Python abstract base classes (ABCs). Abstract classes
236
239
have at least one abstract method or property that must be implemented
237
240
by any *concrete * (non-abstract) subclass. You can define abstract base
238
- classes using the ``abc.ABCMeta `` metaclass and the ``abc.abstractmethod ``
239
- function decorator. Example:
241
+ classes using the :py:class: `abc.ABCMeta ` metaclass and the
242
+ :py:func: `@abc.abstractmethod <abc.abstractmethod> ` function decorator.
243
+ Example:
240
244
241
245
.. code-block :: python
242
246
@@ -263,12 +267,12 @@ function decorator. Example:
263
267
264
268
.. note ::
265
269
266
- In Python 2.7 you have to use `` @abc.abstractproperty `` to define
267
- an abstract property.
270
+ In Python 2.7 you have to use :py:func: ` @abc.abstractproperty
271
+ <abc.abstractproperty> ` to define an abstract property.
268
272
269
273
Note that mypy performs checking for unimplemented abstract methods
270
- even if you omit the `` ABCMeta `` metaclass. This can be useful if the
271
- metaclass would cause runtime metaclass conflicts.
274
+ even if you omit the :py:class: ` ~abc. ABCMeta ` metaclass. This can be
275
+ useful if the metaclass would cause runtime metaclass conflicts.
272
276
273
277
Since you can't create instances of ABCs, they are most commonly used in
274
278
type annotations. For example, this method accepts arbitrary iterables
0 commit comments