blob: bb77d3b4052b2250cead4f2cb0abe1908454c173 [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
6
7__all__ = ['dataclass',
8 'field',
Eric V. Smith8e4560a2018-03-21 17:10:22 -04009 'Field',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050010 'FrozenInstanceError',
11 'InitVar',
Eric V. Smith03220fd2017-12-29 13:59:58 -050012 'MISSING',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050013
14 # Helper functions.
15 'fields',
16 'asdict',
17 'astuple',
18 'make_dataclass',
19 'replace',
Eric V. Smithe7ba0132018-01-06 12:41:53 -050020 'is_dataclass',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050021 ]
22
Eric V. Smithea8fc522018-01-27 19:07:40 -050023# Conditions for adding methods. The boxes indicate what action the
Eric V. Smithf8e75492018-05-16 05:14:53 -040024# dataclass decorator takes. For all of these tables, when I talk
25# about init=, repr=, eq=, order=, unsafe_hash=, or frozen=, I'm
26# referring to the arguments to the @dataclass decorator. When
27# checking if a dunder method already exists, I mean check for an
28# entry in the class's __dict__. I never check to see if an attribute
29# is defined in a base class.
Eric V. Smithea8fc522018-01-27 19:07:40 -050030
31# Key:
32# +=========+=========================================+
33# + Value | Meaning |
34# +=========+=========================================+
35# | <blank> | No action: no method is added. |
36# +---------+-----------------------------------------+
37# | add | Generated method is added. |
38# +---------+-----------------------------------------+
Eric V. Smithea8fc522018-01-27 19:07:40 -050039# | raise | TypeError is raised. |
40# +---------+-----------------------------------------+
41# | None | Attribute is set to None. |
42# +=========+=========================================+
43
44# __init__
45#
46# +--- init= parameter
47# |
48# v | | |
49# | no | yes | <--- class has __init__ in __dict__?
50# +=======+=======+=======+
51# | False | | |
52# +-------+-------+-------+
53# | True | add | | <- the default
54# +=======+=======+=======+
55
56# __repr__
57#
58# +--- repr= parameter
59# |
60# v | | |
61# | no | yes | <--- class has __repr__ in __dict__?
62# +=======+=======+=======+
63# | False | | |
64# +-------+-------+-------+
65# | True | add | | <- the default
66# +=======+=======+=======+
67
68
69# __setattr__
70# __delattr__
71#
72# +--- frozen= parameter
73# |
74# v | | |
75# | no | yes | <--- class has __setattr__ or __delattr__ in __dict__?
76# +=======+=======+=======+
77# | False | | | <- the default
78# +-------+-------+-------+
79# | True | add | raise |
80# +=======+=======+=======+
81# Raise because not adding these methods would break the "frozen-ness"
Eric V. Smithf8e75492018-05-16 05:14:53 -040082# of the class.
Eric V. Smithea8fc522018-01-27 19:07:40 -050083
84# __eq__
85#
86# +--- eq= parameter
87# |
88# v | | |
89# | no | yes | <--- class has __eq__ in __dict__?
90# +=======+=======+=======+
91# | False | | |
92# +-------+-------+-------+
93# | True | add | | <- the default
94# +=======+=======+=======+
95
96# __lt__
97# __le__
98# __gt__
99# __ge__
100#
101# +--- order= parameter
102# |
103# v | | |
104# | no | yes | <--- class has any comparison method in __dict__?
105# +=======+=======+=======+
106# | False | | | <- the default
107# +-------+-------+-------+
108# | True | add | raise |
109# +=======+=======+=======+
110# Raise because to allow this case would interfere with using
Eric V. Smithf8e75492018-05-16 05:14:53 -0400111# functools.total_ordering.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500112
113# __hash__
114
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500115# +------------------- unsafe_hash= parameter
116# | +----------- eq= parameter
117# | | +--- frozen= parameter
118# | | |
119# v v v | | |
120# | no | yes | <--- class has explicitly defined __hash__
121# +=======+=======+=======+========+========+
122# | False | False | False | | | No __eq__, use the base class __hash__
123# +-------+-------+-------+--------+--------+
124# | False | False | True | | | No __eq__, use the base class __hash__
125# +-------+-------+-------+--------+--------+
126# | False | True | False | None | | <-- the default, not hashable
127# +-------+-------+-------+--------+--------+
128# | False | True | True | add | | Frozen, so hashable, allows override
129# +-------+-------+-------+--------+--------+
130# | True | False | False | add | raise | Has no __eq__, but hashable
131# +-------+-------+-------+--------+--------+
132# | True | False | True | add | raise | Has no __eq__, but hashable
133# +-------+-------+-------+--------+--------+
134# | True | True | False | add | raise | Not frozen, but hashable
135# +-------+-------+-------+--------+--------+
136# | True | True | True | add | raise | Frozen, so hashable
137# +=======+=======+=======+========+========+
Eric V. Smithea8fc522018-01-27 19:07:40 -0500138# For boxes that are blank, __hash__ is untouched and therefore
Eric V. Smithf8e75492018-05-16 05:14:53 -0400139# inherited from the base class. If the base is object, then
140# id-based hashing is used.
141#
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400142# Note that a class may already have __hash__=None if it specified an
Eric V. Smithf8e75492018-05-16 05:14:53 -0400143# __eq__ method in the class body (not one that was created by
144# @dataclass).
145#
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500146# See _hash_action (below) for a coded version of this table.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500147
148
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500149# Raised when an attempt is made to modify a frozen class.
150class FrozenInstanceError(AttributeError): pass
151
Eric V. Smithf8e75492018-05-16 05:14:53 -0400152# A sentinel object for default values to signal that a default
153# factory will be used. This is given a nice repr() which will appear
154# in the function signature of dataclasses' constructors.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500155class _HAS_DEFAULT_FACTORY_CLASS:
156 def __repr__(self):
157 return '<factory>'
158_HAS_DEFAULT_FACTORY = _HAS_DEFAULT_FACTORY_CLASS()
159
Eric V. Smith03220fd2017-12-29 13:59:58 -0500160# A sentinel object to detect if a parameter is supplied or not. Use
Eric V. Smithf8e75492018-05-16 05:14:53 -0400161# a class to give it a better repr.
Eric V. Smith03220fd2017-12-29 13:59:58 -0500162class _MISSING_TYPE:
163 pass
164MISSING = _MISSING_TYPE()
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500165
166# Since most per-field metadata will be unused, create an empty
Eric V. Smithf8e75492018-05-16 05:14:53 -0400167# read-only proxy that can be shared among all fields.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500168_EMPTY_METADATA = types.MappingProxyType({})
169
170# Markers for the various kinds of fields and pseudo-fields.
Eric V. Smith01abc6e2018-05-15 08:36:21 -0400171class _FIELD_BASE:
172 def __init__(self, name):
173 self.name = name
174 def __repr__(self):
175 return self.name
176_FIELD = _FIELD_BASE('_FIELD')
177_FIELD_CLASSVAR = _FIELD_BASE('_FIELD_CLASSVAR')
178_FIELD_INITVAR = _FIELD_BASE('_FIELD_INITVAR')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500179
180# The name of an attribute on the class where we store the Field
Eric V. Smithf8e75492018-05-16 05:14:53 -0400181# objects. Also used to check if a class is a Data Class.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400182_FIELDS = '__dataclass_fields__'
183
184# The name of an attribute on the class that stores the parameters to
185# @dataclass.
186_PARAMS = '__dataclass_params__'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500187
188# The name of the function, that if it exists, is called at the end of
189# __init__.
190_POST_INIT_NAME = '__post_init__'
191
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400192# String regex that string annotations for ClassVar or InitVar must match.
193# Allows "identifier.identifier[" or "identifier[".
194# https://bugs.python.org/issue33453 for details.
195_MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500196
197class _InitVarMeta(type):
198 def __getitem__(self, params):
199 return self
200
201class InitVar(metaclass=_InitVarMeta):
202 pass
203
204
205# Instances of Field are only ever created from within this module,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400206# and only from the field() function, although Field instances are
207# exposed externally as (conceptually) read-only objects.
208#
209# name and type are filled in after the fact, not in __init__.
210# They're not known at the time this class is instantiated, but it's
211# convenient if they're available later.
212#
Eric V. Smithf199bc62018-03-18 20:40:34 -0400213# When cls._FIELDS is filled in with a list of Field objects, the name
Eric V. Smithf8e75492018-05-16 05:14:53 -0400214# and type fields will have been populated.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500215class Field:
216 __slots__ = ('name',
217 'type',
218 'default',
219 'default_factory',
220 'repr',
221 'hash',
222 'init',
223 'compare',
224 'metadata',
225 '_field_type', # Private: not to be used by user code.
226 )
227
228 def __init__(self, default, default_factory, init, repr, hash, compare,
229 metadata):
230 self.name = None
231 self.type = None
232 self.default = default
233 self.default_factory = default_factory
234 self.init = init
235 self.repr = repr
236 self.hash = hash
237 self.compare = compare
238 self.metadata = (_EMPTY_METADATA
239 if metadata is None or len(metadata) == 0 else
240 types.MappingProxyType(metadata))
241 self._field_type = None
242
243 def __repr__(self):
244 return ('Field('
245 f'name={self.name!r},'
Eric V. Smith2473eea2018-05-14 11:37:28 -0400246 f'type={self.type!r},'
247 f'default={self.default!r},'
248 f'default_factory={self.default_factory!r},'
249 f'init={self.init!r},'
250 f'repr={self.repr!r},'
251 f'hash={self.hash!r},'
252 f'compare={self.compare!r},'
Eric V. Smith01abc6e2018-05-15 08:36:21 -0400253 f'metadata={self.metadata!r},'
254 f'_field_type={self._field_type}'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500255 ')')
256
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400257 # This is used to support the PEP 487 __set_name__ protocol in the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400258 # case where we're using a field that contains a descriptor as a
259 # defaul value. For details on __set_name__, see
260 # https://www.python.org/dev/peps/pep-0487/#implementation-details.
261 #
262 # Note that in _process_class, this Field object is overwritten
263 # with the default value, so the end result is a descriptor that
264 # had __set_name__ called on it at the right time.
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400265 def __set_name__(self, owner, name):
Eric V. Smith52199522018-03-29 11:07:48 -0400266 func = getattr(type(self.default), '__set_name__', None)
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400267 if func:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400268 # There is a __set_name__ method on the descriptor, call
269 # it.
Eric V. Smith52199522018-03-29 11:07:48 -0400270 func(self.default, owner, name)
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400271
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500272
Eric V. Smithf199bc62018-03-18 20:40:34 -0400273class _DataclassParams:
274 __slots__ = ('init',
275 'repr',
276 'eq',
277 'order',
278 'unsafe_hash',
279 'frozen',
280 )
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400281
Eric V. Smithf199bc62018-03-18 20:40:34 -0400282 def __init__(self, init, repr, eq, order, unsafe_hash, frozen):
283 self.init = init
284 self.repr = repr
285 self.eq = eq
286 self.order = order
287 self.unsafe_hash = unsafe_hash
288 self.frozen = frozen
289
290 def __repr__(self):
291 return ('_DataclassParams('
Eric V. Smith30590422018-05-14 17:16:52 -0400292 f'init={self.init!r},'
293 f'repr={self.repr!r},'
294 f'eq={self.eq!r},'
295 f'order={self.order!r},'
296 f'unsafe_hash={self.unsafe_hash!r},'
297 f'frozen={self.frozen!r}'
Eric V. Smithf199bc62018-03-18 20:40:34 -0400298 ')')
299
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400300
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500301# This function is used instead of exposing Field creation directly,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400302# so that a type checker can be told (via overloads) that this is a
303# function whose type depends on its parameters.
Eric V. Smith03220fd2017-12-29 13:59:58 -0500304def field(*, default=MISSING, default_factory=MISSING, init=True, repr=True,
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500305 hash=None, compare=True, metadata=None):
306 """Return an object to identify dataclass fields.
307
Eric V. Smithf8e75492018-05-16 05:14:53 -0400308 default is the default value of the field. default_factory is a
309 0-argument function called to initialize a field's value. If init
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500310 is True, the field will be a parameter to the class's __init__()
Eric V. Smithf8e75492018-05-16 05:14:53 -0400311 function. If repr is True, the field will be included in the
312 object's repr(). If hash is True, the field will be included in
313 the object's hash(). If compare is True, the field will be used
314 in comparison functions. metadata, if specified, must be a
315 mapping which is stored but not otherwise examined by dataclass.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500316
317 It is an error to specify both default and default_factory.
318 """
319
Eric V. Smith03220fd2017-12-29 13:59:58 -0500320 if default is not MISSING and default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500321 raise ValueError('cannot specify both default and default_factory')
322 return Field(default, default_factory, init, repr, hash, compare,
323 metadata)
324
325
326def _tuple_str(obj_name, fields):
327 # Return a string representing each field of obj_name as a tuple
Eric V. Smithf8e75492018-05-16 05:14:53 -0400328 # member. So, if fields is ['x', 'y'] and obj_name is "self",
329 # return "(self.x,self.y)".
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500330
331 # Special case for the 0-tuple.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500332 if not fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500333 return '()'
334 # Note the trailing comma, needed if this turns out to be a 1-tuple.
335 return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)'
336
337
Eric V. Smithea8fc522018-01-27 19:07:40 -0500338def _create_fn(name, args, body, *, globals=None, locals=None,
Eric V. Smith03220fd2017-12-29 13:59:58 -0500339 return_type=MISSING):
Eric V. Smithf8e75492018-05-16 05:14:53 -0400340 # Note that we mutate locals when exec() is called. Caller
341 # beware! The only callers are internal to this module, so no
342 # worries about external callers.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500343 if locals is None:
344 locals = {}
345 return_annotation = ''
Eric V. Smith03220fd2017-12-29 13:59:58 -0500346 if return_type is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500347 locals['_return_type'] = return_type
348 return_annotation = '->_return_type'
349 args = ','.join(args)
350 body = '\n'.join(f' {b}' for b in body)
351
Eric V. Smithf199bc62018-03-18 20:40:34 -0400352 # Compute the text of the entire function.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500353 txt = f'def {name}({args}){return_annotation}:\n{body}'
354
355 exec(txt, globals, locals)
356 return locals[name]
357
358
359def _field_assign(frozen, name, value, self_name):
360 # If we're a frozen class, then assign to our fields in __init__
Eric V. Smithf8e75492018-05-16 05:14:53 -0400361 # via object.__setattr__. Otherwise, just use a simple
362 # assignment.
363 #
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500364 # self_name is what "self" is called in this function: don't
Eric V. Smithf8e75492018-05-16 05:14:53 -0400365 # hard-code "self", since that might be a field name.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500366 if frozen:
367 return f'object.__setattr__({self_name},{name!r},{value})'
368 return f'{self_name}.{name}={value}'
369
370
371def _field_init(f, frozen, globals, self_name):
372 # Return the text of the line in the body of __init__ that will
Eric V. Smithf8e75492018-05-16 05:14:53 -0400373 # initialize this field.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500374
375 default_name = f'_dflt_{f.name}'
Eric V. Smith03220fd2017-12-29 13:59:58 -0500376 if f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500377 if f.init:
378 # This field has a default factory. If a parameter is
Eric V. Smithf8e75492018-05-16 05:14:53 -0400379 # given, use it. If not, call the factory.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500380 globals[default_name] = f.default_factory
381 value = (f'{default_name}() '
382 f'if {f.name} is _HAS_DEFAULT_FACTORY '
383 f'else {f.name}')
384 else:
385 # This is a field that's not in the __init__ params, but
Eric V. Smithf8e75492018-05-16 05:14:53 -0400386 # has a default factory function. It needs to be
387 # initialized here by calling the factory function,
388 # because there's no other way to initialize it.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500389
390 # For a field initialized with a default=defaultvalue, the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400391 # class dict just has the default value
392 # (cls.fieldname=defaultvalue). But that won't work for a
393 # default factory, the factory must be called in __init__
394 # and we must assign that to self.fieldname. We can't
395 # fall back to the class dict's value, both because it's
396 # not set, and because it might be different per-class
397 # (which, after all, is why we have a factory function!).
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500398
399 globals[default_name] = f.default_factory
400 value = f'{default_name}()'
401 else:
402 # No default factory.
403 if f.init:
Eric V. Smith03220fd2017-12-29 13:59:58 -0500404 if f.default is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500405 # There's no default, just do an assignment.
406 value = f.name
Eric V. Smith03220fd2017-12-29 13:59:58 -0500407 elif f.default is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500408 globals[default_name] = f.default
409 value = f.name
410 else:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400411 # This field does not need initialization. Signify that
412 # to the caller by returning None.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500413 return None
414
415 # Only test this now, so that we can create variables for the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400416 # default. However, return None to signify that we're not going
417 # to actually do the assignment statement for InitVars.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500418 if f._field_type == _FIELD_INITVAR:
419 return None
420
421 # Now, actually generate the field assignment.
422 return _field_assign(frozen, f.name, value, self_name)
423
424
425def _init_param(f):
Eric V. Smithf8e75492018-05-16 05:14:53 -0400426 # Return the __init__ parameter string for this field. For
427 # example, the equivalent of 'x:int=3' (except instead of 'int',
428 # reference a variable set to int, and instead of '3', reference a
429 # variable set to 3).
Eric V. Smith03220fd2017-12-29 13:59:58 -0500430 if f.default is MISSING and f.default_factory is MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400431 # There's no default, and no default_factory, just output the
432 # variable name and type.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500433 default = ''
Eric V. Smith03220fd2017-12-29 13:59:58 -0500434 elif f.default is not MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400435 # There's a default, this will be the name that's used to look
436 # it up.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500437 default = f'=_dflt_{f.name}'
Eric V. Smith03220fd2017-12-29 13:59:58 -0500438 elif f.default_factory is not MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400439 # There's a factory function. Set a marker.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500440 default = '=_HAS_DEFAULT_FACTORY'
441 return f'{f.name}:_type_{f.name}{default}'
442
443
444def _init_fn(fields, frozen, has_post_init, self_name):
445 # fields contains both real fields and InitVar pseudo-fields.
446
447 # Make sure we don't have fields without defaults following fields
Eric V. Smithf8e75492018-05-16 05:14:53 -0400448 # with defaults. This actually would be caught when exec-ing the
449 # function source code, but catching it here gives a better error
450 # message, and future-proofs us in case we build up the function
451 # using ast.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500452 seen_default = False
453 for f in fields:
454 # Only consider fields in the __init__ call.
455 if f.init:
Eric V. Smith03220fd2017-12-29 13:59:58 -0500456 if not (f.default is MISSING and f.default_factory is MISSING):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500457 seen_default = True
458 elif seen_default:
459 raise TypeError(f'non-default argument {f.name!r} '
460 'follows default argument')
461
Eric V. Smith03220fd2017-12-29 13:59:58 -0500462 globals = {'MISSING': MISSING,
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500463 '_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY}
464
465 body_lines = []
466 for f in fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500467 line = _field_init(f, frozen, globals, self_name)
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400468 # line is None means that this field doesn't require
Eric V. Smithf8e75492018-05-16 05:14:53 -0400469 # initialization (it's a pseudo-field). Just skip it.
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400470 if line:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500471 body_lines.append(line)
472
473 # Does this class have a post-init function?
474 if has_post_init:
475 params_str = ','.join(f.name for f in fields
476 if f._field_type is _FIELD_INITVAR)
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400477 body_lines.append(f'{self_name}.{_POST_INIT_NAME}({params_str})')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500478
479 # If no body lines, use 'pass'.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500480 if not body_lines:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500481 body_lines = ['pass']
482
483 locals = {f'_type_{f.name}': f.type for f in fields}
484 return _create_fn('__init__',
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400485 [self_name] + [_init_param(f) for f in fields if f.init],
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500486 body_lines,
487 locals=locals,
488 globals=globals,
489 return_type=None)
490
491
492def _repr_fn(fields):
493 return _create_fn('__repr__',
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400494 ('self',),
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500495 ['return self.__class__.__qualname__ + f"(' +
496 ', '.join([f"{f.name}={{self.{f.name}!r}}"
497 for f in fields]) +
498 ')"'])
499
500
Eric V. Smithf199bc62018-03-18 20:40:34 -0400501def _frozen_get_del_attr(cls, fields):
Eric V. Smithf8e75492018-05-16 05:14:53 -0400502 # XXX: globals is modified on the first call to _create_fn, then
503 # the modified version is used in the second call. Is this okay?
Eric V. Smithf199bc62018-03-18 20:40:34 -0400504 globals = {'cls': cls,
505 'FrozenInstanceError': FrozenInstanceError}
506 if fields:
507 fields_str = '(' + ','.join(repr(f.name) for f in fields) + ',)'
508 else:
509 # Special case for the zero-length tuple.
510 fields_str = '()'
511 return (_create_fn('__setattr__',
512 ('self', 'name', 'value'),
513 (f'if type(self) is cls or name in {fields_str}:',
514 ' raise FrozenInstanceError(f"cannot assign to field {name!r}")',
515 f'super(cls, self).__setattr__(name, value)'),
516 globals=globals),
517 _create_fn('__delattr__',
518 ('self', 'name'),
519 (f'if type(self) is cls or name in {fields_str}:',
520 ' raise FrozenInstanceError(f"cannot delete field {name!r}")',
521 f'super(cls, self).__delattr__(name)'),
522 globals=globals),
523 )
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500524
525
526def _cmp_fn(name, op, self_tuple, other_tuple):
527 # Create a comparison function. If the fields in the object are
Eric V. Smithf8e75492018-05-16 05:14:53 -0400528 # named 'x' and 'y', then self_tuple is the string
529 # '(self.x,self.y)' and other_tuple is the string
530 # '(other.x,other.y)'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500531
532 return _create_fn(name,
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400533 ('self', 'other'),
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500534 [ 'if other.__class__ is self.__class__:',
535 f' return {self_tuple}{op}{other_tuple}',
536 'return NotImplemented'])
537
538
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500539def _hash_fn(fields):
540 self_tuple = _tuple_str('self', fields)
541 return _create_fn('__hash__',
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400542 ('self',),
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500543 [f'return hash({self_tuple})'])
544
545
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400546def _is_classvar(a_type, typing):
Eric V. Smith92858352018-05-16 07:24:00 -0400547 # This test uses a typing internal class, but it's the best way to
548 # test if this is a ClassVar.
549 return (a_type is typing.ClassVar
550 or (type(a_type) is typing._GenericAlias
551 and a_type.__origin__ is typing.ClassVar))
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400552
553
554def _is_initvar(a_type, dataclasses):
555 # The module we're checking against is the module we're
556 # currently in (dataclasses.py).
557 return a_type is dataclasses.InitVar
558
559
560def _is_type(annotation, cls, a_module, a_type, is_type_predicate):
561 # Given a type annotation string, does it refer to a_type in
562 # a_module? For example, when checking that annotation denotes a
563 # ClassVar, then a_module is typing, and a_type is
564 # typing.ClassVar.
565
566 # It's possible to look up a_module given a_type, but it involves
567 # looking in sys.modules (again!), and seems like a waste since
568 # the caller already knows a_module.
569
570 # - annotation is a string type annotation
571 # - cls is the class that this annotation was found in
572 # - a_module is the module we want to match
573 # - a_type is the type in that module we want to match
574 # - is_type_predicate is a function called with (obj, a_module)
575 # that determines if obj is of the desired type.
576
577 # Since this test does not do a local namespace lookup (and
578 # instead only a module (global) lookup), there are some things it
579 # gets wrong.
580
Eric V. Smithf8e75492018-05-16 05:14:53 -0400581 # With string annotations, cv0 will be detected as a ClassVar:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400582 # CV = ClassVar
583 # @dataclass
584 # class C0:
585 # cv0: CV
586
Eric V. Smithf8e75492018-05-16 05:14:53 -0400587 # But in this example cv1 will not be detected as a ClassVar:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400588 # @dataclass
589 # class C1:
590 # CV = ClassVar
591 # cv1: CV
592
Eric V. Smithf8e75492018-05-16 05:14:53 -0400593 # In C1, the code in this function (_is_type) will look up "CV" in
594 # the module and not find it, so it will not consider cv1 as a
595 # ClassVar. This is a fairly obscure corner case, and the best
596 # way to fix it would be to eval() the string "CV" with the
597 # correct global and local namespaces. However that would involve
598 # a eval() penalty for every single field of every dataclass
599 # that's defined. It was judged not worth it.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400600
601 match = _MODULE_IDENTIFIER_RE.match(annotation)
602 if match:
603 ns = None
604 module_name = match.group(1)
605 if not module_name:
606 # No module name, assume the class's module did
607 # "from dataclasses import InitVar".
608 ns = sys.modules.get(cls.__module__).__dict__
609 else:
610 # Look up module_name in the class's module.
611 module = sys.modules.get(cls.__module__)
612 if module and module.__dict__.get(module_name) is a_module:
613 ns = sys.modules.get(a_type.__module__).__dict__
614 if ns and is_type_predicate(ns.get(match.group(2)), a_module):
615 return True
616 return False
617
618
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500619def _get_field(cls, a_name, a_type):
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400620 # Return a Field object for this field name and type. ClassVars
Eric V. Smithf8e75492018-05-16 05:14:53 -0400621 # and InitVars are also returned, but marked as such (see
622 # f._field_type).
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500623
Eric V. Smithf8e75492018-05-16 05:14:53 -0400624 # If the default value isn't derived from Field, then it's only a
625 # normal default value. Convert it to a Field().
Eric V. Smith03220fd2017-12-29 13:59:58 -0500626 default = getattr(cls, a_name, MISSING)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500627 if isinstance(default, Field):
628 f = default
629 else:
Eric V. Smith7389fd92018-03-19 21:07:51 -0400630 if isinstance(default, types.MemberDescriptorType):
631 # This is a field in __slots__, so it has no default value.
632 default = MISSING
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500633 f = field(default=default)
634
Eric V. Smithf8e75492018-05-16 05:14:53 -0400635 # Only at this point do we know the name and the type. Set them.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500636 f.name = a_name
637 f.type = a_type
638
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400639 # Assume it's a normal field until proven otherwise. We're next
Eric V. Smithf8e75492018-05-16 05:14:53 -0400640 # going to decide if it's a ClassVar or InitVar, everything else
641 # is just a normal field.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400642 f._field_type = _FIELD
643
644 # In addition to checking for actual types here, also check for
Eric V. Smithf8e75492018-05-16 05:14:53 -0400645 # string annotations. get_type_hints() won't always work for us
646 # (see https://github.com/python/typing/issues/508 for example),
647 # plus it's expensive and would require an eval for every stirng
648 # annotation. So, make a best effort to see if this is a ClassVar
649 # or InitVar using regex's and checking that the thing referenced
650 # is actually of the correct type.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400651
652 # For the complete discussion, see https://bugs.python.org/issue33453
653
654 # If typing has not been imported, then it's impossible for any
Eric V. Smithf8e75492018-05-16 05:14:53 -0400655 # annotation to be a ClassVar. So, only look for ClassVar if
656 # typing has been imported by any module (not necessarily cls's
657 # module).
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500658 typing = sys.modules.get('typing')
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400659 if typing:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400660 if (_is_classvar(a_type, typing)
661 or (isinstance(f.type, str)
662 and _is_type(f.type, cls, typing, typing.ClassVar,
663 _is_classvar))):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500664 f._field_type = _FIELD_CLASSVAR
665
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400666 # If the type is InitVar, or if it's a matching string annotation,
667 # then it's an InitVar.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500668 if f._field_type is _FIELD:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400669 # The module we're checking against is the module we're
670 # currently in (dataclasses.py).
671 dataclasses = sys.modules[__name__]
672 if (_is_initvar(a_type, dataclasses)
673 or (isinstance(f.type, str)
674 and _is_type(f.type, cls, dataclasses, dataclasses.InitVar,
675 _is_initvar))):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500676 f._field_type = _FIELD_INITVAR
677
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400678 # Validations for individual fields. This is delayed until now,
679 # instead of in the Field() constructor, since only here do we
680 # know the field name, which allows for better error reporting.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500681
682 # Special restrictions for ClassVar and InitVar.
683 if f._field_type in (_FIELD_CLASSVAR, _FIELD_INITVAR):
Eric V. Smith03220fd2017-12-29 13:59:58 -0500684 if f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500685 raise TypeError(f'field {f.name} cannot have a '
686 'default factory')
687 # Should I check for other field settings? default_factory
Eric V. Smithf8e75492018-05-16 05:14:53 -0400688 # seems the most serious to check for. Maybe add others. For
689 # example, how about init=False (or really,
690 # init=<not-the-default-init-value>)? It makes no sense for
691 # ClassVar and InitVar to specify init=<anything>.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500692
693 # For real fields, disallow mutable defaults for known types.
694 if f._field_type is _FIELD and isinstance(f.default, (list, dict, set)):
695 raise ValueError(f'mutable default {type(f.default)} for field '
696 f'{f.name} is not allowed: use default_factory')
697
698 return f
699
700
Eric V. Smithea8fc522018-01-27 19:07:40 -0500701def _set_new_attribute(cls, name, value):
702 # Never overwrites an existing attribute. Returns True if the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400703 # attribute already exists.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500704 if name in cls.__dict__:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500705 return True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500706 setattr(cls, name, value)
Eric V. Smithea8fc522018-01-27 19:07:40 -0500707 return False
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500708
709
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500710# Decide if/how we're going to create a hash function. Key is
Eric V. Smithf8e75492018-05-16 05:14:53 -0400711# (unsafe_hash, eq, frozen, does-hash-exist). Value is the action to
712# take. The common case is to do nothing, so instead of providing a
713# function that is a no-op, use None to signify that.
Eric V. Smith01d618c2018-03-24 22:10:14 -0400714
715def _hash_set_none(cls, fields):
716 return None
717
718def _hash_add(cls, fields):
719 flds = [f for f in fields if (f.compare if f.hash is None else f.hash)]
720 return _hash_fn(flds)
721
722def _hash_exception(cls, fields):
723 # Raise an exception.
724 raise TypeError(f'Cannot overwrite attribute __hash__ '
725 f'in class {cls.__name__}')
726
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500727#
728# +-------------------------------------- unsafe_hash?
729# | +------------------------------- eq?
730# | | +------------------------ frozen?
731# | | | +---------------- has-explicit-hash?
732# | | | |
733# | | | | +------- action
734# | | | | |
735# v v v v v
Eric V. Smith01d618c2018-03-24 22:10:14 -0400736_hash_action = {(False, False, False, False): None,
737 (False, False, False, True ): None,
738 (False, False, True, False): None,
739 (False, False, True, True ): None,
740 (False, True, False, False): _hash_set_none,
741 (False, True, False, True ): None,
742 (False, True, True, False): _hash_add,
743 (False, True, True, True ): None,
744 (True, False, False, False): _hash_add,
745 (True, False, False, True ): _hash_exception,
746 (True, False, True, False): _hash_add,
747 (True, False, True, True ): _hash_exception,
748 (True, True, False, False): _hash_add,
749 (True, True, False, True ): _hash_exception,
750 (True, True, True, False): _hash_add,
751 (True, True, True, True ): _hash_exception,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500752 }
753# See https://bugs.python.org/issue32929#msg312829 for an if-statement
Eric V. Smithf8e75492018-05-16 05:14:53 -0400754# version of this table.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500755
756
Eric V. Smithf199bc62018-03-18 20:40:34 -0400757def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
Eric V. Smithd1388922018-01-07 14:30:17 -0500758 # Now that dicts retain insertion order, there's no reason to use
Eric V. Smithf8e75492018-05-16 05:14:53 -0400759 # an ordered dict. I am leveraging that ordering here, because
760 # derived class fields overwrite base class fields, but the order
761 # is defined by the base class, which is found first.
Eric V. Smithd1388922018-01-07 14:30:17 -0500762 fields = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500763
Eric V. Smithf199bc62018-03-18 20:40:34 -0400764 setattr(cls, _PARAMS, _DataclassParams(init, repr, eq, order,
765 unsafe_hash, frozen))
766
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500767 # Find our base classes in reverse MRO order, and exclude
Eric V. Smithf8e75492018-05-16 05:14:53 -0400768 # ourselves. In reversed order so that more derived classes
769 # override earlier field definitions in base classes. As long as
770 # we're iterating over them, see if any are frozen.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400771 any_frozen_base = False
772 has_dataclass_bases = False
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500773 for b in cls.__mro__[-1:0:-1]:
774 # Only process classes that have been processed by our
Eric V. Smithf8e75492018-05-16 05:14:53 -0400775 # decorator. That is, they have a _FIELDS attribute.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400776 base_fields = getattr(b, _FIELDS, None)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500777 if base_fields:
Eric V. Smithf199bc62018-03-18 20:40:34 -0400778 has_dataclass_bases = True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500779 for f in base_fields.values():
780 fields[f.name] = f
Eric V. Smithf199bc62018-03-18 20:40:34 -0400781 if getattr(b, _PARAMS).frozen:
782 any_frozen_base = True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500783
Eric V. Smith56970b82018-03-22 16:28:48 -0400784 # Annotations that are defined in this class (not in base
Eric V. Smithf8e75492018-05-16 05:14:53 -0400785 # classes). If __annotations__ isn't present, then this class
786 # adds no new annotations. We use this to compute fields that are
787 # added by this class.
788 #
Eric V. Smith56970b82018-03-22 16:28:48 -0400789 # Fields are found from cls_annotations, which is guaranteed to be
Eric V. Smithf8e75492018-05-16 05:14:53 -0400790 # ordered. Default values are from class attributes, if a field
791 # has a default. If the default value is a Field(), then it
792 # contains additional info beyond (and possibly including) the
793 # actual default value. Pseudo-fields ClassVars and InitVars are
794 # included, despite the fact that they're not real fields. That's
795 # dealt with later.
Eric V. Smith56970b82018-03-22 16:28:48 -0400796 cls_annotations = cls.__dict__.get('__annotations__', {})
797
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500798 # Now find fields in our class. While doing so, validate some
Eric V. Smithf8e75492018-05-16 05:14:53 -0400799 # things, and set the default values (as class attributes) where
800 # we can.
Eric V. Smith56970b82018-03-22 16:28:48 -0400801 cls_fields = [_get_field(cls, name, type)
802 for name, type in cls_annotations.items()]
803 for f in cls_fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500804 fields[f.name] = f
805
Eric V. Smithf8e75492018-05-16 05:14:53 -0400806 # If the class attribute (which is the default value for this
807 # field) exists and is of type 'Field', replace it with the
808 # real default. This is so that normal class introspection
809 # sees a real default value, not a Field.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500810 if isinstance(getattr(cls, f.name, None), Field):
Eric V. Smith03220fd2017-12-29 13:59:58 -0500811 if f.default is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500812 # If there's no default, delete the class attribute.
Eric V. Smithf8e75492018-05-16 05:14:53 -0400813 # This happens if we specify field(repr=False), for
814 # example (that is, we specified a field object, but
815 # no default value). Also if we're using a default
816 # factory. The class attribute should not be set at
817 # all in the post-processed class.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500818 delattr(cls, f.name)
819 else:
820 setattr(cls, f.name, f.default)
821
Eric V. Smith56970b82018-03-22 16:28:48 -0400822 # Do we have any Field members that don't also have annotations?
823 for name, value in cls.__dict__.items():
824 if isinstance(value, Field) and not name in cls_annotations:
825 raise TypeError(f'{name!r} is a field but has no type annotation')
826
Eric V. Smithf199bc62018-03-18 20:40:34 -0400827 # Check rules that apply if we are derived from any dataclasses.
828 if has_dataclass_bases:
829 # Raise an exception if any of our bases are frozen, but we're not.
830 if any_frozen_base and not frozen:
831 raise TypeError('cannot inherit non-frozen dataclass from a '
832 'frozen one')
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500833
Eric V. Smithf199bc62018-03-18 20:40:34 -0400834 # Raise an exception if we're frozen, but none of our bases are.
835 if not any_frozen_base and frozen:
836 raise TypeError('cannot inherit frozen dataclass from a '
837 'non-frozen one')
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500838
Eric V. Smithf8e75492018-05-16 05:14:53 -0400839 # Remember all of the fields on our class (including bases). This
840 # also marks this class as being a dataclass.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400841 setattr(cls, _FIELDS, fields)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500842
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500843 # Was this class defined with an explicit __hash__? Note that if
Eric V. Smithf8e75492018-05-16 05:14:53 -0400844 # __eq__ is defined in this class, then python will automatically
845 # set __hash__ to None. This is a heuristic, as it's possible
846 # that such a __hash__ == None was not auto-generated, but it
847 # close enough.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500848 class_hash = cls.__dict__.get('__hash__', MISSING)
849 has_explicit_hash = not (class_hash is MISSING or
850 (class_hash is None and '__eq__' in cls.__dict__))
Eric V. Smithea8fc522018-01-27 19:07:40 -0500851
Eric V. Smithf8e75492018-05-16 05:14:53 -0400852 # If we're generating ordering methods, we must be generating the
853 # eq methods.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500854 if order and not eq:
855 raise ValueError('eq must be true if order is true')
856
857 if init:
858 # Does this class have a post-init function?
859 has_post_init = hasattr(cls, _POST_INIT_NAME)
860
861 # Include InitVars and regular fields (so, not ClassVars).
Eric V. Smithea8fc522018-01-27 19:07:40 -0500862 flds = [f for f in fields.values()
863 if f._field_type in (_FIELD, _FIELD_INITVAR)]
864 _set_new_attribute(cls, '__init__',
865 _init_fn(flds,
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500866 frozen,
Eric V. Smithea8fc522018-01-27 19:07:40 -0500867 has_post_init,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400868 # The name to use for the "self"
869 # param in __init__. Use "self"
870 # if possible.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500871 '__dataclass_self__' if 'self' in fields
872 else 'self',
873 ))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500874
875 # Get the fields as a list, and include only real fields. This is
Eric V. Smithf8e75492018-05-16 05:14:53 -0400876 # used in all of the following methods.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500877 field_list = [f for f in fields.values() if f._field_type is _FIELD]
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500878
879 if repr:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500880 flds = [f for f in field_list if f.repr]
881 _set_new_attribute(cls, '__repr__', _repr_fn(flds))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500882
883 if eq:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500884 # Create _eq__ method. There's no need for a __ne__ method,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400885 # since python will call __eq__ and negate it.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500886 flds = [f for f in field_list if f.compare]
887 self_tuple = _tuple_str('self', flds)
888 other_tuple = _tuple_str('other', flds)
889 _set_new_attribute(cls, '__eq__',
890 _cmp_fn('__eq__', '==',
891 self_tuple, other_tuple))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500892
893 if order:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500894 # Create and set the ordering methods.
895 flds = [f for f in field_list if f.compare]
896 self_tuple = _tuple_str('self', flds)
897 other_tuple = _tuple_str('other', flds)
898 for name, op in [('__lt__', '<'),
899 ('__le__', '<='),
900 ('__gt__', '>'),
901 ('__ge__', '>='),
902 ]:
903 if _set_new_attribute(cls, name,
904 _cmp_fn(name, op, self_tuple, other_tuple)):
905 raise TypeError(f'Cannot overwrite attribute {name} '
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500906 f'in class {cls.__name__}. Consider using '
Eric V. Smithea8fc522018-01-27 19:07:40 -0500907 'functools.total_ordering')
908
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500909 if frozen:
Eric V. Smithf199bc62018-03-18 20:40:34 -0400910 for fn in _frozen_get_del_attr(cls, field_list):
911 if _set_new_attribute(cls, fn.__name__, fn):
912 raise TypeError(f'Cannot overwrite attribute {fn.__name__} '
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500913 f'in class {cls.__name__}')
Eric V. Smithea8fc522018-01-27 19:07:40 -0500914
915 # Decide if/how we're going to create a hash function.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500916 hash_action = _hash_action[bool(unsafe_hash),
917 bool(eq),
918 bool(frozen),
919 has_explicit_hash]
Eric V. Smith01d618c2018-03-24 22:10:14 -0400920 if hash_action:
921 # No need to call _set_new_attribute here, since by the time
Eric V. Smithf8e75492018-05-16 05:14:53 -0400922 # we're here the overwriting is unconditional.
Eric V. Smith01d618c2018-03-24 22:10:14 -0400923 cls.__hash__ = hash_action(cls, field_list)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500924
925 if not getattr(cls, '__doc__'):
926 # Create a class doc-string.
927 cls.__doc__ = (cls.__name__ +
928 str(inspect.signature(cls)).replace(' -> None', ''))
929
930 return cls
931
932
933# _cls should never be specified by keyword, so start it with an
Eric V. Smithf8e75492018-05-16 05:14:53 -0400934# underscore. The presence of _cls is used to detect if this
935# decorator is being called with parameters or not.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500936def dataclass(_cls=None, *, init=True, repr=True, eq=True, order=False,
Eric V. Smith5da8cfb2018-03-01 08:01:41 -0500937 unsafe_hash=False, frozen=False):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500938 """Returns the same class as was passed in, with dunder methods
939 added based on the fields defined in the class.
940
941 Examines PEP 526 __annotations__ to determine fields.
942
943 If init is true, an __init__() method is added to the class. If
944 repr is true, a __repr__() method is added. If order is true, rich
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500945 comparison dunder methods are added. If unsafe_hash is true, a
946 __hash__() method function is added. If frozen is true, fields may
947 not be assigned to after instance creation.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500948 """
949
950 def wrap(cls):
Eric V. Smithf199bc62018-03-18 20:40:34 -0400951 return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500952
953 # See if we're being called as @dataclass or @dataclass().
954 if _cls is None:
955 # We're called with parens.
956 return wrap
957
958 # We're called as @dataclass without parens.
959 return wrap(_cls)
960
961
962def fields(class_or_instance):
963 """Return a tuple describing the fields of this dataclass.
964
965 Accepts a dataclass or an instance of one. Tuple elements are of
966 type Field.
967 """
968
969 # Might it be worth caching this, per class?
970 try:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400971 fields = getattr(class_or_instance, _FIELDS)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500972 except AttributeError:
973 raise TypeError('must be called with a dataclass type or instance')
974
Eric V. Smithd1388922018-01-07 14:30:17 -0500975 # Exclude pseudo-fields. Note that fields is sorted by insertion
Eric V. Smithf8e75492018-05-16 05:14:53 -0400976 # order, so the order of the tuple is as the fields were defined.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500977 return tuple(f for f in fields.values() if f._field_type is _FIELD)
978
979
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500980def _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500981 """Returns True if obj is an instance of a dataclass."""
Eric V. Smithf199bc62018-03-18 20:40:34 -0400982 return not isinstance(obj, type) and hasattr(obj, _FIELDS)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500983
984
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500985def is_dataclass(obj):
986 """Returns True if obj is a dataclass or an instance of a
987 dataclass."""
Eric V. Smithf199bc62018-03-18 20:40:34 -0400988 return hasattr(obj, _FIELDS)
Eric V. Smithe7ba0132018-01-06 12:41:53 -0500989
990
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500991def asdict(obj, *, dict_factory=dict):
992 """Return the fields of a dataclass instance as a new dictionary mapping
993 field names to field values.
994
995 Example usage:
996
997 @dataclass
998 class C:
999 x: int
1000 y: int
1001
1002 c = C(1, 2)
1003 assert asdict(c) == {'x': 1, 'y': 2}
1004
1005 If given, 'dict_factory' will be used instead of built-in dict.
1006 The function applies recursively to field values that are
1007 dataclass instances. This will also look into built-in containers:
1008 tuples, lists, and dicts.
1009 """
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001010 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001011 raise TypeError("asdict() should be called on dataclass instances")
1012 return _asdict_inner(obj, dict_factory)
1013
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001014
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001015def _asdict_inner(obj, dict_factory):
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001016 if _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001017 result = []
1018 for f in fields(obj):
1019 value = _asdict_inner(getattr(obj, f.name), dict_factory)
1020 result.append((f.name, value))
1021 return dict_factory(result)
1022 elif isinstance(obj, (list, tuple)):
1023 return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
1024 elif isinstance(obj, dict):
1025 return type(obj)((_asdict_inner(k, dict_factory), _asdict_inner(v, dict_factory))
1026 for k, v in obj.items())
1027 else:
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001028 return copy.deepcopy(obj)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001029
1030
1031def astuple(obj, *, tuple_factory=tuple):
1032 """Return the fields of a dataclass instance as a new tuple of field values.
1033
1034 Example usage::
1035
1036 @dataclass
1037 class C:
1038 x: int
1039 y: int
1040
1041 c = C(1, 2)
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001042 assert astuple(c) == (1, 2)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001043
1044 If given, 'tuple_factory' will be used instead of built-in tuple.
1045 The function applies recursively to field values that are
1046 dataclass instances. This will also look into built-in containers:
1047 tuples, lists, and dicts.
1048 """
1049
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001050 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001051 raise TypeError("astuple() should be called on dataclass instances")
1052 return _astuple_inner(obj, tuple_factory)
1053
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001054
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001055def _astuple_inner(obj, tuple_factory):
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001056 if _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001057 result = []
1058 for f in fields(obj):
1059 value = _astuple_inner(getattr(obj, f.name), tuple_factory)
1060 result.append(value)
1061 return tuple_factory(result)
1062 elif isinstance(obj, (list, tuple)):
1063 return type(obj)(_astuple_inner(v, tuple_factory) for v in obj)
1064 elif isinstance(obj, dict):
1065 return type(obj)((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory))
1066 for k, v in obj.items())
1067 else:
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001068 return copy.deepcopy(obj)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001069
1070
Eric V. Smithd80b4432018-01-06 17:09:58 -05001071def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
Eric V. Smith5da8cfb2018-03-01 08:01:41 -05001072 repr=True, eq=True, order=False, unsafe_hash=False,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001073 frozen=False):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001074 """Return a new dynamically created dataclass.
1075
Eric V. Smithed7d4292018-01-06 16:14:03 -05001076 The dataclass name will be 'cls_name'. 'fields' is an iterable
1077 of either (name), (name, type) or (name, type, Field) objects. If type is
1078 omitted, use the string 'typing.Any'. Field objects are created by
Eric V. Smithd327ae62018-01-07 08:19:45 -05001079 the equivalent of calling 'field(name, type [, Field-info])'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001080
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001081 C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001082
1083 is equivalent to:
1084
1085 @dataclass
1086 class C(Base):
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001087 x: 'typing.Any'
1088 y: int
1089 z: int = field(init=False)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001090
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001091 For the bases and namespace parameters, see the builtin type() function.
Eric V. Smithd80b4432018-01-06 17:09:58 -05001092
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001093 The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to
Eric V. Smithd80b4432018-01-06 17:09:58 -05001094 dataclass().
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001095 """
1096
1097 if namespace is None:
1098 namespace = {}
1099 else:
1100 # Copy namespace since we're going to mutate it.
1101 namespace = namespace.copy()
1102
Eric V. Smithd1388922018-01-07 14:30:17 -05001103 anns = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001104 for item in fields:
Eric V. Smithed7d4292018-01-06 16:14:03 -05001105 if isinstance(item, str):
1106 name = item
1107 tp = 'typing.Any'
1108 elif len(item) == 2:
1109 name, tp, = item
1110 elif len(item) == 3:
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001111 name, tp, spec = item
1112 namespace[name] = spec
Eric V. Smithed7d4292018-01-06 16:14:03 -05001113 anns[name] = tp
1114
1115 namespace['__annotations__'] = anns
Ivan Levkivskyi5a7092d2018-03-31 13:41:17 +01001116 # We use `types.new_class()` instead of simply `type()` to allow dynamic creation
1117 # of generic dataclassses.
1118 cls = types.new_class(cls_name, bases, {}, lambda ns: ns.update(namespace))
Eric V. Smithd80b4432018-01-06 17:09:58 -05001119 return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001120 unsafe_hash=unsafe_hash, frozen=frozen)
1121
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001122
1123def replace(obj, **changes):
1124 """Return a new object replacing specified fields with new values.
1125
1126 This is especially useful for frozen classes. Example usage:
1127
1128 @dataclass(frozen=True)
1129 class C:
1130 x: int
1131 y: int
1132
1133 c = C(1, 2)
1134 c1 = replace(c, x=3)
1135 assert c1.x == 3 and c1.y == 2
1136 """
1137
Eric V. Smithf8e75492018-05-16 05:14:53 -04001138 # We're going to mutate 'changes', but that's okay because it's a
1139 # new dict, even if called with 'replace(obj, **my_changes)'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001140
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001141 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001142 raise TypeError("replace() should be called on dataclass instances")
1143
1144 # It's an error to have init=False fields in 'changes'.
1145 # If a field is not in 'changes', read its value from the provided obj.
1146
Eric V. Smithf199bc62018-03-18 20:40:34 -04001147 for f in getattr(obj, _FIELDS).values():
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001148 if not f.init:
1149 # Error if this field is specified in changes.
1150 if f.name in changes:
1151 raise ValueError(f'field {f.name} is declared with '
1152 'init=False, it cannot be specified with '
1153 'replace()')
1154 continue
1155
1156 if f.name not in changes:
1157 changes[f.name] = getattr(obj, f.name)
1158
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001159 # Create the new object, which calls __init__() and
Eric V. Smithf8e75492018-05-16 05:14:53 -04001160 # __post_init__() (if defined), using all of the init fields we've
1161 # added and/or left in 'changes'. If there are values supplied in
1162 # changes that aren't fields, this will correctly raise a
1163 # TypeError.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001164 return obj.__class__(**changes)