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:
@@ -51,9 +52,9 @@ than 3.6:
51
52
x = None # type: List [ int ] # Declare attribute 'x' of type List[int]
52
53
53
54
Note that attribute definitions in the class body that use a type comment
54
- are special: a `` None ` ` value is valid as the initializer, even though
55
+ are special: a :py:data: ` None ` value is valid as the initializer, even though
55
56
the declared type is not optional. This should be used sparingly, as this can
56
- result in `` None ` `-related runtime errors that mypy can't detect.
57
+ result in :py:data: ` None `-related runtime errors that mypy can't detect.
57
58
58
59
Similarly, you can give explicit types to instance variables defined
59
60
in a method:
@@ -78,15 +79,16 @@ 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 :py:meth: `__init__ <object.__init__> `
89
+ methods **if at least one argument is annotated **. For example,
90
+ in the following classes :py:meth: `__init__ <object.__init__> `
91
+ is considered fully annotated:
90
92
91
93
.. code-block :: python
92
94
@@ -98,8 +100,8 @@ annotated:
98
100
def __init__ (self , arg : int ):
99
101
self .var = arg
100
102
101
- However, if `` __init__ `` has no annotated arguments and no return type
102
- annotation, it is considered an untyped method:
103
+ However, if :py:meth: ` __init__ <object.__init__> ` has no annotated
104
+ arguments and no return type annotation, it is considered an untyped method:
103
105
104
106
.. code-block :: python
105
107
@@ -130,12 +132,14 @@ 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 :py:data: `~typing.ClassVar ` from ``typing_extensions ``
136
+ instead (available on PyPI). If you use Python 2.7, you can import it
137
+ from :py:mod: `typing `.
135
138
136
139
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
140
+ :py:data: `~typing.ClassVar `. An attribute without the
141
+ :py:data: `~typing.ClassVar ` annotation can still be used
142
+ as a class variable. However, mypy won't prevent it from
139
143
being used as an instance variable, as discussed previously:
140
144
141
145
.. code-block :: python
@@ -148,27 +152,29 @@ being used as an instance variable, as discussed previously:
148
152
a = A()
149
153
a.x = 1 # Also OK
150
154
151
- 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).
155
+ Note that :py:data: ` ~typing. ClassVar ` is not a class, and
156
+ you can't use it with :py:func: ` isinstance ` or :py:func: ` issubclass `.
157
+ It does not change Python runtime behavior -- it's only for
158
+ type checkers such as mypy (and also helpful for human readers).
155
159
156
160
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:
161
+ a :py:data: `~typing.ClassVar ` annotation, but this might not do
162
+ what you'd expect:
158
163
159
164
.. code-block :: python
160
165
161
166
class A :
162
167
y: ClassVar = 0 # Type implicitly Any!
163
168
164
- In this case the type of the attribute will be implicitly ``Any ``.
169
+ In this case the type of the attribute will be implicitly
170
+ :py:data: `~typing.Any `.
165
171
This behavior will change in the future, since it's surprising.
166
172
167
173
.. 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).
174
+ A :py:data: ` ~typing. ClassVar ` type parameter cannot include
175
+ type variables: ``ClassVar[T] `` and ``ClassVar[List[T]] ``
176
+ are both invalid if ``T `` is a type variable (see
177
+ :ref: ` generic-classes ` for more about type variables).
172
178
173
179
Overriding statically typed methods
174
180
***********************************
@@ -235,8 +241,9 @@ Abstract base classes and multiple inheritance
235
241
Mypy supports Python abstract base classes (ABCs). Abstract classes
236
242
have at least one abstract method or property that must be implemented
237
243
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:
244
+ classes using the :py:class: `abc.ABCMeta ` metaclass and the
245
+ :py:func: `@abc.abstractmethod <abc.abstractmethod> ` function decorator.
246
+ Example:
240
247
241
248
.. code-block :: python
242
249
@@ -263,12 +270,12 @@ function decorator. Example:
263
270
264
271
.. note ::
265
272
266
- In Python 2.7 you have to use `` @abc.abstractproperty `` to define
267
- an abstract property.
273
+ In Python 2.7 you have to use :py:func: ` @abc.abstractproperty
274
+ <abc.abstractproperty> ` to define an abstract property.
268
275
269
276
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.
277
+ even if you omit the :py:class: ` ~abc. ABCMeta ` metaclass. This can be
278
+ useful if the metaclass would cause runtime metaclass conflicts.
272
279
273
280
Since you can't create instances of ABCs, they are most commonly used in
274
281
type annotations. For example, this method accepts arbitrary iterables
0 commit comments