blob: a4afd50376bdc6fa861bbfafb0276ac0e4a8c0ac [file] [log] [blame]
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001import sys
2import types
3from copy import deepcopy
Eric V. Smithf0db54a2017-12-04 16:58:55 -05004import inspect
5
6__all__ = ['dataclass',
7 'field',
8 'FrozenInstanceError',
9 'InitVar',
Eric V. Smith03220fd2017-12-29 13:59:58 -050010 'MISSING',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050011
12 # Helper functions.
13 'fields',
14 'asdict',
15 'astuple',
16 'make_dataclass',
17 'replace',
Eric V. Smithe7ba0132018-01-06 12:41:53 -050018 'is_dataclass',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050019 ]
20
Eric V. Smithea8fc522018-01-27 19:07:40 -050021# Conditions for adding methods. The boxes indicate what action the
22# dataclass decorator takes. For all of these tables, when I talk
Eric V. Smithdbf9cff2018-02-25 21:30:17 -050023# about init=, repr=, eq=, order=, unsafe_hash=, or frozen=, I'm
24# referring to the arguments to the @dataclass decorator. When
25# checking if a dunder method already exists, I mean check for an
26# entry in the class's __dict__. I never check to see if an
27# attribute is defined in a base class.
Eric V. Smithea8fc522018-01-27 19:07:40 -050028
29# Key:
30# +=========+=========================================+
31# + Value | Meaning |
32# +=========+=========================================+
33# | <blank> | No action: no method is added. |
34# +---------+-----------------------------------------+
35# | add | Generated method is added. |
36# +---------+-----------------------------------------+
Eric V. Smithea8fc522018-01-27 19:07:40 -050037# | raise | TypeError is raised. |
38# +---------+-----------------------------------------+
39# | None | Attribute is set to None. |
40# +=========+=========================================+
41
42# __init__
43#
44# +--- init= parameter
45# |
46# v | | |
47# | no | yes | <--- class has __init__ in __dict__?
48# +=======+=======+=======+
49# | False | | |
50# +-------+-------+-------+
51# | True | add | | <- the default
52# +=======+=======+=======+
53
54# __repr__
55#
56# +--- repr= parameter
57# |
58# v | | |
59# | no | yes | <--- class has __repr__ in __dict__?
60# +=======+=======+=======+
61# | False | | |
62# +-------+-------+-------+
63# | True | add | | <- the default
64# +=======+=======+=======+
65
66
67# __setattr__
68# __delattr__
69#
70# +--- frozen= parameter
71# |
72# v | | |
73# | no | yes | <--- class has __setattr__ or __delattr__ in __dict__?
74# +=======+=======+=======+
75# | False | | | <- the default
76# +-------+-------+-------+
77# | True | add | raise |
78# +=======+=======+=======+
79# Raise because not adding these methods would break the "frozen-ness"
80# of the class.
81
82# __eq__
83#
84# +--- eq= parameter
85# |
86# v | | |
87# | no | yes | <--- class has __eq__ in __dict__?
88# +=======+=======+=======+
89# | False | | |
90# +-------+-------+-------+
91# | True | add | | <- the default
92# +=======+=======+=======+
93
94# __lt__
95# __le__
96# __gt__
97# __ge__
98#
99# +--- order= parameter
100# |
101# v | | |
102# | no | yes | <--- class has any comparison method in __dict__?
103# +=======+=======+=======+
104# | False | | | <- the default
105# +-------+-------+-------+
106# | True | add | raise |
107# +=======+=======+=======+
108# Raise because to allow this case would interfere with using
109# functools.total_ordering.
110
111# __hash__
112
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500113# +------------------- unsafe_hash= parameter
114# | +----------- eq= parameter
115# | | +--- frozen= parameter
116# | | |
117# v v v | | |
118# | no | yes | <--- class has explicitly defined __hash__
119# +=======+=======+=======+========+========+
120# | False | False | False | | | No __eq__, use the base class __hash__
121# +-------+-------+-------+--------+--------+
122# | False | False | True | | | No __eq__, use the base class __hash__
123# +-------+-------+-------+--------+--------+
124# | False | True | False | None | | <-- the default, not hashable
125# +-------+-------+-------+--------+--------+
126# | False | True | True | add | | Frozen, so hashable, allows override
127# +-------+-------+-------+--------+--------+
128# | True | False | False | add | raise | Has no __eq__, but hashable
129# +-------+-------+-------+--------+--------+
130# | True | False | True | add | raise | Has no __eq__, but hashable
131# +-------+-------+-------+--------+--------+
132# | True | True | False | add | raise | Not frozen, but hashable
133# +-------+-------+-------+--------+--------+
134# | True | True | True | add | raise | Frozen, so hashable
135# +=======+=======+=======+========+========+
Eric V. Smithea8fc522018-01-27 19:07:40 -0500136# For boxes that are blank, __hash__ is untouched and therefore
137# inherited from the base class. If the base is object, then
138# id-based hashing is used.
139# Note that a class may have already __hash__=None if it specified an
140# __eq__ method in the class body (not one that was created by
141# @dataclass).
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500142# See _hash_action (below) for a coded version of this table.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500143
144
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500145# Raised when an attempt is made to modify a frozen class.
146class FrozenInstanceError(AttributeError): pass
147
148# A sentinel object for default values to signal that a
149# default-factory will be used.
150# This is given a nice repr() which will appear in the function
151# signature of dataclasses' constructors.
152class _HAS_DEFAULT_FACTORY_CLASS:
153 def __repr__(self):
154 return '<factory>'
155_HAS_DEFAULT_FACTORY = _HAS_DEFAULT_FACTORY_CLASS()
156
Eric V. Smith03220fd2017-12-29 13:59:58 -0500157# A sentinel object to detect if a parameter is supplied or not. Use
158# a class to give it a better repr.
159class _MISSING_TYPE:
160 pass
161MISSING = _MISSING_TYPE()
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500162
163# Since most per-field metadata will be unused, create an empty
164# read-only proxy that can be shared among all fields.
165_EMPTY_METADATA = types.MappingProxyType({})
166
167# Markers for the various kinds of fields and pseudo-fields.
168_FIELD = object() # An actual field.
169_FIELD_CLASSVAR = object() # Not a field, but a ClassVar.
170_FIELD_INITVAR = object() # Not a field, but an InitVar.
171
172# The name of an attribute on the class where we store the Field
173# objects. Also used to check if a class is a Data Class.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400174_FIELDS = '__dataclass_fields__'
175
176# The name of an attribute on the class that stores the parameters to
177# @dataclass.
178_PARAMS = '__dataclass_params__'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500179
180# The name of the function, that if it exists, is called at the end of
181# __init__.
182_POST_INIT_NAME = '__post_init__'
183
184
185class _InitVarMeta(type):
186 def __getitem__(self, params):
187 return self
188
189class InitVar(metaclass=_InitVarMeta):
190 pass
191
192
193# Instances of Field are only ever created from within this module,
194# and only from the field() function, although Field instances are
195# exposed externally as (conceptually) read-only objects.
196# name and type are filled in after the fact, not in __init__. They're
197# not known at the time this class is instantiated, but it's
198# convenient if they're available later.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400199# When cls._FIELDS is filled in with a list of Field objects, the name
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500200# and type fields will have been populated.
201class Field:
202 __slots__ = ('name',
203 'type',
204 'default',
205 'default_factory',
206 'repr',
207 'hash',
208 'init',
209 'compare',
210 'metadata',
211 '_field_type', # Private: not to be used by user code.
212 )
213
214 def __init__(self, default, default_factory, init, repr, hash, compare,
215 metadata):
216 self.name = None
217 self.type = None
218 self.default = default
219 self.default_factory = default_factory
220 self.init = init
221 self.repr = repr
222 self.hash = hash
223 self.compare = compare
224 self.metadata = (_EMPTY_METADATA
225 if metadata is None or len(metadata) == 0 else
226 types.MappingProxyType(metadata))
227 self._field_type = None
228
229 def __repr__(self):
230 return ('Field('
231 f'name={self.name!r},'
232 f'type={self.type},'
233 f'default={self.default},'
234 f'default_factory={self.default_factory},'
235 f'init={self.init},'
236 f'repr={self.repr},'
237 f'hash={self.hash},'
238 f'compare={self.compare},'
239 f'metadata={self.metadata}'
240 ')')
241
242
Eric V. Smithf199bc62018-03-18 20:40:34 -0400243class _DataclassParams:
244 __slots__ = ('init',
245 'repr',
246 'eq',
247 'order',
248 'unsafe_hash',
249 'frozen',
250 )
251 def __init__(self, init, repr, eq, order, unsafe_hash, frozen):
252 self.init = init
253 self.repr = repr
254 self.eq = eq
255 self.order = order
256 self.unsafe_hash = unsafe_hash
257 self.frozen = frozen
258
259 def __repr__(self):
260 return ('_DataclassParams('
261 f'init={self.init},'
262 f'repr={self.repr},'
263 f'eq={self.eq},'
264 f'order={self.order},'
265 f'unsafe_hash={self.unsafe_hash},'
266 f'frozen={self.frozen}'
267 ')')
268
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500269# This function is used instead of exposing Field creation directly,
270# so that a type checker can be told (via overloads) that this is a
271# function whose type depends on its parameters.
Eric V. Smith03220fd2017-12-29 13:59:58 -0500272def field(*, default=MISSING, default_factory=MISSING, init=True, repr=True,
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500273 hash=None, compare=True, metadata=None):
274 """Return an object to identify dataclass fields.
275
276 default is the default value of the field. default_factory is a
277 0-argument function called to initialize a field's value. If init
278 is True, the field will be a parameter to the class's __init__()
279 function. If repr is True, the field will be included in the
280 object's repr(). If hash is True, the field will be included in
281 the object's hash(). If compare is True, the field will be used in
282 comparison functions. metadata, if specified, must be a mapping
283 which is stored but not otherwise examined by dataclass.
284
285 It is an error to specify both default and default_factory.
286 """
287
Eric V. Smith03220fd2017-12-29 13:59:58 -0500288 if default is not MISSING and default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500289 raise ValueError('cannot specify both default and default_factory')
290 return Field(default, default_factory, init, repr, hash, compare,
291 metadata)
292
293
294def _tuple_str(obj_name, fields):
295 # Return a string representing each field of obj_name as a tuple
296 # member. So, if fields is ['x', 'y'] and obj_name is "self",
297 # return "(self.x,self.y)".
298
299 # Special case for the 0-tuple.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500300 if not fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500301 return '()'
302 # Note the trailing comma, needed if this turns out to be a 1-tuple.
303 return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)'
304
305
Eric V. Smithea8fc522018-01-27 19:07:40 -0500306def _create_fn(name, args, body, *, globals=None, locals=None,
Eric V. Smith03220fd2017-12-29 13:59:58 -0500307 return_type=MISSING):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500308 # Note that we mutate locals when exec() is called. Caller beware!
309 if locals is None:
310 locals = {}
311 return_annotation = ''
Eric V. Smith03220fd2017-12-29 13:59:58 -0500312 if return_type is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500313 locals['_return_type'] = return_type
314 return_annotation = '->_return_type'
315 args = ','.join(args)
316 body = '\n'.join(f' {b}' for b in body)
317
Eric V. Smithf199bc62018-03-18 20:40:34 -0400318 # Compute the text of the entire function.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500319 txt = f'def {name}({args}){return_annotation}:\n{body}'
320
321 exec(txt, globals, locals)
322 return locals[name]
323
324
325def _field_assign(frozen, name, value, self_name):
326 # If we're a frozen class, then assign to our fields in __init__
327 # via object.__setattr__. Otherwise, just use a simple
328 # assignment.
329 # self_name is what "self" is called in this function: don't
330 # hard-code "self", since that might be a field name.
331 if frozen:
332 return f'object.__setattr__({self_name},{name!r},{value})'
333 return f'{self_name}.{name}={value}'
334
335
336def _field_init(f, frozen, globals, self_name):
337 # Return the text of the line in the body of __init__ that will
338 # initialize this field.
339
340 default_name = f'_dflt_{f.name}'
Eric V. Smith03220fd2017-12-29 13:59:58 -0500341 if f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500342 if f.init:
343 # This field has a default factory. If a parameter is
344 # given, use it. If not, call the factory.
345 globals[default_name] = f.default_factory
346 value = (f'{default_name}() '
347 f'if {f.name} is _HAS_DEFAULT_FACTORY '
348 f'else {f.name}')
349 else:
350 # This is a field that's not in the __init__ params, but
351 # has a default factory function. It needs to be
352 # initialized here by calling the factory function,
353 # because there's no other way to initialize it.
354
355 # For a field initialized with a default=defaultvalue, the
356 # class dict just has the default value
357 # (cls.fieldname=defaultvalue). But that won't work for a
358 # default factory, the factory must be called in __init__
359 # and we must assign that to self.fieldname. We can't
360 # fall back to the class dict's value, both because it's
361 # not set, and because it might be different per-class
362 # (which, after all, is why we have a factory function!).
363
364 globals[default_name] = f.default_factory
365 value = f'{default_name}()'
366 else:
367 # No default factory.
368 if f.init:
Eric V. Smith03220fd2017-12-29 13:59:58 -0500369 if f.default is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500370 # There's no default, just do an assignment.
371 value = f.name
Eric V. Smith03220fd2017-12-29 13:59:58 -0500372 elif f.default is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500373 globals[default_name] = f.default
374 value = f.name
375 else:
376 # This field does not need initialization. Signify that to
377 # the caller by returning None.
378 return None
379
380 # Only test this now, so that we can create variables for the
381 # default. However, return None to signify that we're not going
382 # to actually do the assignment statement for InitVars.
383 if f._field_type == _FIELD_INITVAR:
384 return None
385
386 # Now, actually generate the field assignment.
387 return _field_assign(frozen, f.name, value, self_name)
388
389
390def _init_param(f):
391 # Return the __init__ parameter string for this field.
392 # For example, the equivalent of 'x:int=3' (except instead of 'int',
393 # reference a variable set to int, and instead of '3', reference a
394 # variable set to 3).
Eric V. Smith03220fd2017-12-29 13:59:58 -0500395 if f.default is MISSING and f.default_factory is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500396 # There's no default, and no default_factory, just
397 # output the variable name and type.
398 default = ''
Eric V. Smith03220fd2017-12-29 13:59:58 -0500399 elif f.default is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500400 # There's a default, this will be the name that's used to look it up.
401 default = f'=_dflt_{f.name}'
Eric V. Smith03220fd2017-12-29 13:59:58 -0500402 elif f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500403 # There's a factory function. Set a marker.
404 default = '=_HAS_DEFAULT_FACTORY'
405 return f'{f.name}:_type_{f.name}{default}'
406
407
408def _init_fn(fields, frozen, has_post_init, self_name):
409 # fields contains both real fields and InitVar pseudo-fields.
410
411 # Make sure we don't have fields without defaults following fields
412 # with defaults. This actually would be caught when exec-ing the
413 # function source code, but catching it here gives a better error
414 # message, and future-proofs us in case we build up the function
415 # using ast.
416 seen_default = False
417 for f in fields:
418 # Only consider fields in the __init__ call.
419 if f.init:
Eric V. Smith03220fd2017-12-29 13:59:58 -0500420 if not (f.default is MISSING and f.default_factory is MISSING):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500421 seen_default = True
422 elif seen_default:
423 raise TypeError(f'non-default argument {f.name!r} '
424 'follows default argument')
425
Eric V. Smith03220fd2017-12-29 13:59:58 -0500426 globals = {'MISSING': MISSING,
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500427 '_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY}
428
429 body_lines = []
430 for f in fields:
431 # Do not initialize the pseudo-fields, only the real ones.
432 line = _field_init(f, frozen, globals, self_name)
433 if line is not None:
434 # line is None means that this field doesn't require
435 # initialization. Just skip it.
436 body_lines.append(line)
437
438 # Does this class have a post-init function?
439 if has_post_init:
440 params_str = ','.join(f.name for f in fields
441 if f._field_type is _FIELD_INITVAR)
442 body_lines += [f'{self_name}.{_POST_INIT_NAME}({params_str})']
443
444 # If no body lines, use 'pass'.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500445 if not body_lines:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500446 body_lines = ['pass']
447
448 locals = {f'_type_{f.name}': f.type for f in fields}
449 return _create_fn('__init__',
450 [self_name] +[_init_param(f) for f in fields if f.init],
451 body_lines,
452 locals=locals,
453 globals=globals,
454 return_type=None)
455
456
457def _repr_fn(fields):
458 return _create_fn('__repr__',
459 ['self'],
460 ['return self.__class__.__qualname__ + f"(' +
461 ', '.join([f"{f.name}={{self.{f.name}!r}}"
462 for f in fields]) +
463 ')"'])
464
465
Eric V. Smithf199bc62018-03-18 20:40:34 -0400466def _frozen_get_del_attr(cls, fields):
467 # XXX: globals is modified on the first call to _create_fn, then the
468 # modified version is used in the second call. Is this okay?
469 globals = {'cls': cls,
470 'FrozenInstanceError': FrozenInstanceError}
471 if fields:
472 fields_str = '(' + ','.join(repr(f.name) for f in fields) + ',)'
473 else:
474 # Special case for the zero-length tuple.
475 fields_str = '()'
476 return (_create_fn('__setattr__',
477 ('self', 'name', 'value'),
478 (f'if type(self) is cls or name in {fields_str}:',
479 ' raise FrozenInstanceError(f"cannot assign to field {name!r}")',
480 f'super(cls, self).__setattr__(name, value)'),
481 globals=globals),
482 _create_fn('__delattr__',
483 ('self', 'name'),
484 (f'if type(self) is cls or name in {fields_str}:',
485 ' raise FrozenInstanceError(f"cannot delete field {name!r}")',
486 f'super(cls, self).__delattr__(name)'),
487 globals=globals),
488 )
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500489
490
491def _cmp_fn(name, op, self_tuple, other_tuple):
492 # Create a comparison function. If the fields in the object are
493 # named 'x' and 'y', then self_tuple is the string
494 # '(self.x,self.y)' and other_tuple is the string
495 # '(other.x,other.y)'.
496
497 return _create_fn(name,
498 ['self', 'other'],
499 [ 'if other.__class__ is self.__class__:',
500 f' return {self_tuple}{op}{other_tuple}',
501 'return NotImplemented'])
502
503
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500504def _hash_fn(fields):
505 self_tuple = _tuple_str('self', fields)
506 return _create_fn('__hash__',
507 ['self'],
508 [f'return hash({self_tuple})'])
509
510
511def _get_field(cls, a_name, a_type):
512 # Return a Field object, for this field name and type. ClassVars
513 # and InitVars are also returned, but marked as such (see
514 # f._field_type).
515
516 # If the default value isn't derived from field, then it's
517 # only a normal default value. Convert it to a Field().
Eric V. Smith03220fd2017-12-29 13:59:58 -0500518 default = getattr(cls, a_name, MISSING)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500519 if isinstance(default, Field):
520 f = default
521 else:
Eric V. Smith7389fd92018-03-19 21:07:51 -0400522 if isinstance(default, types.MemberDescriptorType):
523 # This is a field in __slots__, so it has no default value.
524 default = MISSING
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500525 f = field(default=default)
526
527 # Assume it's a normal field until proven otherwise.
528 f._field_type = _FIELD
529
530 # Only at this point do we know the name and the type. Set them.
531 f.name = a_name
532 f.type = a_type
533
534 # If typing has not been imported, then it's impossible for
535 # any annotation to be a ClassVar. So, only look for ClassVar
536 # if typing has been imported.
537 typing = sys.modules.get('typing')
538 if typing is not None:
539 # This test uses a typing internal class, but it's the best
540 # way to test if this is a ClassVar.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000541 if (type(a_type) is typing._GenericAlias and
542 a_type.__origin__ is typing.ClassVar):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500543 # This field is a ClassVar, so it's not a field.
544 f._field_type = _FIELD_CLASSVAR
545
546 if f._field_type is _FIELD:
547 # Check if this is an InitVar.
548 if a_type is InitVar:
549 # InitVars are not fields, either.
550 f._field_type = _FIELD_INITVAR
551
552 # Validations for fields. This is delayed until now, instead of
553 # in the Field() constructor, since only here do we know the field
554 # name, which allows better error reporting.
555
556 # Special restrictions for ClassVar and InitVar.
557 if f._field_type in (_FIELD_CLASSVAR, _FIELD_INITVAR):
Eric V. Smith03220fd2017-12-29 13:59:58 -0500558 if f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500559 raise TypeError(f'field {f.name} cannot have a '
560 'default factory')
561 # Should I check for other field settings? default_factory
562 # seems the most serious to check for. Maybe add others. For
563 # example, how about init=False (or really,
564 # init=<not-the-default-init-value>)? It makes no sense for
565 # ClassVar and InitVar to specify init=<anything>.
566
567 # For real fields, disallow mutable defaults for known types.
568 if f._field_type is _FIELD and isinstance(f.default, (list, dict, set)):
569 raise ValueError(f'mutable default {type(f.default)} for field '
570 f'{f.name} is not allowed: use default_factory')
571
572 return f
573
574
575def _find_fields(cls):
576 # Return a list of Field objects, in order, for this class (and no
577 # base classes). Fields are found from __annotations__ (which is
578 # guaranteed to be ordered). Default values are from class
579 # attributes, if a field has a default. If the default value is
580 # a Field(), then it contains additional info beyond (and
581 # possibly including) the actual default value. Pseudo-fields
582 # ClassVars and InitVars are included, despite the fact that
Eric V. Smithea8fc522018-01-27 19:07:40 -0500583 # they're not real fields. That's dealt with later.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500584
585 annotations = getattr(cls, '__annotations__', {})
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500586 return [_get_field(cls, a_name, a_type)
587 for a_name, a_type in annotations.items()]
588
589
Eric V. Smithea8fc522018-01-27 19:07:40 -0500590def _set_new_attribute(cls, name, value):
591 # Never overwrites an existing attribute. Returns True if the
592 # attribute already exists.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500593 if name in cls.__dict__:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500594 return True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500595 setattr(cls, name, value)
Eric V. Smithea8fc522018-01-27 19:07:40 -0500596 return False
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500597
598
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500599# Decide if/how we're going to create a hash function. Key is
600# (unsafe_hash, eq, frozen, does-hash-exist). Value is the action to
601# take.
602# Actions:
603# '': Do nothing.
604# 'none': Set __hash__ to None.
605# 'add': Always add a generated __hash__function.
606# 'exception': Raise an exception.
607#
608# +-------------------------------------- unsafe_hash?
609# | +------------------------------- eq?
610# | | +------------------------ frozen?
611# | | | +---------------- has-explicit-hash?
612# | | | |
613# | | | | +------- action
614# | | | | |
615# v v v v v
616_hash_action = {(False, False, False, False): (''),
617 (False, False, False, True ): (''),
618 (False, False, True, False): (''),
619 (False, False, True, True ): (''),
620 (False, True, False, False): ('none'),
621 (False, True, False, True ): (''),
622 (False, True, True, False): ('add'),
623 (False, True, True, True ): (''),
624 (True, False, False, False): ('add'),
625 (True, False, False, True ): ('exception'),
626 (True, False, True, False): ('add'),
627 (True, False, True, True ): ('exception'),
628 (True, True, False, False): ('add'),
629 (True, True, False, True ): ('exception'),
630 (True, True, True, False): ('add'),
631 (True, True, True, True ): ('exception'),
632 }
633# See https://bugs.python.org/issue32929#msg312829 for an if-statement
634# version of this table.
635
636
Eric V. Smithf199bc62018-03-18 20:40:34 -0400637def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
Eric V. Smithd1388922018-01-07 14:30:17 -0500638 # Now that dicts retain insertion order, there's no reason to use
639 # an ordered dict. I am leveraging that ordering here, because
640 # derived class fields overwrite base class fields, but the order
641 # is defined by the base class, which is found first.
642 fields = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500643
Eric V. Smithf199bc62018-03-18 20:40:34 -0400644 setattr(cls, _PARAMS, _DataclassParams(init, repr, eq, order,
645 unsafe_hash, frozen))
646
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500647 # Find our base classes in reverse MRO order, and exclude
648 # ourselves. In reversed order so that more derived classes
649 # override earlier field definitions in base classes.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400650 # As long as we're iterating over them, see if any are frozen.
651 any_frozen_base = False
652 has_dataclass_bases = False
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500653 for b in cls.__mro__[-1:0:-1]:
654 # Only process classes that have been processed by our
Eric V. Smithf199bc62018-03-18 20:40:34 -0400655 # decorator. That is, they have a _FIELDS attribute.
656 base_fields = getattr(b, _FIELDS, None)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500657 if base_fields:
Eric V. Smithf199bc62018-03-18 20:40:34 -0400658 has_dataclass_bases = True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500659 for f in base_fields.values():
660 fields[f.name] = f
Eric V. Smithf199bc62018-03-18 20:40:34 -0400661 if getattr(b, _PARAMS).frozen:
662 any_frozen_base = True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500663
664 # Now find fields in our class. While doing so, validate some
665 # things, and set the default values (as class attributes)
666 # where we can.
667 for f in _find_fields(cls):
668 fields[f.name] = f
669
670 # If the class attribute (which is the default value for
671 # this field) exists and is of type 'Field', replace it
672 # with the real default. This is so that normal class
673 # introspection sees a real default value, not a Field.
674 if isinstance(getattr(cls, f.name, None), Field):
Eric V. Smith03220fd2017-12-29 13:59:58 -0500675 if f.default is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500676 # If there's no default, delete the class attribute.
677 # This happens if we specify field(repr=False), for
678 # example (that is, we specified a field object, but
679 # no default value). Also if we're using a default
680 # factory. The class attribute should not be set at
681 # all in the post-processed class.
682 delattr(cls, f.name)
683 else:
684 setattr(cls, f.name, f.default)
685
Eric V. Smithf199bc62018-03-18 20:40:34 -0400686 # Check rules that apply if we are derived from any dataclasses.
687 if has_dataclass_bases:
688 # Raise an exception if any of our bases are frozen, but we're not.
689 if any_frozen_base and not frozen:
690 raise TypeError('cannot inherit non-frozen dataclass from a '
691 'frozen one')
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500692
Eric V. Smithf199bc62018-03-18 20:40:34 -0400693 # Raise an exception if we're frozen, but none of our bases are.
694 if not any_frozen_base and frozen:
695 raise TypeError('cannot inherit frozen dataclass from a '
696 'non-frozen one')
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500697
Eric V. Smithf199bc62018-03-18 20:40:34 -0400698 # Remember all of the fields on our class (including bases). This also
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500699 # marks this class as being a dataclass.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400700 setattr(cls, _FIELDS, fields)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500701
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500702 # Was this class defined with an explicit __hash__? Note that if
703 # __eq__ is defined in this class, then python will automatically
704 # set __hash__ to None. This is a heuristic, as it's possible
705 # that such a __hash__ == None was not auto-generated, but it
706 # close enough.
707 class_hash = cls.__dict__.get('__hash__', MISSING)
708 has_explicit_hash = not (class_hash is MISSING or
709 (class_hash is None and '__eq__' in cls.__dict__))
Eric V. Smithea8fc522018-01-27 19:07:40 -0500710
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500711 # If we're generating ordering methods, we must be generating
712 # the eq methods.
713 if order and not eq:
714 raise ValueError('eq must be true if order is true')
715
716 if init:
717 # Does this class have a post-init function?
718 has_post_init = hasattr(cls, _POST_INIT_NAME)
719
720 # Include InitVars and regular fields (so, not ClassVars).
Eric V. Smithea8fc522018-01-27 19:07:40 -0500721 flds = [f for f in fields.values()
722 if f._field_type in (_FIELD, _FIELD_INITVAR)]
723 _set_new_attribute(cls, '__init__',
724 _init_fn(flds,
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500725 frozen,
Eric V. Smithea8fc522018-01-27 19:07:40 -0500726 has_post_init,
727 # The name to use for the "self" param
728 # in __init__. Use "self" if possible.
729 '__dataclass_self__' if 'self' in fields
730 else 'self',
731 ))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500732
733 # Get the fields as a list, and include only real fields. This is
734 # used in all of the following methods.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500735 field_list = [f for f in fields.values() if f._field_type is _FIELD]
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500736
737 if repr:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500738 flds = [f for f in field_list if f.repr]
739 _set_new_attribute(cls, '__repr__', _repr_fn(flds))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500740
741 if eq:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500742 # Create _eq__ method. There's no need for a __ne__ method,
743 # since python will call __eq__ and negate it.
744 flds = [f for f in field_list if f.compare]
745 self_tuple = _tuple_str('self', flds)
746 other_tuple = _tuple_str('other', flds)
747 _set_new_attribute(cls, '__eq__',
748 _cmp_fn('__eq__', '==',
749 self_tuple, other_tuple))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500750
751 if order:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500752 # Create and set the ordering methods.
753 flds = [f for f in field_list if f.compare]
754 self_tuple = _tuple_str('self', flds)
755 other_tuple = _tuple_str('other', flds)
756 for name, op in [('__lt__', '<'),
757 ('__le__', '<='),
758 ('__gt__', '>'),
759 ('__ge__', '>='),
760 ]:
761 if _set_new_attribute(cls, name,
762 _cmp_fn(name, op, self_tuple, other_tuple)):
763 raise TypeError(f'Cannot overwrite attribute {name} '
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500764 f'in class {cls.__name__}. Consider using '
Eric V. Smithea8fc522018-01-27 19:07:40 -0500765 'functools.total_ordering')
766
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500767 if frozen:
Eric V. Smithf199bc62018-03-18 20:40:34 -0400768 # XXX: Which fields are frozen? InitVar? ClassVar? hashed-only?
769 for fn in _frozen_get_del_attr(cls, field_list):
770 if _set_new_attribute(cls, fn.__name__, fn):
771 raise TypeError(f'Cannot overwrite attribute {fn.__name__} '
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500772 f'in class {cls.__name__}')
Eric V. Smithea8fc522018-01-27 19:07:40 -0500773
774 # Decide if/how we're going to create a hash function.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500775 hash_action = _hash_action[bool(unsafe_hash),
776 bool(eq),
777 bool(frozen),
778 has_explicit_hash]
779
Eric V. Smithea8fc522018-01-27 19:07:40 -0500780 # No need to call _set_new_attribute here, since we already know if
781 # we're overwriting a __hash__ or not.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500782 if hash_action == '':
Eric V. Smithea8fc522018-01-27 19:07:40 -0500783 # Do nothing.
784 pass
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500785 elif hash_action == 'none':
Eric V. Smithea8fc522018-01-27 19:07:40 -0500786 cls.__hash__ = None
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500787 elif hash_action == 'add':
788 flds = [f for f in field_list if (f.compare if f.hash is None else f.hash)]
789 cls.__hash__ = _hash_fn(flds)
790 elif hash_action == 'exception':
791 # Raise an exception.
792 raise TypeError(f'Cannot overwrite attribute __hash__ '
793 f'in class {cls.__name__}')
Eric V. Smithea8fc522018-01-27 19:07:40 -0500794 else:
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500795 assert False, f"can't get here: {hash_action}"
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500796
797 if not getattr(cls, '__doc__'):
798 # Create a class doc-string.
799 cls.__doc__ = (cls.__name__ +
800 str(inspect.signature(cls)).replace(' -> None', ''))
801
802 return cls
803
804
805# _cls should never be specified by keyword, so start it with an
Raymond Hettingerd55209d2018-01-10 20:56:41 -0800806# underscore. The presence of _cls is used to detect if this
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500807# decorator is being called with parameters or not.
808def dataclass(_cls=None, *, init=True, repr=True, eq=True, order=False,
Eric V. Smith5da8cfb2018-03-01 08:01:41 -0500809 unsafe_hash=False, frozen=False):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500810 """Returns the same class as was passed in, with dunder methods
811 added based on the fields defined in the class.
812
813 Examines PEP 526 __annotations__ to determine fields.
814
815 If init is true, an __init__() method is added to the class. If
816 repr is true, a __repr__() method is added. If order is true, rich
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500817 comparison dunder methods are added. If unsafe_hash is true, a
818 __hash__() method function is added. If frozen is true, fields may
819 not be assigned to after instance creation.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500820 """
821
822 def wrap(cls):
Eric V. Smithf199bc62018-03-18 20:40:34 -0400823 return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500824
825 # See if we're being called as @dataclass or @dataclass().
826 if _cls is None:
827 # We're called with parens.
828 return wrap
829
830 # We're called as @dataclass without parens.
831 return wrap(_cls)
832
833
834def fields(class_or_instance):
835 """Return a tuple describing the fields of this dataclass.
836
837 Accepts a dataclass or an instance of one. Tuple elements are of
838 type Field.
839 """
840
841 # Might it be worth caching this, per class?
842 try:
Eric V. Smithf199bc62018-03-18 20:40:34 -0400843 fields = getattr(class_or_instance, _FIELDS)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500844 except AttributeError:
845 raise TypeError('must be called with a dataclass type or instance')
846
Eric V. Smithd1388922018-01-07 14:30:17 -0500847 # Exclude pseudo-fields. Note that fields is sorted by insertion
848 # order, so the order of the tuple is as the fields were defined.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500849 return tuple(f for f in fields.values() if f._field_type is _FIELD)
850
851
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500852def _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500853 """Returns True if obj is an instance of a dataclass."""
Eric V. Smithf199bc62018-03-18 20:40:34 -0400854 return not isinstance(obj, type) and hasattr(obj, _FIELDS)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500855
856
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500857def is_dataclass(obj):
858 """Returns True if obj is a dataclass or an instance of a
859 dataclass."""
Eric V. Smithf199bc62018-03-18 20:40:34 -0400860 return hasattr(obj, _FIELDS)
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500861
862
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500863def asdict(obj, *, dict_factory=dict):
864 """Return the fields of a dataclass instance as a new dictionary mapping
865 field names to field values.
866
867 Example usage:
868
869 @dataclass
870 class C:
871 x: int
872 y: int
873
874 c = C(1, 2)
875 assert asdict(c) == {'x': 1, 'y': 2}
876
877 If given, 'dict_factory' will be used instead of built-in dict.
878 The function applies recursively to field values that are
879 dataclass instances. This will also look into built-in containers:
880 tuples, lists, and dicts.
881 """
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500882 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500883 raise TypeError("asdict() should be called on dataclass instances")
884 return _asdict_inner(obj, dict_factory)
885
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500886
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500887def _asdict_inner(obj, dict_factory):
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500888 if _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500889 result = []
890 for f in fields(obj):
891 value = _asdict_inner(getattr(obj, f.name), dict_factory)
892 result.append((f.name, value))
893 return dict_factory(result)
894 elif isinstance(obj, (list, tuple)):
895 return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
896 elif isinstance(obj, dict):
897 return type(obj)((_asdict_inner(k, dict_factory), _asdict_inner(v, dict_factory))
898 for k, v in obj.items())
899 else:
900 return deepcopy(obj)
901
902
903def astuple(obj, *, tuple_factory=tuple):
904 """Return the fields of a dataclass instance as a new tuple of field values.
905
906 Example usage::
907
908 @dataclass
909 class C:
910 x: int
911 y: int
912
913 c = C(1, 2)
Raymond Hettingerd55209d2018-01-10 20:56:41 -0800914 assert astuple(c) == (1, 2)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500915
916 If given, 'tuple_factory' will be used instead of built-in tuple.
917 The function applies recursively to field values that are
918 dataclass instances. This will also look into built-in containers:
919 tuples, lists, and dicts.
920 """
921
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500922 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500923 raise TypeError("astuple() should be called on dataclass instances")
924 return _astuple_inner(obj, tuple_factory)
925
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500926
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500927def _astuple_inner(obj, tuple_factory):
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500928 if _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500929 result = []
930 for f in fields(obj):
931 value = _astuple_inner(getattr(obj, f.name), tuple_factory)
932 result.append(value)
933 return tuple_factory(result)
934 elif isinstance(obj, (list, tuple)):
935 return type(obj)(_astuple_inner(v, tuple_factory) for v in obj)
936 elif isinstance(obj, dict):
937 return type(obj)((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory))
938 for k, v in obj.items())
939 else:
940 return deepcopy(obj)
941
942
Eric V. Smithd80b4432018-01-06 17:09:58 -0500943def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
Eric V. Smith5da8cfb2018-03-01 08:01:41 -0500944 repr=True, eq=True, order=False, unsafe_hash=False,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500945 frozen=False):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500946 """Return a new dynamically created dataclass.
947
Eric V. Smithed7d4292018-01-06 16:14:03 -0500948 The dataclass name will be 'cls_name'. 'fields' is an iterable
949 of either (name), (name, type) or (name, type, Field) objects. If type is
950 omitted, use the string 'typing.Any'. Field objects are created by
Eric V. Smithd327ae62018-01-07 08:19:45 -0500951 the equivalent of calling 'field(name, type [, Field-info])'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500952
Raymond Hettingerd55209d2018-01-10 20:56:41 -0800953 C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500954
955 is equivalent to:
956
957 @dataclass
958 class C(Base):
Raymond Hettingerd55209d2018-01-10 20:56:41 -0800959 x: 'typing.Any'
960 y: int
961 z: int = field(init=False)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500962
Raymond Hettingerd55209d2018-01-10 20:56:41 -0800963 For the bases and namespace parameters, see the builtin type() function.
Eric V. Smithd80b4432018-01-06 17:09:58 -0500964
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500965 The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to
Eric V. Smithd80b4432018-01-06 17:09:58 -0500966 dataclass().
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500967 """
968
969 if namespace is None:
970 namespace = {}
971 else:
972 # Copy namespace since we're going to mutate it.
973 namespace = namespace.copy()
974
Eric V. Smithd1388922018-01-07 14:30:17 -0500975 anns = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500976 for item in fields:
Eric V. Smithed7d4292018-01-06 16:14:03 -0500977 if isinstance(item, str):
978 name = item
979 tp = 'typing.Any'
980 elif len(item) == 2:
981 name, tp, = item
982 elif len(item) == 3:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500983 name, tp, spec = item
984 namespace[name] = spec
Eric V. Smithed7d4292018-01-06 16:14:03 -0500985 anns[name] = tp
986
987 namespace['__annotations__'] = anns
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500988 cls = type(cls_name, bases, namespace)
Eric V. Smithd80b4432018-01-06 17:09:58 -0500989 return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500990 unsafe_hash=unsafe_hash, frozen=frozen)
991
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500992
993def replace(obj, **changes):
994 """Return a new object replacing specified fields with new values.
995
996 This is especially useful for frozen classes. Example usage:
997
998 @dataclass(frozen=True)
999 class C:
1000 x: int
1001 y: int
1002
1003 c = C(1, 2)
1004 c1 = replace(c, x=3)
1005 assert c1.x == 3 and c1.y == 2
1006 """
1007
1008 # We're going to mutate 'changes', but that's okay because it's a new
1009 # dict, even if called with 'replace(obj, **my_changes)'.
1010
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001011 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001012 raise TypeError("replace() should be called on dataclass instances")
1013
1014 # It's an error to have init=False fields in 'changes'.
1015 # If a field is not in 'changes', read its value from the provided obj.
1016
Eric V. Smithf199bc62018-03-18 20:40:34 -04001017 for f in getattr(obj, _FIELDS).values():
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001018 if not f.init:
1019 # Error if this field is specified in changes.
1020 if f.name in changes:
1021 raise ValueError(f'field {f.name} is declared with '
1022 'init=False, it cannot be specified with '
1023 'replace()')
1024 continue
1025
1026 if f.name not in changes:
1027 changes[f.name] = getattr(obj, f.name)
1028
1029 # Create the new object, which calls __init__() and __post_init__
1030 # (if defined), using all of the init fields we've added and/or
1031 # left in 'changes'.
1032 # If there are values supplied in changes that aren't fields, this
1033 # will correctly raise a TypeError.
1034 return obj.__class__(**changes)