blob: 489dece7ccf3cbfe426a36e911bcd38c432fb994 [file] [log] [blame]
Miss Islington (bot)04e96da2018-05-16 02:17:03 -07001:mod:`dataclasses` --- Dataclasses
2==========================================
3
4.. module:: dataclasses
5 :synopsis: Generate special methods and add to user-defined classes.
6
7.. moduleauthor:: Eric V. Smith <eric@trueblade.com>
8.. sectionauthor:: Eric V. Smith <eric@trueblade.com>
9
10**Source code:** :source:`Lib/dataclasses.py`
11
12--------------
13
14This module provides a decorator and functions for automatically
15adding generated :term:`special method`\s such as :meth:`__init__` and
16:meth:`__repr__` to user-defined classes. It was originally described
17in :pep:`557`.
18
19The member variables to use in these generated methods are defined
20using :pep:`526` type annotations. For example this code::
21
22 @dataclass
23 class InventoryItem:
24 '''Class for keeping track of an item in inventory.'''
25 name: str
26 unit_price: float
27 quantity_on_hand: int = 0
28
29 def total_cost(self) -> float:
30 return self.unit_price * self.quantity_on_hand
31
32Will add, among other things, a :meth:`__init__` that looks like::
33
34 def __init__(self, name: str, unit_price: float, quantity_on_hand: int=0):
35 self.name = name
36 self.unit_price = unit_price
37 self.quantity_on_hand = quantity_on_hand
38
39Note that this method is automatically added to the class: it is not
40directly specified in the ``InventoryItem`` definition shown above.
41
42.. versionadded:: 3.7
43
44Module-level decorators, classes, and functions
45-----------------------------------------------
46
47.. decorator:: dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)
48
49 This function is a :term:`decorator` that is used to add generated
50 :term:`special method`\s to classes, as described below.
51
52 The :func:`dataclass` decorator examines the class to find
53 ``field``\s. A ``field`` is defined as class variable that has a
54 type annotation. With two exceptions described below, nothing in
55 :func:`dataclass` examines the type specified in the variable
56 annotation.
57
58 The order of the fields in all of the generated methods is the
59 order in which they appear in the class definition.
60
61 The :func:`dataclass` decorator will add various "dunder" methods to
62 the class, described below. If any of the added methods already
63 exist on the class, a :exc:`TypeError` will be raised. The decorator
64 returns the same class that is called on: no new class is created.
65
66 If :func:`dataclass` is used just as a simple decorator with no parameters,
67 it acts as if it has the default values documented in this
68 signature. That is, these three uses of :func:`dataclass` are
69 equivalent::
70
71 @dataclass
72 class C:
73 ...
74
75 @dataclass()
76 class C:
77 ...
78
79 @dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)
80 class C:
81 ...
82
83 The parameters to :func:`dataclass` are:
84
85 - ``init``: If true (the default), a :meth:`__init__` method will be
86 generated.
87
88 If the class already defines :meth:`__init__`, this parameter is
89 ignored.
90
91 - ``repr``: If true (the default), a :meth:`__repr__` method will be
92 generated. The generated repr string will have the class name and
93 the name and repr of each field, in the order they are defined in
94 the class. Fields that are marked as being excluded from the repr
95 are not included. For example:
96 ``InventoryItem(name='widget', unit_price=3.0, quantity_on_hand=10)``.
97
98 If the class already defines :meth:`__repr__`, this parameter is
99 ignored.
100
101 - ``eq``: If true (the default), an :meth:`__eq__` method will be
102 generated. This method compares the class as if it were a tuple
103 of its fields, in order. Both instances in the comparison must
104 be of the identical type.
105
106 If the class already defines :meth:`__eq__`, this parameter is
107 ignored.
108
109 - ``order``: If true (the default is ``False``), :meth:`__lt__`,
110 :meth:`__le__`, :meth:`__gt__`, and :meth:`__ge__` methods will be
111 generated. These compare the class as if it were a tuple of its
112 fields, in order. Both instances in the comparison must be of the
113 identical type. If ``order`` is true and ``eq`` is false, a
114 :exc:`ValueError` is raised.
115
116 If the class already defines any of :meth:`__lt__`,
117 :meth:`__le__`, :meth:`__gt__`, or :meth:`__ge__`, then
118 :exc:`ValueError` is raised.
119
120 - ``unsafe_hash``: If ``False`` (the default), the :meth:`__hash__` method
121 is generated according to how ``eq`` and ``frozen`` are set.
122
123 If ``eq`` and ``frozen`` are both true, :func:`dataclass` will
124 generate a :meth:`__hash__` method for you. If ``eq`` is true
125 and ``frozen`` is false, :meth:`__hash__` will be set to
126 ``None``, marking it unhashable (which it is, since it is
127 mutable). If ``eq`` is false, :meth:`__hash__` will be left
128 untouched meaning the :meth:`__hash__` method of the superclass
129 will be used (if the superclass is :class:`object`, this means it will
130 fall back to id-based hashing).
131
132 Although not recommended, you can force :func:`dataclass` to
133 create a :meth:`__hash__` method with ``unsafe_hash=True``. This
134 might be the case if your class is logically immutable but can
135 nonetheless be mutated. This is a specialized use case and should
136 be considered carefully.
137
138 If a class already has an explicitely defined :meth:`__hash__`
139 the behavior when adding :meth:`__hash__` is modified. An
140 expicitely defined :meth:`__hash__` is defined when:
141
142 - :meth:`__eq__` is defined in the class and :meth:`__hash__` is defined
143 with any value other than ``None``.
144
145 - :meth:`__eq__` is defined in the class and any non-``None``
146 :meth:`__hash__` is defined.
147
148 - :meth:`__eq__` is not defined on the class, and any :meth:`__hash__` is
149 defined.
150
151 If ``unsafe_hash`` is true and an explicitely defined :meth:`__hash__`
152 is present, then :exc:`ValueError` is raised.
153
154 If ``unsafe_hash`` is false and an explicitely defined :meth:`__hash__`
155 is present, then no :meth:`__hash__` is added.
156
157 See the Python documentation for more information.
158
159 - ``frozen``: If true (the default is False), assigning to fields will
160 generate an exception. This emulates read-only frozen instances.
161 If either :meth:`__getattr__` or :meth:`__setattr__` is defined in
162 the class, then :exc:`ValueError` is raised. See the discussion
163 below.
164
165 ``field``\s may optionally specify a default value, using normal
166 Python syntax::
167
168 @dataclass
169 class C:
170 a: int # 'a' has no default value
171 b: int = 0 # assign a default value for 'b'
172
173 In this example, both ``a`` and ``b`` will be included in the added
174 :meth:`__init__` method, which will be defined as::
175
176 def __init__(self, a: int, b: int = 0):
177
178 :exc:`TypeError` will be raised if a field without a default value
179 follows a field with a default value. This is true either when this
180 occurs in a single class, or as a result of class inheritance.
181
182.. function:: field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None)
183
184 For common and simple use cases, no other functionality is
185 required. There are, however, some Data Class features that
186 require additional per-field information. To satisfy this need for
187 additional information, you can replace the default field value
188 with a call to the provided :func:`field` function. For example::
189
190 @dataclass
191 class C:
192 l: List[int] = field(default_factory=list)
193
194 c = C()
195 c.l += [1, 2, 3]
196
197 As shown above, the ``MISSING`` value is a sentinel object used to
198 detect if the ``default`` and ``default_factory`` parameters are
199 provided. This sentinel is used because ``None`` is a valid value
200 for ``default``. No code should directly use the ``MISSING``
201 value.
202
203 The parameters to :func:`field` are:
204
205 - ``default``: If provided, this will be the default value for this
206 field. This is needed because the :meth:`field` call itself
207 replaces the normal position of the default value.
208
209 - ``default_factory``: If provided, it must be a zero-argument
210 callable that will be called when a default value is needed for
211 this field. Among other purposes, this can be used to specify
212 fields with mutable default values, as discussed below. It is an
213 error to specify both ``default`` and ``default_factory``.
214
215 - ``init``: If true (the default), this field is included as a
216 parameter to the generated :meth:`__init__` method.
217
218 - ``repr``: If true (the default), this field is included in the
219 string returned by the generated :meth:`__repr__` method.
220
221 - ``compare``: If true (the default), this field is included in the
222 generated equality and comparison methods (:meth:`__eq__`,
223 :meth:`__gt__`, et al.).
224
225 - ``hash``: This can be a bool or ``None``. If True, this field is
226 included in the generated :meth:`__hash__` method. If ``None`` (the
227 default), use the value of ``compare``: this would normally be
228 the expected behavior. A field should be considered in the hash
229 if it's used for comparisons. Setting this value to anything
230 other than ``None`` is discouraged.
231
232 One possible reason to set ``hash=False`` but ``compare=True``
233 would be if a field is expensive to compute a hash value for,
234 that field is needed for equality testing, and there are other
235 fields that contribute to the type's hash value. Even if a field
236 is excluded from the hash, it will still be used for comparisons.
237
238 - ``metadata``: This can be a mapping or None. None is treated as
239 an empty dict. This value is wrapped in
240 :func:`~types.MappingProxyType` to make it read-only, and exposed
241 on the :class:`Field` object. It is not used at all by Data
242 Classes, and is provided as a third-party extension mechanism.
243 Multiple third-parties can each have their own key, to use as a
244 namespace in the metadata.
245
246 If the default value of a field is specified by a call to
247 :func:`field()`, then the class attribute for this field will be
248 replaced by the specified ``default`` value. If no ``default`` is
249 provided, then the class attribute will be deleted. The intent is
250 that after the :func:`dataclass` decorator runs, the class
251 attributes will all contain the default values for the fields, just
252 as if the default value itself were specified. For example,
253 after::
254
255 @dataclass
256 class C:
257 x: int
258 y: int = field(repr=False)
259 z: int = field(repr=False, default=10)
260 t: int = 20
261
262 The class attribute ``C.z`` will be ``10``, the class attribute
263 ``C.t`` will be ``20``, and the class attributes ``C.x`` and
264 ``C.y`` will not be set.
265
266.. class:: Field
267
268 :class:`Field` objects describe each defined field. These objects
269 are created internally, and are returned by the :func:`fields`
270 module-level method (see below). Users should never instantiate a
271 :class:`Field` object directly. Its documented attributes are:
272
273 - ``name``: The name of the field.
274
275 - ``type``: The type of the field.
276
277 - ``default``, ``default_factory``, ``init``, ``repr``, ``hash``,
278 ``compare``, and ``metadata`` have the identical meaning and
279 values as they do in the :func:`field` declaration.
280
281 Other attributes may exist, but they are private and must not be
282 inspected or relied on.
283
284.. function:: fields(class_or_instance)
285
286 Returns a tuple of :class:`Field` objects
287 that define the fields for this Data Class. Accepts either a Data
288 Class, or an instance of a Data Class. Raises :exc:`ValueError` if
289 not passed a Data Class or instance of one. Does not return
290 pseudo-fields which are ``ClassVar`` or ``InitVar``.
291
292.. function:: asdict(instance, *, dict_factory=dict)
293
294 Converts the Data Class ``instance`` to a dict (by using the
295 factory function ``dict_factory``). Each Data Class is converted
296 to a dict of its fields, as ``name: value`` pairs. Data Classes, dicts,
297 lists, and tuples are recursed into. For example::
298
299 @dataclass
300 class Point:
301 x: int
302 y: int
303
304 @dataclass
305 class C:
306 l: List[Point]
307
308 p = Point(10, 20)
309 assert asdict(p) == {'x': 10, 'y': 20}
310
311 c = C([Point(0, 0), Point(10, 4)])
312 assert asdict(c) == {'l': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}
313
314 Raises :exc:`TypeError` if ``instance`` is not a Data Class instance.
315
316.. function:: astuple(*, tuple_factory=tuple)
317
318 Converts the Data Class ``instance`` to a tuple (by using the
319 factory function ``tuple_factory``). Each Data Class is converted
320 to a tuple of its field values. Data Classes, dicts, lists, and
321 tuples are recursed into.
322
323 Continuing from the previous example::
324
325 assert astuple(p) == (10, 20)
326 assert astuple(c) == ([(0, 0), (10, 4)],)
327
328 Raises :exc:`TypeError` if ``instance`` is not a Data Class instance.
329
330.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)
331
332 Creates a new Data Class with name ``cls_name``, fields as defined
333 in ``fields``, base classes as given in ``bases``, and initialized
334 with a namespace as given in ``namespace``. ``fields`` is an
335 iterable whose elements are each either ``name``, ``(name, type)``,
336 or ``(name, type, Field)``. If just ``name`` is supplied,
337 ``typing.Any`` is used for ``type``. The values of ``init``,
338 ``repr``, ``eq``, ``order``, ``unsafe_hash``, and ``frozen`` have
339 the same meaning as they do in :func:`dataclass`.
340
341 This function is not strictly required, because any Python
342 mechanism for creating a new class with ``__annotations__`` can
343 then apply the :func:`dataclass` function to convert that class to
344 a Data Class. This function is provided as a convenience. For
345 example::
346
347 C = make_dataclass('C',
348 [('x', int),
349 'y',
350 ('z', int, field(default=5))],
351 namespace={'add_one': lambda self: self.x + 1})
352
353 Is equivalent to::
354
355 @dataclass
356 class C:
357 x: int
358 y: 'typing.Any'
359 z: int = 5
360
361 def add_one(self):
362 return self.x + 1
363
364.. function:: replace(instance, **changes)
365
366 Creates a new object of the same type of ``instance``, replacing
367 fields with values from ``changes``. If ``instance`` is not a Data
368 Class, raises :exc:`TypeError`. If values in ``changes`` do not
369 specify fields, raises :exc:`TypeError`.
370
371 The newly returned object is created by calling the :meth:`__init__`
372 method of the Data Class. This ensures that
373 :meth:`__post_init__`, if present, is also called.
374
375 Init-only variables without default values, if any exist, must be
376 specified on the call to :func:`replace` so that they can be passed to
377 :meth:`__init__` and :meth:`__post_init__`.
378
379 It is an error for :func:`changes` to contain any fields that are
380 defined as having ``init=False``. A :exc:`ValueError` will be raised
381 in this case.
382
383 Be forewarned about how ``init=False`` fields work during a call to
384 :func:`replace`. They are not copied from the source object, but
385 rather are initialized in :meth:`__post_init__`, if they're
386 initialized at all. It is expected that ``init=False`` fields will
387 be rarely and judiciously used. If they are used, it might be wise
388 to have alternate class constructors, or perhaps a custom
389 ``replace()`` (or similarly named) method which handles instance
390 copying.
391
392.. function:: is_dataclass(class_or_instance)
393
394 Returns True if its parameter is a dataclass or an instance of one,
395 otherwise returns False.
396
397 If you need to know if a class is an instance of a dataclass (and
398 not a dataclass itself), then add a further check for ``not
399 isinstance(obj, type)``::
400
401 def is_dataclass_instance(obj):
402 return is_dataclass(obj) and not isinstance(obj, type)
403
404Post-init processing
405--------------------
406
407The generated :meth:`__init__` code will call a method named
408:meth:`__post_init__`, if :meth:`__post_init__` is defined on the
409class. It will normally be called as ``self.__post_init__()``.
410However, if any ``InitVar`` fields are defined, they will also be
411passed to :meth:`__post_init` in the order they were defined in the
412class. If no :meth:`__init__` method is generated, then
413:meth:`__post_init__` will not automatically be called.
414
415Among other uses, this allows for initializing field values that
416depend on one or more other fields. For example::
417
418 @dataclass
419 class C:
420 a: float
421 b: float
422 c: float = field(init=False)
423
424 def __post_init__(self):
425 self.c = self.a + self.b
426
427See the section below on init-only variables for ways to pass
428parameters to :meth:`__post_init__`. Also see the warning about how
429:func:`replace` handles ``init=False`` fields.
430
431Class variables
432---------------
433
434One of two places where :func:`dataclass` actually inspects the type
435of a field is to determine if a field is a class variable as defined
436in :pep:`526`. It does this by checking if the type of the field is
437``typing.ClassVar``. If a field is a ``ClassVar``, it is excluded
438from consideration as a field and is ignored by the Data Class
439mechanisms. Such ``ClassVar`` pseudo-fields are not returned by the
440module-level :func:`fields` function.
441
442Init-only variables
443-------------------
444
445The other place where :func:`dataclass` inspects a type annotation is to
446determine if a field is an init-only variable. It does this by seeing
447if the type of a field is of type ``dataclasses.InitVar``. If a field
448is an ``InitVar``, it is considered a pseudo-field called an init-only
449field. As it is not a true field, it is not returned by the
450module-level :func:`fields` function. Init-only fields are added as
451parameters to the generated :meth:`__init__` method, and are passed to
452the optional :meth:`__post_init__` method. They are not otherwise used
453by Data Classes.
454
455For example, suppose a field will be initialzed from a database, if a
456value is not provided when creating the class::
457
458 @dataclass
459 class C:
460 i: int
461 j: int = None
462 database: InitVar[DatabaseType] = None
463
464 def __post_init__(self, database):
465 if self.j is None and database is not None:
466 self.j = database.lookup('j')
467
468 c = C(10, database=my_database)
469
470In this case, :func:`fields` will return :class:`Field` objects for ``i`` and
471``j``, but not for ``database``.
472
473Frozen instances
474----------------
475
476It is not possible to create truly immutable Python objects. However,
477by passing ``frozen=True`` to the :meth:`dataclass` decorator you can
478emulate immutability. In that case, Data Classes will add
479:meth:`__setattr__` and :meth:`__delattr__` methods to the class. These
480methods will raise a :exc:`FrozenInstanceError` when invoked.
481
482There is a tiny performance penalty when using ``frozen=True``:
483:meth:`__init__` cannot use simple assignment to initialize fields, and
484must use :meth:`object.__setattr__`.
485
486Inheritance
487-----------
488
489When the Data Class is being created by the :meth:`dataclass` decorator,
490it looks through all of the class's base classes in reverse MRO (that
491is, starting at :class:`object`) and, for each Data Class that it finds,
492adds the fields from that base class to an ordered mapping of fields.
493After all of the base class fields are added, it adds its own fields
494to the ordered mapping. All of the generated methods will use this
495combined, calculated ordered mapping of fields. Because the fields
496are in insertion order, derived classes override base classes. An
497example::
498
499 @dataclass
500 class Base:
501 x: Any = 15.0
502 y: int = 0
503
504 @dataclass
505 class C(Base):
506 z: int = 10
507 x: int = 15
508
509The final list of fields is, in order, ``x``, ``y``, ``z``. The final
510type of ``x`` is ``int``, as specified in class ``C``.
511
512The generated :meth:`__init__` method for ``C`` will look like::
513
514 def __init__(self, x: int = 15, y: int = 0, z: int = 10):
515
516Default factory functions
517-------------------------
518
519 If a :func:`field` specifies a ``default_factory``, it is called with
520 zero arguments when a default value for the field is needed. For
521 example, to create a new instance of a list, use::
522
523 l: list = field(default_factory=list)
524
525 If a field is excluded from :meth:`__init__` (using ``init=False``)
526 and the field also specifies ``default_factory``, then the default
527 factory function will always be called from the generated
528 :meth:`__init__` function. This happens because there is no other
529 way to give the field an initial value.
530
531Mutable default values
532----------------------
533
534 Python stores default member variable values in class attributes.
535 Consider this example, not using Data Classes::
536
537 class C:
538 x = []
539 def add(self, element):
540 self.x += element
541
542 o1 = C()
543 o2 = C()
544 o1.add(1)
545 o2.add(2)
546 assert o1.x == [1, 2]
547 assert o1.x is o2.x
548
549 Note that the two instances of class ``C`` share the same class
550 variable ``x``, as expected.
551
552 Using Data Classes, *if* this code was valid::
553
554 @dataclass
555 class D:
556 x: List = []
557 def add(self, element):
558 self.x += element
559
560 it would generate code similar to::
561
562 class D:
563 x = []
564 def __init__(self, x=x):
565 self.x = x
566 def add(self, element):
567 self.x += element
568
569 assert D().x is D().x
570
571 This has the same issue as the original example using class ``C``.
572 That is, two instances of class ``D`` that do not specify a value for
573 ``x`` when creating a class instance will share the same copy of
574 ``x``. Because Data Classes just use normal Python class creation
575 they also share this problem. There is no general way for Data
576 Classes to detect this condition. Instead, Data Classes will raise a
577 :exc:`TypeError` if it detects a default parameter of type ``list``,
578 ``dict``, or ``set``. This is a partial solution, but it does protect
579 against many common errors.
580
581 Using default factory functions is a way to create new instances of
582 mutable types as default values for fields::
583
584 @dataclass
585 class D:
586 x: list = field(default_factory=list)
587
588 assert D().x is not D().x