blob: aa84f1b9533cb94cfefcaa6101a872cbf4f1fa52 [file] [log] [blame]
Eric V. Smith2a7bacb2018-05-15 22:44:27 -04001import re
Eric V. Smithf0db54a2017-12-04 16:58:55 -05002import sys
Eric V. Smithf96ddad2018-03-24 17:20:26 -04003import copy
Eric V. Smithf0db54a2017-12-04 16:58:55 -05004import types
Eric V. Smithf0db54a2017-12-04 16:58:55 -05005import inspect
Eric V. Smith4e812962018-05-16 11:31:29 -04006import keyword
Vadim Pushtaev4d12e4d2018-08-12 14:46:05 +03007import builtins
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +05308import functools
Ben Avrahamibef7d292020-10-06 20:40:50 +03009import abc
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +053010import _thread
Batuhan Taskayac7437e22020-10-21 16:49:22 +030011from types import FunctionType, GenericAlias
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +053012
Eric V. Smithf0db54a2017-12-04 16:58:55 -050013
14__all__ = ['dataclass',
15 'field',
Eric V. Smith8e4560a2018-03-21 17:10:22 -040016 'Field',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050017 'FrozenInstanceError',
18 'InitVar',
Eric V. Smithc0280532021-04-25 20:42:39 -040019 'KW_ONLY',
Eric V. Smith03220fd2017-12-29 13:59:58 -050020 'MISSING',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050021
22 # Helper functions.
23 'fields',
24 'asdict',
25 'astuple',
26 'make_dataclass',
27 'replace',
Eric V. Smithe7ba0132018-01-06 12:41:53 -050028 'is_dataclass',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050029 ]
30
Eric V. Smithea8fc522018-01-27 19:07:40 -050031# Conditions for adding methods. The boxes indicate what action the
Eric V. Smithf8e75492018-05-16 05:14:53 -040032# dataclass decorator takes. For all of these tables, when I talk
33# about init=, repr=, eq=, order=, unsafe_hash=, or frozen=, I'm
34# referring to the arguments to the @dataclass decorator. When
35# checking if a dunder method already exists, I mean check for an
36# entry in the class's __dict__. I never check to see if an attribute
37# is defined in a base class.
Eric V. Smithea8fc522018-01-27 19:07:40 -050038
39# Key:
40# +=========+=========================================+
41# + Value | Meaning |
42# +=========+=========================================+
43# | <blank> | No action: no method is added. |
44# +---------+-----------------------------------------+
45# | add | Generated method is added. |
46# +---------+-----------------------------------------+
Eric V. Smithea8fc522018-01-27 19:07:40 -050047# | raise | TypeError is raised. |
48# +---------+-----------------------------------------+
49# | None | Attribute is set to None. |
50# +=========+=========================================+
51
52# __init__
53#
54# +--- init= parameter
55# |
56# v | | |
57# | no | yes | <--- class has __init__ in __dict__?
58# +=======+=======+=======+
59# | False | | |
60# +-------+-------+-------+
61# | True | add | | <- the default
62# +=======+=======+=======+
63
64# __repr__
65#
66# +--- repr= parameter
67# |
68# v | | |
69# | no | yes | <--- class has __repr__ in __dict__?
70# +=======+=======+=======+
71# | False | | |
72# +-------+-------+-------+
73# | True | add | | <- the default
74# +=======+=======+=======+
75
76
77# __setattr__
78# __delattr__
79#
80# +--- frozen= parameter
81# |
82# v | | |
83# | no | yes | <--- class has __setattr__ or __delattr__ in __dict__?
84# +=======+=======+=======+
85# | False | | | <- the default
86# +-------+-------+-------+
87# | True | add | raise |
88# +=======+=======+=======+
89# Raise because not adding these methods would break the "frozen-ness"
Eric V. Smithf8e75492018-05-16 05:14:53 -040090# of the class.
Eric V. Smithea8fc522018-01-27 19:07:40 -050091
92# __eq__
93#
94# +--- eq= parameter
95# |
96# v | | |
97# | no | yes | <--- class has __eq__ in __dict__?
98# +=======+=======+=======+
99# | False | | |
100# +-------+-------+-------+
101# | True | add | | <- the default
102# +=======+=======+=======+
103
104# __lt__
105# __le__
106# __gt__
107# __ge__
108#
109# +--- order= parameter
110# |
111# v | | |
112# | no | yes | <--- class has any comparison method in __dict__?
113# +=======+=======+=======+
114# | False | | | <- the default
115# +-------+-------+-------+
116# | True | add | raise |
117# +=======+=======+=======+
118# Raise because to allow this case would interfere with using
Eric V. Smithf8e75492018-05-16 05:14:53 -0400119# functools.total_ordering.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500120
121# __hash__
122
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500123# +------------------- unsafe_hash= parameter
124# | +----------- eq= parameter
125# | | +--- frozen= parameter
126# | | |
127# v v v | | |
128# | no | yes | <--- class has explicitly defined __hash__
129# +=======+=======+=======+========+========+
130# | False | False | False | | | No __eq__, use the base class __hash__
131# +-------+-------+-------+--------+--------+
132# | False | False | True | | | No __eq__, use the base class __hash__
133# +-------+-------+-------+--------+--------+
134# | False | True | False | None | | <-- the default, not hashable
135# +-------+-------+-------+--------+--------+
136# | False | True | True | add | | Frozen, so hashable, allows override
137# +-------+-------+-------+--------+--------+
138# | True | False | False | add | raise | Has no __eq__, but hashable
139# +-------+-------+-------+--------+--------+
140# | True | False | True | add | raise | Has no __eq__, but hashable
141# +-------+-------+-------+--------+--------+
142# | True | True | False | add | raise | Not frozen, but hashable
143# +-------+-------+-------+--------+--------+
144# | True | True | True | add | raise | Frozen, so hashable
145# +=======+=======+=======+========+========+
Eric V. Smithea8fc522018-01-27 19:07:40 -0500146# For boxes that are blank, __hash__ is untouched and therefore
Eric V. Smithf8e75492018-05-16 05:14:53 -0400147# inherited from the base class. If the base is object, then
148# id-based hashing is used.
149#
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400150# Note that a class may already have __hash__=None if it specified an
Eric V. Smithf8e75492018-05-16 05:14:53 -0400151# __eq__ method in the class body (not one that was created by
152# @dataclass).
153#
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500154# See _hash_action (below) for a coded version of this table.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500155
Brandt Bucher145bf262021-02-26 14:51:55 -0800156# __match_args__
157#
Eric V. Smith750f4842021-04-10 21:28:42 -0400158# +--- match_args= parameter
159# |
160# v | | |
161# | no | yes | <--- class has __match_args__ in __dict__?
162# +=======+=======+=======+
163# | False | | |
164# +-------+-------+-------+
165# | True | add | | <- the default
166# +=======+=======+=======+
Eric V. Smithc0280532021-04-25 20:42:39 -0400167# __match_args__ is always added unless the class already defines it. It is a
168# tuple of __init__ parameter names; non-init fields must be matched by keyword.
Brandt Bucher145bf262021-02-26 14:51:55 -0800169
Eric V. Smithea8fc522018-01-27 19:07:40 -0500170
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500171# Raised when an attempt is made to modify a frozen class.
172class FrozenInstanceError(AttributeError): pass
173
Eric V. Smithf8e75492018-05-16 05:14:53 -0400174# A sentinel object for default values to signal that a default
175# factory will be used. This is given a nice repr() which will appear
176# in the function signature of dataclasses' constructors.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500177class _HAS_DEFAULT_FACTORY_CLASS:
178 def __repr__(self):
179 return '<factory>'
180_HAS_DEFAULT_FACTORY = _HAS_DEFAULT_FACTORY_CLASS()
181
Eric V. Smith03220fd2017-12-29 13:59:58 -0500182# A sentinel object to detect if a parameter is supplied or not. Use
Eric V. Smithf8e75492018-05-16 05:14:53 -0400183# a class to give it a better repr.
Eric V. Smith03220fd2017-12-29 13:59:58 -0500184class _MISSING_TYPE:
185 pass
186MISSING = _MISSING_TYPE()
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500187
Eric V. Smithc0280532021-04-25 20:42:39 -0400188# A sentinel object to indicate that following fields are keyword-only by
189# default. Use a class to give it a better repr.
190class _KW_ONLY_TYPE:
191 pass
192KW_ONLY = _KW_ONLY_TYPE()
193
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500194# Since most per-field metadata will be unused, create an empty
Eric V. Smithf8e75492018-05-16 05:14:53 -0400195# read-only proxy that can be shared among all fields.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500196_EMPTY_METADATA = types.MappingProxyType({})
197
198# Markers for the various kinds of fields and pseudo-fields.
Eric V. Smith01abc6e2018-05-15 08:36:21 -0400199class _FIELD_BASE:
200 def __init__(self, name):
201 self.name = name
202 def __repr__(self):
203 return self.name
204_FIELD = _FIELD_BASE('_FIELD')
205_FIELD_CLASSVAR = _FIELD_BASE('_FIELD_CLASSVAR')
206_FIELD_INITVAR = _FIELD_BASE('_FIELD_INITVAR')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500207
208# The name of an attribute on the class where we store the Field
Eric V. Smithf8e75492018-05-16 05:14:53 -0400209# objects. Also used to check if a class is a Data Class.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400210_FIELDS = '__dataclass_fields__'
211
212# The name of an attribute on the class that stores the parameters to
213# @dataclass.
214_PARAMS = '__dataclass_params__'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500215
216# The name of the function, that if it exists, is called at the end of
217# __init__.
218_POST_INIT_NAME = '__post_init__'
219
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400220# String regex that string annotations for ClassVar or InitVar must match.
221# Allows "identifier.identifier[" or "identifier[".
222# https://bugs.python.org/issue33453 for details.
223_MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500224
Serhiy Storchakab4d0b392019-09-22 13:32:41 +0300225class InitVar:
Augusto Hack01ee12b2019-06-02 23:14:48 -0300226 __slots__ = ('type', )
227
228 def __init__(self, type):
229 self.type = type
230
231 def __repr__(self):
Samuel Colvin793cb852019-10-13 12:45:36 +0100232 if isinstance(self.type, type):
233 type_name = self.type.__name__
234 else:
235 # typing objects, e.g. List[int]
236 type_name = repr(self.type)
237 return f'dataclasses.InitVar[{type_name}]'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500238
Serhiy Storchakab4d0b392019-09-22 13:32:41 +0300239 def __class_getitem__(cls, type):
240 return InitVar(type)
241
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500242# Instances of Field are only ever created from within this module,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400243# and only from the field() function, although Field instances are
244# exposed externally as (conceptually) read-only objects.
245#
246# name and type are filled in after the fact, not in __init__.
247# They're not known at the time this class is instantiated, but it's
248# convenient if they're available later.
249#
Eric V. Smithf199bc62018-03-18 20:40:34 -0400250# When cls._FIELDS is filled in with a list of Field objects, the name
Eric V. Smithf8e75492018-05-16 05:14:53 -0400251# and type fields will have been populated.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500252class Field:
253 __slots__ = ('name',
254 'type',
255 'default',
256 'default_factory',
257 'repr',
258 'hash',
259 'init',
260 'compare',
261 'metadata',
Eric V. Smithc0280532021-04-25 20:42:39 -0400262 'kw_only',
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500263 '_field_type', # Private: not to be used by user code.
264 )
265
266 def __init__(self, default, default_factory, init, repr, hash, compare,
Eric V. Smithc0280532021-04-25 20:42:39 -0400267 metadata, kw_only):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500268 self.name = None
269 self.type = None
270 self.default = default
271 self.default_factory = default_factory
272 self.init = init
273 self.repr = repr
274 self.hash = hash
275 self.compare = compare
276 self.metadata = (_EMPTY_METADATA
Christopher Huntb01786c2019-02-12 06:50:49 -0500277 if metadata is None else
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500278 types.MappingProxyType(metadata))
Eric V. Smithc0280532021-04-25 20:42:39 -0400279 self.kw_only = kw_only
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500280 self._field_type = None
281
282 def __repr__(self):
283 return ('Field('
284 f'name={self.name!r},'
Eric V. Smith2473eea2018-05-14 11:37:28 -0400285 f'type={self.type!r},'
286 f'default={self.default!r},'
287 f'default_factory={self.default_factory!r},'
288 f'init={self.init!r},'
289 f'repr={self.repr!r},'
290 f'hash={self.hash!r},'
291 f'compare={self.compare!r},'
Eric V. Smith01abc6e2018-05-15 08:36:21 -0400292 f'metadata={self.metadata!r},'
Eric V. Smithc0280532021-04-25 20:42:39 -0400293 f'kw_only={self.kw_only!r},'
Eric V. Smith01abc6e2018-05-15 08:36:21 -0400294 f'_field_type={self._field_type}'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500295 ')')
296
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400297 # This is used to support the PEP 487 __set_name__ protocol in the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400298 # case where we're using a field that contains a descriptor as a
Artjome55ca3f2018-07-06 02:09:13 +0300299 # default value. For details on __set_name__, see
Eric V. Smithf8e75492018-05-16 05:14:53 -0400300 # https://www.python.org/dev/peps/pep-0487/#implementation-details.
301 #
302 # Note that in _process_class, this Field object is overwritten
303 # with the default value, so the end result is a descriptor that
304 # had __set_name__ called on it at the right time.
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400305 def __set_name__(self, owner, name):
Eric V. Smith52199522018-03-29 11:07:48 -0400306 func = getattr(type(self.default), '__set_name__', None)
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400307 if func:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400308 # There is a __set_name__ method on the descriptor, call
309 # it.
Eric V. Smith52199522018-03-29 11:07:48 -0400310 func(self.default, owner, name)
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400311
Ethan Smithd01628e2020-04-14 16:14:15 -0700312 __class_getitem__ = classmethod(GenericAlias)
313
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500314
Eric V. Smithf199bc62018-03-18 20:40:34 -0400315class _DataclassParams:
316 __slots__ = ('init',
317 'repr',
318 'eq',
319 'order',
320 'unsafe_hash',
321 'frozen',
322 )
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400323
Eric V. Smithf199bc62018-03-18 20:40:34 -0400324 def __init__(self, init, repr, eq, order, unsafe_hash, frozen):
325 self.init = init
326 self.repr = repr
327 self.eq = eq
328 self.order = order
329 self.unsafe_hash = unsafe_hash
330 self.frozen = frozen
331
332 def __repr__(self):
333 return ('_DataclassParams('
Eric V. Smith30590422018-05-14 17:16:52 -0400334 f'init={self.init!r},'
335 f'repr={self.repr!r},'
336 f'eq={self.eq!r},'
337 f'order={self.order!r},'
338 f'unsafe_hash={self.unsafe_hash!r},'
339 f'frozen={self.frozen!r}'
Eric V. Smithf199bc62018-03-18 20:40:34 -0400340 ')')
341
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400342
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500343# This function is used instead of exposing Field creation directly,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400344# so that a type checker can be told (via overloads) that this is a
345# function whose type depends on its parameters.
Eric V. Smith03220fd2017-12-29 13:59:58 -0500346def field(*, default=MISSING, default_factory=MISSING, init=True, repr=True,
Eric V. Smithc0280532021-04-25 20:42:39 -0400347 hash=None, compare=True, metadata=None, kw_only=MISSING):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500348 """Return an object to identify dataclass fields.
349
Eric V. Smithf8e75492018-05-16 05:14:53 -0400350 default is the default value of the field. default_factory is a
351 0-argument function called to initialize a field's value. If init
Eric V. Smithc0280532021-04-25 20:42:39 -0400352 is true, the field will be a parameter to the class's __init__()
353 function. If repr is true, the field will be included in the
354 object's repr(). If hash is true, the field will be included in the
355 object's hash(). If compare is true, the field will be used in
356 comparison functions. metadata, if specified, must be a mapping
357 which is stored but not otherwise examined by dataclass. If kw_only
358 is true, the field will become a keyword-only parameter to
359 __init__().
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500360
361 It is an error to specify both default and default_factory.
362 """
363
Eric V. Smith03220fd2017-12-29 13:59:58 -0500364 if default is not MISSING and default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500365 raise ValueError('cannot specify both default and default_factory')
366 return Field(default, default_factory, init, repr, hash, compare,
Eric V. Smithc0280532021-04-25 20:42:39 -0400367 metadata, kw_only)
368
369
370def _fields_in_init_order(fields):
371 # Returns the fields as __init__ will output them. It returns 2 tuples:
372 # the first for normal args, and the second for keyword args.
373
374 return (tuple(f for f in fields if f.init and not f.kw_only),
375 tuple(f for f in fields if f.init and f.kw_only)
376 )
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500377
378
379def _tuple_str(obj_name, fields):
380 # Return a string representing each field of obj_name as a tuple
Eric V. Smithf8e75492018-05-16 05:14:53 -0400381 # member. So, if fields is ['x', 'y'] and obj_name is "self",
382 # return "(self.x,self.y)".
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500383
384 # Special case for the 0-tuple.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500385 if not fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500386 return '()'
387 # Note the trailing comma, needed if this turns out to be a 1-tuple.
388 return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)'
389
390
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +0530391# This function's logic is copied from "recursive_repr" function in
392# reprlib module to avoid dependency.
393def _recursive_repr(user_function):
394 # Decorator to make a repr function return "..." for a recursive
395 # call.
396 repr_running = set()
397
398 @functools.wraps(user_function)
399 def wrapper(self):
400 key = id(self), _thread.get_ident()
401 if key in repr_running:
402 return '...'
403 repr_running.add(key)
404 try:
405 result = user_function(self)
406 finally:
407 repr_running.discard(key)
408 return result
409 return wrapper
410
411
Eric V. Smithea8fc522018-01-27 19:07:40 -0500412def _create_fn(name, args, body, *, globals=None, locals=None,
Eric V. Smith03220fd2017-12-29 13:59:58 -0500413 return_type=MISSING):
Eric V. Smithf8e75492018-05-16 05:14:53 -0400414 # Note that we mutate locals when exec() is called. Caller
415 # beware! The only callers are internal to this module, so no
416 # worries about external callers.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500417 if locals is None:
418 locals = {}
Yury Selivanovd219cc42019-12-09 09:54:20 -0500419 if 'BUILTINS' not in locals:
420 locals['BUILTINS'] = builtins
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500421 return_annotation = ''
Eric V. Smith03220fd2017-12-29 13:59:58 -0500422 if return_type is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500423 locals['_return_type'] = return_type
424 return_annotation = '->_return_type'
425 args = ','.join(args)
Yury Selivanovd219cc42019-12-09 09:54:20 -0500426 body = '\n'.join(f' {b}' for b in body)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500427
Eric V. Smithf199bc62018-03-18 20:40:34 -0400428 # Compute the text of the entire function.
Yury Selivanovd219cc42019-12-09 09:54:20 -0500429 txt = f' def {name}({args}){return_annotation}:\n{body}'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500430
Yury Selivanovd219cc42019-12-09 09:54:20 -0500431 local_vars = ', '.join(locals.keys())
432 txt = f"def __create_fn__({local_vars}):\n{txt}\n return {name}"
Yury Selivanovd219cc42019-12-09 09:54:20 -0500433 ns = {}
434 exec(txt, globals, ns)
Pablo Galindob0544ba2021-04-21 12:41:19 +0100435 return ns['__create_fn__'](**locals)
436
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500437
438def _field_assign(frozen, name, value, self_name):
439 # If we're a frozen class, then assign to our fields in __init__
Eric V. Smithf8e75492018-05-16 05:14:53 -0400440 # via object.__setattr__. Otherwise, just use a simple
441 # assignment.
442 #
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500443 # self_name is what "self" is called in this function: don't
Eric V. Smithf8e75492018-05-16 05:14:53 -0400444 # hard-code "self", since that might be a field name.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500445 if frozen:
Yury Selivanovd219cc42019-12-09 09:54:20 -0500446 return f'BUILTINS.object.__setattr__({self_name},{name!r},{value})'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500447 return f'{self_name}.{name}={value}'
448
449
450def _field_init(f, frozen, globals, self_name):
451 # Return the text of the line in the body of __init__ that will
Eric V. Smithf8e75492018-05-16 05:14:53 -0400452 # initialize this field.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500453
454 default_name = f'_dflt_{f.name}'
Eric V. Smith03220fd2017-12-29 13:59:58 -0500455 if f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500456 if f.init:
457 # This field has a default factory. If a parameter is
Eric V. Smithf8e75492018-05-16 05:14:53 -0400458 # given, use it. If not, call the factory.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500459 globals[default_name] = f.default_factory
460 value = (f'{default_name}() '
461 f'if {f.name} is _HAS_DEFAULT_FACTORY '
462 f'else {f.name}')
463 else:
464 # This is a field that's not in the __init__ params, but
Eric V. Smithf8e75492018-05-16 05:14:53 -0400465 # has a default factory function. It needs to be
466 # initialized here by calling the factory function,
467 # because there's no other way to initialize it.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500468
469 # For a field initialized with a default=defaultvalue, the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400470 # class dict just has the default value
471 # (cls.fieldname=defaultvalue). But that won't work for a
472 # default factory, the factory must be called in __init__
473 # and we must assign that to self.fieldname. We can't
474 # fall back to the class dict's value, both because it's
475 # not set, and because it might be different per-class
476 # (which, after all, is why we have a factory function!).
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500477
478 globals[default_name] = f.default_factory
479 value = f'{default_name}()'
480 else:
481 # No default factory.
482 if f.init:
Eric V. Smith03220fd2017-12-29 13:59:58 -0500483 if f.default is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500484 # There's no default, just do an assignment.
485 value = f.name
Eric V. Smith03220fd2017-12-29 13:59:58 -0500486 elif f.default is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500487 globals[default_name] = f.default
488 value = f.name
489 else:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400490 # This field does not need initialization. Signify that
491 # to the caller by returning None.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500492 return None
493
494 # Only test this now, so that we can create variables for the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400495 # default. However, return None to signify that we're not going
496 # to actually do the assignment statement for InitVars.
Eric V. Smithe7adf2b2018-06-07 14:43:59 -0400497 if f._field_type is _FIELD_INITVAR:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500498 return None
499
500 # Now, actually generate the field assignment.
501 return _field_assign(frozen, f.name, value, self_name)
502
503
504def _init_param(f):
Eric V. Smithf8e75492018-05-16 05:14:53 -0400505 # Return the __init__ parameter string for this field. For
506 # example, the equivalent of 'x:int=3' (except instead of 'int',
507 # reference a variable set to int, and instead of '3', reference a
508 # variable set to 3).
Eric V. Smith03220fd2017-12-29 13:59:58 -0500509 if f.default is MISSING and f.default_factory is MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400510 # There's no default, and no default_factory, just output the
511 # variable name and type.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500512 default = ''
Eric V. Smith03220fd2017-12-29 13:59:58 -0500513 elif f.default is not MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400514 # There's a default, this will be the name that's used to look
515 # it up.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500516 default = f'=_dflt_{f.name}'
Eric V. Smith03220fd2017-12-29 13:59:58 -0500517 elif f.default_factory is not MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400518 # There's a factory function. Set a marker.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500519 default = '=_HAS_DEFAULT_FACTORY'
520 return f'{f.name}:_type_{f.name}{default}'
521
522
Eric V. Smithc0280532021-04-25 20:42:39 -0400523def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,
524 self_name, globals):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500525 # fields contains both real fields and InitVar pseudo-fields.
526
527 # Make sure we don't have fields without defaults following fields
Eric V. Smithf8e75492018-05-16 05:14:53 -0400528 # with defaults. This actually would be caught when exec-ing the
529 # function source code, but catching it here gives a better error
530 # message, and future-proofs us in case we build up the function
531 # using ast.
Eric V. Smithc0280532021-04-25 20:42:39 -0400532
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500533 seen_default = False
Eric V. Smithc0280532021-04-25 20:42:39 -0400534 for f in std_fields:
535 # Only consider the non-kw-only fields in the __init__ call.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500536 if f.init:
Eric V. Smith03220fd2017-12-29 13:59:58 -0500537 if not (f.default is MISSING and f.default_factory is MISSING):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500538 seen_default = True
539 elif seen_default:
540 raise TypeError(f'non-default argument {f.name!r} '
541 'follows default argument')
542
Yury Selivanovd219cc42019-12-09 09:54:20 -0500543 locals = {f'_type_{f.name}': f.type for f in fields}
544 locals.update({
545 'MISSING': MISSING,
546 '_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY,
547 })
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500548
549 body_lines = []
550 for f in fields:
Yury Selivanovd219cc42019-12-09 09:54:20 -0500551 line = _field_init(f, frozen, locals, self_name)
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400552 # line is None means that this field doesn't require
Eric V. Smithf8e75492018-05-16 05:14:53 -0400553 # initialization (it's a pseudo-field). Just skip it.
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400554 if line:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500555 body_lines.append(line)
556
557 # Does this class have a post-init function?
558 if has_post_init:
559 params_str = ','.join(f.name for f in fields
560 if f._field_type is _FIELD_INITVAR)
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400561 body_lines.append(f'{self_name}.{_POST_INIT_NAME}({params_str})')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500562
563 # If no body lines, use 'pass'.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500564 if not body_lines:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500565 body_lines = ['pass']
566
Eric V. Smithc0280532021-04-25 20:42:39 -0400567 _init_params = [_init_param(f) for f in std_fields]
568 if kw_only_fields:
569 # Add the keyword-only args. Because the * can only be added if
570 # there's at least one keyword-only arg, there needs to be a test here
571 # (instead of just concatenting the lists together).
572 _init_params += ['*']
573 _init_params += [_init_param(f) for f in kw_only_fields]
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500574 return _create_fn('__init__',
Eric V. Smithc0280532021-04-25 20:42:39 -0400575 [self_name] + _init_params,
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500576 body_lines,
577 locals=locals,
578 globals=globals,
579 return_type=None)
580
581
Yury Selivanovd219cc42019-12-09 09:54:20 -0500582def _repr_fn(fields, globals):
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +0530583 fn = _create_fn('__repr__',
584 ('self',),
585 ['return self.__class__.__qualname__ + f"(' +
586 ', '.join([f"{f.name}={{self.{f.name}!r}}"
587 for f in fields]) +
Yury Selivanovd219cc42019-12-09 09:54:20 -0500588 ')"'],
589 globals=globals)
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +0530590 return _recursive_repr(fn)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500591
592
Yury Selivanovd219cc42019-12-09 09:54:20 -0500593def _frozen_get_del_attr(cls, fields, globals):
594 locals = {'cls': cls,
Eric V. Smithf199bc62018-03-18 20:40:34 -0400595 'FrozenInstanceError': FrozenInstanceError}
596 if fields:
597 fields_str = '(' + ','.join(repr(f.name) for f in fields) + ',)'
598 else:
599 # Special case for the zero-length tuple.
600 fields_str = '()'
601 return (_create_fn('__setattr__',
602 ('self', 'name', 'value'),
603 (f'if type(self) is cls or name in {fields_str}:',
604 ' raise FrozenInstanceError(f"cannot assign to field {name!r}")',
605 f'super(cls, self).__setattr__(name, value)'),
Yury Selivanovd219cc42019-12-09 09:54:20 -0500606 locals=locals,
Eric V. Smithf199bc62018-03-18 20:40:34 -0400607 globals=globals),
608 _create_fn('__delattr__',
609 ('self', 'name'),
610 (f'if type(self) is cls or name in {fields_str}:',
611 ' raise FrozenInstanceError(f"cannot delete field {name!r}")',
612 f'super(cls, self).__delattr__(name)'),
Yury Selivanovd219cc42019-12-09 09:54:20 -0500613 locals=locals,
Eric V. Smithf199bc62018-03-18 20:40:34 -0400614 globals=globals),
615 )
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500616
617
Yury Selivanovd219cc42019-12-09 09:54:20 -0500618def _cmp_fn(name, op, self_tuple, other_tuple, globals):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500619 # Create a comparison function. If the fields in the object are
Eric V. Smithf8e75492018-05-16 05:14:53 -0400620 # named 'x' and 'y', then self_tuple is the string
621 # '(self.x,self.y)' and other_tuple is the string
622 # '(other.x,other.y)'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500623
624 return _create_fn(name,
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400625 ('self', 'other'),
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500626 [ 'if other.__class__ is self.__class__:',
627 f' return {self_tuple}{op}{other_tuple}',
Yury Selivanovd219cc42019-12-09 09:54:20 -0500628 'return NotImplemented'],
629 globals=globals)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500630
631
Yury Selivanovd219cc42019-12-09 09:54:20 -0500632def _hash_fn(fields, globals):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500633 self_tuple = _tuple_str('self', fields)
634 return _create_fn('__hash__',
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400635 ('self',),
Yury Selivanovd219cc42019-12-09 09:54:20 -0500636 [f'return hash({self_tuple})'],
637 globals=globals)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500638
639
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400640def _is_classvar(a_type, typing):
Eric V. Smith92858352018-05-16 07:24:00 -0400641 # This test uses a typing internal class, but it's the best way to
642 # test if this is a ClassVar.
643 return (a_type is typing.ClassVar
644 or (type(a_type) is typing._GenericAlias
645 and a_type.__origin__ is typing.ClassVar))
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400646
647
648def _is_initvar(a_type, dataclasses):
649 # The module we're checking against is the module we're
650 # currently in (dataclasses.py).
Augusto Hack01ee12b2019-06-02 23:14:48 -0300651 return (a_type is dataclasses.InitVar
652 or type(a_type) is dataclasses.InitVar)
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400653
Eric V. Smithc0280532021-04-25 20:42:39 -0400654def _is_kw_only(a_type, dataclasses):
655 return a_type is dataclasses.KW_ONLY
656
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400657
658def _is_type(annotation, cls, a_module, a_type, is_type_predicate):
659 # Given a type annotation string, does it refer to a_type in
660 # a_module? For example, when checking that annotation denotes a
661 # ClassVar, then a_module is typing, and a_type is
662 # typing.ClassVar.
663
664 # It's possible to look up a_module given a_type, but it involves
665 # looking in sys.modules (again!), and seems like a waste since
666 # the caller already knows a_module.
667
668 # - annotation is a string type annotation
669 # - cls is the class that this annotation was found in
670 # - a_module is the module we want to match
671 # - a_type is the type in that module we want to match
672 # - is_type_predicate is a function called with (obj, a_module)
673 # that determines if obj is of the desired type.
674
675 # Since this test does not do a local namespace lookup (and
676 # instead only a module (global) lookup), there are some things it
677 # gets wrong.
678
Eric V. Smithf8e75492018-05-16 05:14:53 -0400679 # With string annotations, cv0 will be detected as a ClassVar:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400680 # CV = ClassVar
681 # @dataclass
682 # class C0:
683 # cv0: CV
684
Eric V. Smithf8e75492018-05-16 05:14:53 -0400685 # But in this example cv1 will not be detected as a ClassVar:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400686 # @dataclass
687 # class C1:
688 # CV = ClassVar
689 # cv1: CV
690
Eric V. Smithf8e75492018-05-16 05:14:53 -0400691 # In C1, the code in this function (_is_type) will look up "CV" in
692 # the module and not find it, so it will not consider cv1 as a
693 # ClassVar. This is a fairly obscure corner case, and the best
694 # way to fix it would be to eval() the string "CV" with the
695 # correct global and local namespaces. However that would involve
696 # a eval() penalty for every single field of every dataclass
697 # that's defined. It was judged not worth it.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400698
699 match = _MODULE_IDENTIFIER_RE.match(annotation)
700 if match:
701 ns = None
702 module_name = match.group(1)
703 if not module_name:
704 # No module name, assume the class's module did
705 # "from dataclasses import InitVar".
706 ns = sys.modules.get(cls.__module__).__dict__
707 else:
708 # Look up module_name in the class's module.
709 module = sys.modules.get(cls.__module__)
710 if module and module.__dict__.get(module_name) is a_module:
711 ns = sys.modules.get(a_type.__module__).__dict__
712 if ns and is_type_predicate(ns.get(match.group(2)), a_module):
713 return True
714 return False
715
716
Eric V. Smithc0280532021-04-25 20:42:39 -0400717def _get_field(cls, a_name, a_type, default_kw_only):
718 # Return a Field object for this field name and type. ClassVars and
719 # InitVars are also returned, but marked as such (see f._field_type).
720 # default_kw_only is the value of kw_only to use if there isn't a field()
721 # that defines it.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500722
Eric V. Smithf8e75492018-05-16 05:14:53 -0400723 # If the default value isn't derived from Field, then it's only a
724 # normal default value. Convert it to a Field().
Eric V. Smith03220fd2017-12-29 13:59:58 -0500725 default = getattr(cls, a_name, MISSING)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500726 if isinstance(default, Field):
727 f = default
728 else:
Eric V. Smith7389fd92018-03-19 21:07:51 -0400729 if isinstance(default, types.MemberDescriptorType):
730 # This is a field in __slots__, so it has no default value.
731 default = MISSING
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500732 f = field(default=default)
733
Eric V. Smithf8e75492018-05-16 05:14:53 -0400734 # Only at this point do we know the name and the type. Set them.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500735 f.name = a_name
736 f.type = a_type
737
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400738 # Assume it's a normal field until proven otherwise. We're next
Eric V. Smithf8e75492018-05-16 05:14:53 -0400739 # going to decide if it's a ClassVar or InitVar, everything else
740 # is just a normal field.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400741 f._field_type = _FIELD
742
743 # In addition to checking for actual types here, also check for
Eric V. Smithf8e75492018-05-16 05:14:53 -0400744 # string annotations. get_type_hints() won't always work for us
745 # (see https://github.com/python/typing/issues/508 for example),
Eric V. Smith76beadb2021-04-17 09:53:24 -0400746 # plus it's expensive and would require an eval for every string
Eric V. Smithf8e75492018-05-16 05:14:53 -0400747 # annotation. So, make a best effort to see if this is a ClassVar
748 # or InitVar using regex's and checking that the thing referenced
749 # is actually of the correct type.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400750
751 # For the complete discussion, see https://bugs.python.org/issue33453
752
753 # If typing has not been imported, then it's impossible for any
Eric V. Smithf8e75492018-05-16 05:14:53 -0400754 # annotation to be a ClassVar. So, only look for ClassVar if
755 # typing has been imported by any module (not necessarily cls's
756 # module).
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500757 typing = sys.modules.get('typing')
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400758 if typing:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400759 if (_is_classvar(a_type, typing)
760 or (isinstance(f.type, str)
761 and _is_type(f.type, cls, typing, typing.ClassVar,
762 _is_classvar))):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500763 f._field_type = _FIELD_CLASSVAR
764
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400765 # If the type is InitVar, or if it's a matching string annotation,
766 # then it's an InitVar.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500767 if f._field_type is _FIELD:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400768 # The module we're checking against is the module we're
769 # currently in (dataclasses.py).
770 dataclasses = sys.modules[__name__]
771 if (_is_initvar(a_type, dataclasses)
772 or (isinstance(f.type, str)
773 and _is_type(f.type, cls, dataclasses, dataclasses.InitVar,
774 _is_initvar))):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500775 f._field_type = _FIELD_INITVAR
776
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400777 # Validations for individual fields. This is delayed until now,
778 # instead of in the Field() constructor, since only here do we
779 # know the field name, which allows for better error reporting.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500780
781 # Special restrictions for ClassVar and InitVar.
782 if f._field_type in (_FIELD_CLASSVAR, _FIELD_INITVAR):
Eric V. Smith03220fd2017-12-29 13:59:58 -0500783 if f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500784 raise TypeError(f'field {f.name} cannot have a '
785 'default factory')
786 # Should I check for other field settings? default_factory
Eric V. Smithf8e75492018-05-16 05:14:53 -0400787 # seems the most serious to check for. Maybe add others. For
788 # example, how about init=False (or really,
789 # init=<not-the-default-init-value>)? It makes no sense for
790 # ClassVar and InitVar to specify init=<anything>.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500791
Eric V. Smithc0280532021-04-25 20:42:39 -0400792 # kw_only validation and assignment.
793 if f._field_type in (_FIELD, _FIELD_INITVAR):
794 # For real and InitVar fields, if kw_only wasn't specified use the
795 # default value.
796 if f.kw_only is MISSING:
797 f.kw_only = default_kw_only
798 else:
799 # Make sure kw_only isn't set for ClassVars
800 assert f._field_type is _FIELD_CLASSVAR
801 if f.kw_only is not MISSING:
802 raise TypeError(f'field {f.name} is a ClassVar but specifies '
803 'kw_only')
804
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500805 # For real fields, disallow mutable defaults for known types.
806 if f._field_type is _FIELD and isinstance(f.default, (list, dict, set)):
807 raise ValueError(f'mutable default {type(f.default)} for field '
808 f'{f.name} is not allowed: use default_factory')
809
810 return f
811
Batuhan Taskayac7437e22020-10-21 16:49:22 +0300812def _set_qualname(cls, value):
813 # Ensure that the functions returned from _create_fn uses the proper
814 # __qualname__ (the class they belong to).
815 if isinstance(value, FunctionType):
816 value.__qualname__ = f"{cls.__qualname__}.{value.__name__}"
817 return value
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500818
Eric V. Smithea8fc522018-01-27 19:07:40 -0500819def _set_new_attribute(cls, name, value):
820 # Never overwrites an existing attribute. Returns True if the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400821 # attribute already exists.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500822 if name in cls.__dict__:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500823 return True
Batuhan Taskayac7437e22020-10-21 16:49:22 +0300824 _set_qualname(cls, value)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500825 setattr(cls, name, value)
Eric V. Smithea8fc522018-01-27 19:07:40 -0500826 return False
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500827
828
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500829# Decide if/how we're going to create a hash function. Key is
Eric V. Smithf8e75492018-05-16 05:14:53 -0400830# (unsafe_hash, eq, frozen, does-hash-exist). Value is the action to
831# take. The common case is to do nothing, so instead of providing a
832# function that is a no-op, use None to signify that.
Eric V. Smith01d618c2018-03-24 22:10:14 -0400833
Yury Selivanovd219cc42019-12-09 09:54:20 -0500834def _hash_set_none(cls, fields, globals):
Eric V. Smith01d618c2018-03-24 22:10:14 -0400835 return None
836
Yury Selivanovd219cc42019-12-09 09:54:20 -0500837def _hash_add(cls, fields, globals):
Eric V. Smith01d618c2018-03-24 22:10:14 -0400838 flds = [f for f in fields if (f.compare if f.hash is None else f.hash)]
Batuhan Taskayac7437e22020-10-21 16:49:22 +0300839 return _set_qualname(cls, _hash_fn(flds, globals))
Eric V. Smith01d618c2018-03-24 22:10:14 -0400840
Yury Selivanovd219cc42019-12-09 09:54:20 -0500841def _hash_exception(cls, fields, globals):
Eric V. Smith01d618c2018-03-24 22:10:14 -0400842 # Raise an exception.
843 raise TypeError(f'Cannot overwrite attribute __hash__ '
844 f'in class {cls.__name__}')
845
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500846#
847# +-------------------------------------- unsafe_hash?
848# | +------------------------------- eq?
849# | | +------------------------ frozen?
850# | | | +---------------- has-explicit-hash?
851# | | | |
852# | | | | +------- action
853# | | | | |
854# v v v v v
Eric V. Smith01d618c2018-03-24 22:10:14 -0400855_hash_action = {(False, False, False, False): None,
856 (False, False, False, True ): None,
857 (False, False, True, False): None,
858 (False, False, True, True ): None,
859 (False, True, False, False): _hash_set_none,
860 (False, True, False, True ): None,
861 (False, True, True, False): _hash_add,
862 (False, True, True, True ): None,
863 (True, False, False, False): _hash_add,
864 (True, False, False, True ): _hash_exception,
865 (True, False, True, False): _hash_add,
866 (True, False, True, True ): _hash_exception,
867 (True, True, False, False): _hash_add,
868 (True, True, False, True ): _hash_exception,
869 (True, True, True, False): _hash_add,
870 (True, True, True, True ): _hash_exception,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500871 }
872# See https://bugs.python.org/issue32929#msg312829 for an if-statement
Eric V. Smithf8e75492018-05-16 05:14:53 -0400873# version of this table.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500874
875
Eric V. Smith750f4842021-04-10 21:28:42 -0400876def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
Yurii Karabasc2419912021-05-01 05:14:30 +0300877 match_args, kw_only, slots):
Eric V. Smithd1388922018-01-07 14:30:17 -0500878 # Now that dicts retain insertion order, there's no reason to use
Eric V. Smithf8e75492018-05-16 05:14:53 -0400879 # an ordered dict. I am leveraging that ordering here, because
880 # derived class fields overwrite base class fields, but the order
881 # is defined by the base class, which is found first.
Eric V. Smithd1388922018-01-07 14:30:17 -0500882 fields = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500883
Yury Selivanovd219cc42019-12-09 09:54:20 -0500884 if cls.__module__ in sys.modules:
885 globals = sys.modules[cls.__module__].__dict__
886 else:
887 # Theoretically this can happen if someone writes
888 # a custom string to cls.__module__. In which case
889 # such dataclass won't be fully introspectable
890 # (w.r.t. typing.get_type_hints) but will still function
891 # correctly.
892 globals = {}
893
Eric V. Smithf199bc62018-03-18 20:40:34 -0400894 setattr(cls, _PARAMS, _DataclassParams(init, repr, eq, order,
895 unsafe_hash, frozen))
896
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500897 # Find our base classes in reverse MRO order, and exclude
Eric V. Smithf8e75492018-05-16 05:14:53 -0400898 # ourselves. In reversed order so that more derived classes
899 # override earlier field definitions in base classes. As long as
900 # we're iterating over them, see if any are frozen.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400901 any_frozen_base = False
902 has_dataclass_bases = False
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500903 for b in cls.__mro__[-1:0:-1]:
904 # Only process classes that have been processed by our
Eric V. Smithf8e75492018-05-16 05:14:53 -0400905 # decorator. That is, they have a _FIELDS attribute.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400906 base_fields = getattr(b, _FIELDS, None)
Iurii Kemaev376ffc62021-04-06 06:14:01 +0100907 if base_fields is not None:
Eric V. Smithf199bc62018-03-18 20:40:34 -0400908 has_dataclass_bases = True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500909 for f in base_fields.values():
910 fields[f.name] = f
Eric V. Smithf199bc62018-03-18 20:40:34 -0400911 if getattr(b, _PARAMS).frozen:
912 any_frozen_base = True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500913
Eric V. Smith56970b82018-03-22 16:28:48 -0400914 # Annotations that are defined in this class (not in base
Eric V. Smithf8e75492018-05-16 05:14:53 -0400915 # classes). If __annotations__ isn't present, then this class
916 # adds no new annotations. We use this to compute fields that are
917 # added by this class.
918 #
Eric V. Smith56970b82018-03-22 16:28:48 -0400919 # Fields are found from cls_annotations, which is guaranteed to be
Eric V. Smithf8e75492018-05-16 05:14:53 -0400920 # ordered. Default values are from class attributes, if a field
921 # has a default. If the default value is a Field(), then it
922 # contains additional info beyond (and possibly including) the
923 # actual default value. Pseudo-fields ClassVars and InitVars are
924 # included, despite the fact that they're not real fields. That's
925 # dealt with later.
Eric V. Smith56970b82018-03-22 16:28:48 -0400926 cls_annotations = cls.__dict__.get('__annotations__', {})
927
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500928 # Now find fields in our class. While doing so, validate some
Eric V. Smithf8e75492018-05-16 05:14:53 -0400929 # things, and set the default values (as class attributes) where
930 # we can.
Eric V. Smithc0280532021-04-25 20:42:39 -0400931 cls_fields = []
932 # Get a reference to this module for the _is_kw_only() test.
Eric V. Smith99ad7422021-05-03 03:24:53 -0400933 KW_ONLY_seen = False
Eric V. Smithc0280532021-04-25 20:42:39 -0400934 dataclasses = sys.modules[__name__]
935 for name, type in cls_annotations.items():
936 # See if this is a marker to change the value of kw_only.
937 if (_is_kw_only(type, dataclasses)
938 or (isinstance(type, str)
939 and _is_type(type, cls, dataclasses, dataclasses.KW_ONLY,
940 _is_kw_only))):
941 # Switch the default to kw_only=True, and ignore this
942 # annotation: it's not a real field.
Eric V. Smith99ad7422021-05-03 03:24:53 -0400943 if KW_ONLY_seen:
944 raise TypeError(f'{name!r} is KW_ONLY, but KW_ONLY '
945 'has already been specified')
946 KW_ONLY_seen = True
Eric V. Smithc0280532021-04-25 20:42:39 -0400947 kw_only = True
948 else:
949 # Otherwise it's a field of some type.
950 cls_fields.append(_get_field(cls, name, type, kw_only))
951
Eric V. Smith56970b82018-03-22 16:28:48 -0400952 for f in cls_fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500953 fields[f.name] = f
954
Eric V. Smithf8e75492018-05-16 05:14:53 -0400955 # If the class attribute (which is the default value for this
956 # field) exists and is of type 'Field', replace it with the
957 # real default. This is so that normal class introspection
958 # sees a real default value, not a Field.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500959 if isinstance(getattr(cls, f.name, None), Field):
Eric V. Smith03220fd2017-12-29 13:59:58 -0500960 if f.default is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500961 # If there's no default, delete the class attribute.
Eric V. Smithf8e75492018-05-16 05:14:53 -0400962 # This happens if we specify field(repr=False), for
963 # example (that is, we specified a field object, but
964 # no default value). Also if we're using a default
965 # factory. The class attribute should not be set at
966 # all in the post-processed class.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500967 delattr(cls, f.name)
968 else:
969 setattr(cls, f.name, f.default)
970
Eric V. Smith56970b82018-03-22 16:28:48 -0400971 # Do we have any Field members that don't also have annotations?
972 for name, value in cls.__dict__.items():
973 if isinstance(value, Field) and not name in cls_annotations:
974 raise TypeError(f'{name!r} is a field but has no type annotation')
975
Eric V. Smithf199bc62018-03-18 20:40:34 -0400976 # Check rules that apply if we are derived from any dataclasses.
977 if has_dataclass_bases:
978 # Raise an exception if any of our bases are frozen, but we're not.
979 if any_frozen_base and not frozen:
980 raise TypeError('cannot inherit non-frozen dataclass from a '
981 'frozen one')
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500982
Eric V. Smithf199bc62018-03-18 20:40:34 -0400983 # Raise an exception if we're frozen, but none of our bases are.
984 if not any_frozen_base and frozen:
985 raise TypeError('cannot inherit frozen dataclass from a '
986 'non-frozen one')
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500987
Eric V. Smithf8e75492018-05-16 05:14:53 -0400988 # Remember all of the fields on our class (including bases). This
989 # also marks this class as being a dataclass.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400990 setattr(cls, _FIELDS, fields)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500991
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500992 # Was this class defined with an explicit __hash__? Note that if
Eric V. Smithf8e75492018-05-16 05:14:53 -0400993 # __eq__ is defined in this class, then python will automatically
994 # set __hash__ to None. This is a heuristic, as it's possible
995 # that such a __hash__ == None was not auto-generated, but it
996 # close enough.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500997 class_hash = cls.__dict__.get('__hash__', MISSING)
998 has_explicit_hash = not (class_hash is MISSING or
999 (class_hash is None and '__eq__' in cls.__dict__))
Eric V. Smithea8fc522018-01-27 19:07:40 -05001000
Eric V. Smithf8e75492018-05-16 05:14:53 -04001001 # If we're generating ordering methods, we must be generating the
1002 # eq methods.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001003 if order and not eq:
1004 raise ValueError('eq must be true if order is true')
1005
Eric V. Smithc0280532021-04-25 20:42:39 -04001006 # Include InitVars and regular fields (so, not ClassVars). This is
1007 # initialized here, outside of the "if init:" test, because std_init_fields
1008 # is used with match_args, below.
1009 all_init_fields = [f for f in fields.values()
1010 if f._field_type in (_FIELD, _FIELD_INITVAR)]
1011 (std_init_fields,
1012 kw_only_init_fields) = _fields_in_init_order(all_init_fields)
1013
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001014 if init:
1015 # Does this class have a post-init function?
1016 has_post_init = hasattr(cls, _POST_INIT_NAME)
1017
Eric V. Smithea8fc522018-01-27 19:07:40 -05001018 _set_new_attribute(cls, '__init__',
Eric V. Smithc0280532021-04-25 20:42:39 -04001019 _init_fn(all_init_fields,
1020 std_init_fields,
1021 kw_only_init_fields,
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -05001022 frozen,
Eric V. Smithea8fc522018-01-27 19:07:40 -05001023 has_post_init,
Eric V. Smithf8e75492018-05-16 05:14:53 -04001024 # The name to use for the "self"
1025 # param in __init__. Use "self"
1026 # if possible.
Eric V. Smithea8fc522018-01-27 19:07:40 -05001027 '__dataclass_self__' if 'self' in fields
1028 else 'self',
Yury Selivanovd219cc42019-12-09 09:54:20 -05001029 globals,
Eric V. Smithea8fc522018-01-27 19:07:40 -05001030 ))
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001031
1032 # Get the fields as a list, and include only real fields. This is
Eric V. Smithf8e75492018-05-16 05:14:53 -04001033 # used in all of the following methods.
Eric V. Smithea8fc522018-01-27 19:07:40 -05001034 field_list = [f for f in fields.values() if f._field_type is _FIELD]
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001035
1036 if repr:
Eric V. Smithea8fc522018-01-27 19:07:40 -05001037 flds = [f for f in field_list if f.repr]
Yury Selivanovd219cc42019-12-09 09:54:20 -05001038 _set_new_attribute(cls, '__repr__', _repr_fn(flds, globals))
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001039
1040 if eq:
Miss Islington (bot)1757ddd2021-05-28 18:07:27 -07001041 # Create __eq__ method. There's no need for a __ne__ method,
Eric V. Smithf8e75492018-05-16 05:14:53 -04001042 # since python will call __eq__ and negate it.
Eric V. Smithea8fc522018-01-27 19:07:40 -05001043 flds = [f for f in field_list if f.compare]
1044 self_tuple = _tuple_str('self', flds)
1045 other_tuple = _tuple_str('other', flds)
1046 _set_new_attribute(cls, '__eq__',
1047 _cmp_fn('__eq__', '==',
Yury Selivanovd219cc42019-12-09 09:54:20 -05001048 self_tuple, other_tuple,
1049 globals=globals))
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001050
1051 if order:
Eric V. Smithea8fc522018-01-27 19:07:40 -05001052 # Create and set the ordering methods.
1053 flds = [f for f in field_list if f.compare]
1054 self_tuple = _tuple_str('self', flds)
1055 other_tuple = _tuple_str('other', flds)
1056 for name, op in [('__lt__', '<'),
1057 ('__le__', '<='),
1058 ('__gt__', '>'),
1059 ('__ge__', '>='),
1060 ]:
1061 if _set_new_attribute(cls, name,
Yury Selivanovd219cc42019-12-09 09:54:20 -05001062 _cmp_fn(name, op, self_tuple, other_tuple,
1063 globals=globals)):
Eric V. Smithea8fc522018-01-27 19:07:40 -05001064 raise TypeError(f'Cannot overwrite attribute {name} '
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001065 f'in class {cls.__name__}. Consider using '
Eric V. Smithea8fc522018-01-27 19:07:40 -05001066 'functools.total_ordering')
1067
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -05001068 if frozen:
Yury Selivanovd219cc42019-12-09 09:54:20 -05001069 for fn in _frozen_get_del_attr(cls, field_list, globals):
Eric V. Smithf199bc62018-03-18 20:40:34 -04001070 if _set_new_attribute(cls, fn.__name__, fn):
1071 raise TypeError(f'Cannot overwrite attribute {fn.__name__} '
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001072 f'in class {cls.__name__}')
Eric V. Smithea8fc522018-01-27 19:07:40 -05001073
1074 # Decide if/how we're going to create a hash function.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001075 hash_action = _hash_action[bool(unsafe_hash),
1076 bool(eq),
1077 bool(frozen),
1078 has_explicit_hash]
Eric V. Smith01d618c2018-03-24 22:10:14 -04001079 if hash_action:
1080 # No need to call _set_new_attribute here, since by the time
Eric V. Smithf8e75492018-05-16 05:14:53 -04001081 # we're here the overwriting is unconditional.
Yury Selivanovd219cc42019-12-09 09:54:20 -05001082 cls.__hash__ = hash_action(cls, field_list, globals)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001083
1084 if not getattr(cls, '__doc__'):
1085 # Create a class doc-string.
1086 cls.__doc__ = (cls.__name__ +
Pablo Galindob0544ba2021-04-21 12:41:19 +01001087 str(inspect.signature(cls)).replace(' -> None', ''))
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001088
Eric V. Smith750f4842021-04-10 21:28:42 -04001089 if match_args:
Eric V. Smithc0280532021-04-25 20:42:39 -04001090 # I could probably compute this once
Eric V. Smith750f4842021-04-10 21:28:42 -04001091 _set_new_attribute(cls, '__match_args__',
Eric V. Smithc0280532021-04-25 20:42:39 -04001092 tuple(f.name for f in std_init_fields))
Brandt Bucher145bf262021-02-26 14:51:55 -08001093
Yurii Karabasc2419912021-05-01 05:14:30 +03001094 if slots:
Eric V. Smith823fbf42021-05-01 13:27:30 -04001095 cls = _add_slots(cls, frozen)
Yurii Karabasc2419912021-05-01 05:14:30 +03001096
Ben Avrahamibef7d292020-10-06 20:40:50 +03001097 abc.update_abstractmethods(cls)
1098
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001099 return cls
1100
1101
Eric V. Smith823fbf42021-05-01 13:27:30 -04001102# _dataclass_getstate and _dataclass_setstate are needed for pickling frozen
1103# classes with slots. These could be slighly more performant if we generated
1104# the code instead of iterating over fields. But that can be a project for
1105# another day, if performance becomes an issue.
1106def _dataclass_getstate(self):
1107 return [getattr(self, f.name) for f in fields(self)]
1108
1109
1110def _dataclass_setstate(self, state):
1111 for field, value in zip(fields(self), state):
1112 # use setattr because dataclass may be frozen
1113 object.__setattr__(self, field.name, value)
1114
1115
1116def _add_slots(cls, is_frozen):
Yurii Karabasc2419912021-05-01 05:14:30 +03001117 # Need to create a new class, since we can't set __slots__
1118 # after a class has been created.
1119
1120 # Make sure __slots__ isn't already set.
1121 if '__slots__' in cls.__dict__:
1122 raise TypeError(f'{cls.__name__} already specifies __slots__')
1123
1124 # Create a new dict for our new class.
1125 cls_dict = dict(cls.__dict__)
1126 field_names = tuple(f.name for f in fields(cls))
1127 cls_dict['__slots__'] = field_names
1128 for field_name in field_names:
1129 # Remove our attributes, if present. They'll still be
1130 # available in _MARKER.
1131 cls_dict.pop(field_name, None)
1132
1133 # Remove __dict__ itself.
1134 cls_dict.pop('__dict__', None)
1135
1136 # And finally create the class.
1137 qualname = getattr(cls, '__qualname__', None)
1138 cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
1139 if qualname is not None:
1140 cls.__qualname__ = qualname
1141
Eric V. Smith823fbf42021-05-01 13:27:30 -04001142 if is_frozen:
1143 # Need this for pickling frozen classes with slots.
1144 cls.__getstate__ = _dataclass_getstate
1145 cls.__setstate__ = _dataclass_setstate
1146
Yurii Karabasc2419912021-05-01 05:14:30 +03001147 return cls
1148
1149
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03001150def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,
Eric V. Smithc0280532021-04-25 20:42:39 -04001151 unsafe_hash=False, frozen=False, match_args=True,
Yurii Karabasc2419912021-05-01 05:14:30 +03001152 kw_only=False, slots=False):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001153 """Returns the same class as was passed in, with dunder methods
1154 added based on the fields defined in the class.
1155
1156 Examines PEP 526 __annotations__ to determine fields.
1157
1158 If init is true, an __init__() method is added to the class. If
1159 repr is true, a __repr__() method is added. If order is true, rich
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001160 comparison dunder methods are added. If unsafe_hash is true, a
1161 __hash__() method function is added. If frozen is true, fields may
Eric V. Smith750f4842021-04-10 21:28:42 -04001162 not be assigned to after instance creation. If match_args is true,
Eric V. Smithc0280532021-04-25 20:42:39 -04001163 the __match_args__ tuple is added. If kw_only is true, then by
Yurii Karabasc2419912021-05-01 05:14:30 +03001164 default all fields are keyword-only. If slots is true, an
1165 __slots__ attribute is added.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001166 """
1167
1168 def wrap(cls):
Eric V. Smith750f4842021-04-10 21:28:42 -04001169 return _process_class(cls, init, repr, eq, order, unsafe_hash,
Yurii Karabasc2419912021-05-01 05:14:30 +03001170 frozen, match_args, kw_only, slots)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001171
1172 # See if we're being called as @dataclass or @dataclass().
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03001173 if cls is None:
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001174 # We're called with parens.
1175 return wrap
1176
1177 # We're called as @dataclass without parens.
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03001178 return wrap(cls)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001179
1180
1181def fields(class_or_instance):
1182 """Return a tuple describing the fields of this dataclass.
1183
1184 Accepts a dataclass or an instance of one. Tuple elements are of
1185 type Field.
1186 """
1187
1188 # Might it be worth caching this, per class?
1189 try:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -04001190 fields = getattr(class_or_instance, _FIELDS)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001191 except AttributeError:
1192 raise TypeError('must be called with a dataclass type or instance')
1193
Eric V. Smithd1388922018-01-07 14:30:17 -05001194 # Exclude pseudo-fields. Note that fields is sorted by insertion
Eric V. Smithf8e75492018-05-16 05:14:53 -04001195 # order, so the order of the tuple is as the fields were defined.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001196 return tuple(f for f in fields.values() if f._field_type is _FIELD)
1197
1198
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001199def _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001200 """Returns True if obj is an instance of a dataclass."""
Eric V. Smithb0f4dab2019-08-20 01:40:28 -04001201 return hasattr(type(obj), _FIELDS)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001202
1203
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001204def is_dataclass(obj):
1205 """Returns True if obj is a dataclass or an instance of a
1206 dataclass."""
Eric V. Smithb0f4dab2019-08-20 01:40:28 -04001207 cls = obj if isinstance(obj, type) else type(obj)
1208 return hasattr(cls, _FIELDS)
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001209
1210
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001211def asdict(obj, *, dict_factory=dict):
1212 """Return the fields of a dataclass instance as a new dictionary mapping
1213 field names to field values.
1214
1215 Example usage:
1216
1217 @dataclass
1218 class C:
1219 x: int
1220 y: int
1221
1222 c = C(1, 2)
1223 assert asdict(c) == {'x': 1, 'y': 2}
1224
1225 If given, 'dict_factory' will be used instead of built-in dict.
1226 The function applies recursively to field values that are
1227 dataclass instances. This will also look into built-in containers:
1228 tuples, lists, and dicts.
1229 """
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001230 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001231 raise TypeError("asdict() should be called on dataclass instances")
1232 return _asdict_inner(obj, dict_factory)
1233
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001234
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001235def _asdict_inner(obj, dict_factory):
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001236 if _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001237 result = []
1238 for f in fields(obj):
1239 value = _asdict_inner(getattr(obj, f.name), dict_factory)
1240 result.append((f.name, value))
1241 return dict_factory(result)
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001242 elif isinstance(obj, tuple) and hasattr(obj, '_fields'):
1243 # obj is a namedtuple. Recurse into it, but the returned
1244 # object is another namedtuple of the same type. This is
1245 # similar to how other list- or tuple-derived classes are
1246 # treated (see below), but we just need to create them
1247 # differently because a namedtuple's __init__ needs to be
1248 # called differently (see bpo-34363).
1249
1250 # I'm not using namedtuple's _asdict()
1251 # method, because:
1252 # - it does not recurse in to the namedtuple fields and
1253 # convert them to dicts (using dict_factory).
Jürgen Gmach80526f62020-06-24 12:46:52 +02001254 # - I don't actually want to return a dict here. The main
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001255 # use case here is json.dumps, and it handles converting
1256 # namedtuples to lists. Admittedly we're losing some
1257 # information here when we produce a json list instead of a
1258 # dict. Note that if we returned dicts here instead of
1259 # namedtuples, we could no longer call asdict() on a data
1260 # structure where a namedtuple was used as a dict key.
1261
1262 return type(obj)(*[_asdict_inner(v, dict_factory) for v in obj])
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001263 elif isinstance(obj, (list, tuple)):
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001264 # Assume we can create an object of this type by passing in a
1265 # generator (which is not true for namedtuples, handled
1266 # above).
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001267 return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
1268 elif isinstance(obj, dict):
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001269 return type(obj)((_asdict_inner(k, dict_factory),
1270 _asdict_inner(v, dict_factory))
1271 for k, v in obj.items())
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001272 else:
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001273 return copy.deepcopy(obj)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001274
1275
1276def astuple(obj, *, tuple_factory=tuple):
1277 """Return the fields of a dataclass instance as a new tuple of field values.
1278
1279 Example usage::
1280
1281 @dataclass
1282 class C:
1283 x: int
1284 y: int
1285
1286 c = C(1, 2)
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001287 assert astuple(c) == (1, 2)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001288
1289 If given, 'tuple_factory' will be used instead of built-in tuple.
1290 The function applies recursively to field values that are
1291 dataclass instances. This will also look into built-in containers:
1292 tuples, lists, and dicts.
1293 """
1294
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001295 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001296 raise TypeError("astuple() should be called on dataclass instances")
1297 return _astuple_inner(obj, tuple_factory)
1298
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001299
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001300def _astuple_inner(obj, tuple_factory):
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001301 if _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001302 result = []
1303 for f in fields(obj):
1304 value = _astuple_inner(getattr(obj, f.name), tuple_factory)
1305 result.append(value)
1306 return tuple_factory(result)
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001307 elif isinstance(obj, tuple) and hasattr(obj, '_fields'):
1308 # obj is a namedtuple. Recurse into it, but the returned
1309 # object is another namedtuple of the same type. This is
1310 # similar to how other list- or tuple-derived classes are
1311 # treated (see below), but we just need to create them
1312 # differently because a namedtuple's __init__ needs to be
1313 # called differently (see bpo-34363).
1314 return type(obj)(*[_astuple_inner(v, tuple_factory) for v in obj])
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001315 elif isinstance(obj, (list, tuple)):
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001316 # Assume we can create an object of this type by passing in a
1317 # generator (which is not true for namedtuples, handled
1318 # above).
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001319 return type(obj)(_astuple_inner(v, tuple_factory) for v in obj)
1320 elif isinstance(obj, dict):
1321 return type(obj)((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory))
1322 for k, v in obj.items())
1323 else:
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001324 return copy.deepcopy(obj)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001325
1326
Eric V. Smithd80b4432018-01-06 17:09:58 -05001327def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
Eric V. Smith5da8cfb2018-03-01 08:01:41 -05001328 repr=True, eq=True, order=False, unsafe_hash=False,
Yurii Karabasc2419912021-05-01 05:14:30 +03001329 frozen=False, match_args=True, slots=False):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001330 """Return a new dynamically created dataclass.
1331
Eric V. Smithed7d4292018-01-06 16:14:03 -05001332 The dataclass name will be 'cls_name'. 'fields' is an iterable
1333 of either (name), (name, type) or (name, type, Field) objects. If type is
1334 omitted, use the string 'typing.Any'. Field objects are created by
Eric V. Smithd327ae62018-01-07 08:19:45 -05001335 the equivalent of calling 'field(name, type [, Field-info])'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001336
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001337 C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001338
1339 is equivalent to:
1340
1341 @dataclass
1342 class C(Base):
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001343 x: 'typing.Any'
1344 y: int
1345 z: int = field(init=False)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001346
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001347 For the bases and namespace parameters, see the builtin type() function.
Eric V. Smithd80b4432018-01-06 17:09:58 -05001348
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001349 The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to
Eric V. Smithd80b4432018-01-06 17:09:58 -05001350 dataclass().
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001351 """
1352
1353 if namespace is None:
1354 namespace = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001355
Eric V. Smith4e812962018-05-16 11:31:29 -04001356 # While we're looking through the field names, validate that they
1357 # are identifiers, are not keywords, and not duplicates.
1358 seen = set()
Eric V. Smithc1a66bd2021-04-12 21:02:02 -04001359 annotations = {}
1360 defaults = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001361 for item in fields:
Eric V. Smithed7d4292018-01-06 16:14:03 -05001362 if isinstance(item, str):
1363 name = item
1364 tp = 'typing.Any'
1365 elif len(item) == 2:
1366 name, tp, = item
1367 elif len(item) == 3:
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001368 name, tp, spec = item
Eric V. Smithc1a66bd2021-04-12 21:02:02 -04001369 defaults[name] = spec
Eric V. Smith4e812962018-05-16 11:31:29 -04001370 else:
1371 raise TypeError(f'Invalid field: {item!r}')
1372
1373 if not isinstance(name, str) or not name.isidentifier():
Min ho Kim96e12d52019-07-22 06:12:33 +10001374 raise TypeError(f'Field names must be valid identifiers: {name!r}')
Eric V. Smith4e812962018-05-16 11:31:29 -04001375 if keyword.iskeyword(name):
1376 raise TypeError(f'Field names must not be keywords: {name!r}')
1377 if name in seen:
1378 raise TypeError(f'Field name duplicated: {name!r}')
1379
1380 seen.add(name)
Eric V. Smithc1a66bd2021-04-12 21:02:02 -04001381 annotations[name] = tp
Eric V. Smithed7d4292018-01-06 16:14:03 -05001382
Eric V. Smithc1a66bd2021-04-12 21:02:02 -04001383 # Update 'ns' with the user-supplied namespace plus our calculated values.
1384 def exec_body_callback(ns):
1385 ns.update(namespace)
1386 ns.update(defaults)
1387 ns['__annotations__'] = annotations
1388
Ivan Levkivskyi5a7092d2018-03-31 13:41:17 +01001389 # We use `types.new_class()` instead of simply `type()` to allow dynamic creation
Miss Islington (bot)e086bfe2021-10-09 12:50:45 -07001390 # of generic dataclasses.
Eric V. Smithc1a66bd2021-04-12 21:02:02 -04001391 cls = types.new_class(cls_name, bases, {}, exec_body_callback)
1392
1393 # Apply the normal decorator.
Eric V. Smithd80b4432018-01-06 17:09:58 -05001394 return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
Eric V. Smith750f4842021-04-10 21:28:42 -04001395 unsafe_hash=unsafe_hash, frozen=frozen,
Yurii Karabasc2419912021-05-01 05:14:30 +03001396 match_args=match_args, slots=slots)
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001397
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001398
Serhiy Storchaka2d88e632019-06-26 19:07:44 +03001399def replace(obj, /, **changes):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001400 """Return a new object replacing specified fields with new values.
1401
1402 This is especially useful for frozen classes. Example usage:
1403
1404 @dataclass(frozen=True)
1405 class C:
1406 x: int
1407 y: int
1408
1409 c = C(1, 2)
1410 c1 = replace(c, x=3)
1411 assert c1.x == 3 and c1.y == 2
1412 """
1413
Eric V. Smithf8e75492018-05-16 05:14:53 -04001414 # We're going to mutate 'changes', but that's okay because it's a
1415 # new dict, even if called with 'replace(obj, **my_changes)'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001416
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001417 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001418 raise TypeError("replace() should be called on dataclass instances")
1419
1420 # It's an error to have init=False fields in 'changes'.
1421 # If a field is not in 'changes', read its value from the provided obj.
1422
Eric V. Smithf199bc62018-03-18 20:40:34 -04001423 for f in getattr(obj, _FIELDS).values():
Eric V. Smithe7adf2b2018-06-07 14:43:59 -04001424 # Only consider normal fields or InitVars.
1425 if f._field_type is _FIELD_CLASSVAR:
1426 continue
1427
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001428 if not f.init:
1429 # Error if this field is specified in changes.
1430 if f.name in changes:
1431 raise ValueError(f'field {f.name} is declared with '
1432 'init=False, it cannot be specified with '
1433 'replace()')
1434 continue
1435
1436 if f.name not in changes:
Zackery Spytz75220672021-04-05 13:41:01 -06001437 if f._field_type is _FIELD_INITVAR and f.default is MISSING:
Dong-hee Na3d70f7a2018-06-23 23:46:32 +09001438 raise ValueError(f"InitVar {f.name!r} "
1439 'must be specified with replace()')
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001440 changes[f.name] = getattr(obj, f.name)
1441
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001442 # Create the new object, which calls __init__() and
Eric V. Smithf8e75492018-05-16 05:14:53 -04001443 # __post_init__() (if defined), using all of the init fields we've
1444 # added and/or left in 'changes'. If there are values supplied in
1445 # changes that aren't fields, this will correctly raise a
1446 # TypeError.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001447 return obj.__class__(**changes)