blob: 9135b07c9f259a914f7c1d574dc8e87855a3b214 [file] [log] [blame]
Eric V. Smith2a7bacb2018-05-15 22:44:27 -04001import re
Eric V. Smithf0db54a2017-12-04 16:58:55 -05002import sys
Eric V. Smithf96ddad2018-03-24 17:20:26 -04003import copy
Eric V. Smithf0db54a2017-12-04 16:58:55 -05004import types
Eric V. Smithf0db54a2017-12-04 16:58:55 -05005import inspect
Eric V. Smith4e812962018-05-16 11:31:29 -04006import keyword
Vadim Pushtaev4d12e4d2018-08-12 14:46:05 +03007import builtins
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +05308import functools
9import _thread
10
Eric V. Smithf0db54a2017-12-04 16:58:55 -050011
12__all__ = ['dataclass',
13 'field',
Eric V. Smith8e4560a2018-03-21 17:10:22 -040014 'Field',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050015 'FrozenInstanceError',
16 'InitVar',
Eric V. Smith03220fd2017-12-29 13:59:58 -050017 'MISSING',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050018
19 # Helper functions.
20 'fields',
21 'asdict',
22 'astuple',
23 'make_dataclass',
24 'replace',
Eric V. Smithe7ba0132018-01-06 12:41:53 -050025 'is_dataclass',
Eric V. Smithf0db54a2017-12-04 16:58:55 -050026 ]
27
Eric V. Smithea8fc522018-01-27 19:07:40 -050028# Conditions for adding methods. The boxes indicate what action the
Eric V. Smithf8e75492018-05-16 05:14:53 -040029# dataclass decorator takes. For all of these tables, when I talk
30# about init=, repr=, eq=, order=, unsafe_hash=, or frozen=, I'm
31# referring to the arguments to the @dataclass decorator. When
32# checking if a dunder method already exists, I mean check for an
33# entry in the class's __dict__. I never check to see if an attribute
34# is defined in a base class.
Eric V. Smithea8fc522018-01-27 19:07:40 -050035
36# Key:
37# +=========+=========================================+
38# + Value | Meaning |
39# +=========+=========================================+
40# | <blank> | No action: no method is added. |
41# +---------+-----------------------------------------+
42# | add | Generated method is added. |
43# +---------+-----------------------------------------+
Eric V. Smithea8fc522018-01-27 19:07:40 -050044# | raise | TypeError is raised. |
45# +---------+-----------------------------------------+
46# | None | Attribute is set to None. |
47# +=========+=========================================+
48
49# __init__
50#
51# +--- init= parameter
52# |
53# v | | |
54# | no | yes | <--- class has __init__ in __dict__?
55# +=======+=======+=======+
56# | False | | |
57# +-------+-------+-------+
58# | True | add | | <- the default
59# +=======+=======+=======+
60
61# __repr__
62#
63# +--- repr= parameter
64# |
65# v | | |
66# | no | yes | <--- class has __repr__ in __dict__?
67# +=======+=======+=======+
68# | False | | |
69# +-------+-------+-------+
70# | True | add | | <- the default
71# +=======+=======+=======+
72
73
74# __setattr__
75# __delattr__
76#
77# +--- frozen= parameter
78# |
79# v | | |
80# | no | yes | <--- class has __setattr__ or __delattr__ in __dict__?
81# +=======+=======+=======+
82# | False | | | <- the default
83# +-------+-------+-------+
84# | True | add | raise |
85# +=======+=======+=======+
86# Raise because not adding these methods would break the "frozen-ness"
Eric V. Smithf8e75492018-05-16 05:14:53 -040087# of the class.
Eric V. Smithea8fc522018-01-27 19:07:40 -050088
89# __eq__
90#
91# +--- eq= parameter
92# |
93# v | | |
94# | no | yes | <--- class has __eq__ in __dict__?
95# +=======+=======+=======+
96# | False | | |
97# +-------+-------+-------+
98# | True | add | | <- the default
99# +=======+=======+=======+
100
101# __lt__
102# __le__
103# __gt__
104# __ge__
105#
106# +--- order= parameter
107# |
108# v | | |
109# | no | yes | <--- class has any comparison method in __dict__?
110# +=======+=======+=======+
111# | False | | | <- the default
112# +-------+-------+-------+
113# | True | add | raise |
114# +=======+=======+=======+
115# Raise because to allow this case would interfere with using
Eric V. Smithf8e75492018-05-16 05:14:53 -0400116# functools.total_ordering.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500117
118# __hash__
119
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500120# +------------------- unsafe_hash= parameter
121# | +----------- eq= parameter
122# | | +--- frozen= parameter
123# | | |
124# v v v | | |
125# | no | yes | <--- class has explicitly defined __hash__
126# +=======+=======+=======+========+========+
127# | False | False | False | | | No __eq__, use the base class __hash__
128# +-------+-------+-------+--------+--------+
129# | False | False | True | | | No __eq__, use the base class __hash__
130# +-------+-------+-------+--------+--------+
131# | False | True | False | None | | <-- the default, not hashable
132# +-------+-------+-------+--------+--------+
133# | False | True | True | add | | Frozen, so hashable, allows override
134# +-------+-------+-------+--------+--------+
135# | True | False | False | add | raise | Has no __eq__, but hashable
136# +-------+-------+-------+--------+--------+
137# | True | False | True | add | raise | Has no __eq__, but hashable
138# +-------+-------+-------+--------+--------+
139# | True | True | False | add | raise | Not frozen, but hashable
140# +-------+-------+-------+--------+--------+
141# | True | True | True | add | raise | Frozen, so hashable
142# +=======+=======+=======+========+========+
Eric V. Smithea8fc522018-01-27 19:07:40 -0500143# For boxes that are blank, __hash__ is untouched and therefore
Eric V. Smithf8e75492018-05-16 05:14:53 -0400144# inherited from the base class. If the base is object, then
145# id-based hashing is used.
146#
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400147# Note that a class may already have __hash__=None if it specified an
Eric V. Smithf8e75492018-05-16 05:14:53 -0400148# __eq__ method in the class body (not one that was created by
149# @dataclass).
150#
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500151# See _hash_action (below) for a coded version of this table.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500152
153
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500154# Raised when an attempt is made to modify a frozen class.
155class FrozenInstanceError(AttributeError): pass
156
Eric V. Smithf8e75492018-05-16 05:14:53 -0400157# A sentinel object for default values to signal that a default
158# factory will be used. This is given a nice repr() which will appear
159# in the function signature of dataclasses' constructors.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500160class _HAS_DEFAULT_FACTORY_CLASS:
161 def __repr__(self):
162 return '<factory>'
163_HAS_DEFAULT_FACTORY = _HAS_DEFAULT_FACTORY_CLASS()
164
Eric V. Smith03220fd2017-12-29 13:59:58 -0500165# A sentinel object to detect if a parameter is supplied or not. Use
Eric V. Smithf8e75492018-05-16 05:14:53 -0400166# a class to give it a better repr.
Eric V. Smith03220fd2017-12-29 13:59:58 -0500167class _MISSING_TYPE:
168 pass
169MISSING = _MISSING_TYPE()
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500170
171# Since most per-field metadata will be unused, create an empty
Eric V. Smithf8e75492018-05-16 05:14:53 -0400172# read-only proxy that can be shared among all fields.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500173_EMPTY_METADATA = types.MappingProxyType({})
174
175# Markers for the various kinds of fields and pseudo-fields.
Eric V. Smith01abc6e2018-05-15 08:36:21 -0400176class _FIELD_BASE:
177 def __init__(self, name):
178 self.name = name
179 def __repr__(self):
180 return self.name
181_FIELD = _FIELD_BASE('_FIELD')
182_FIELD_CLASSVAR = _FIELD_BASE('_FIELD_CLASSVAR')
183_FIELD_INITVAR = _FIELD_BASE('_FIELD_INITVAR')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500184
185# The name of an attribute on the class where we store the Field
Eric V. Smithf8e75492018-05-16 05:14:53 -0400186# objects. Also used to check if a class is a Data Class.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400187_FIELDS = '__dataclass_fields__'
188
189# The name of an attribute on the class that stores the parameters to
190# @dataclass.
191_PARAMS = '__dataclass_params__'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500192
193# The name of the function, that if it exists, is called at the end of
194# __init__.
195_POST_INIT_NAME = '__post_init__'
196
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400197# String regex that string annotations for ClassVar or InitVar must match.
198# Allows "identifier.identifier[" or "identifier[".
199# https://bugs.python.org/issue33453 for details.
200_MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500201
Serhiy Storchakab4d0b392019-09-22 13:32:41 +0300202class InitVar:
Augusto Hack01ee12b2019-06-02 23:14:48 -0300203 __slots__ = ('type', )
204
205 def __init__(self, type):
206 self.type = type
207
208 def __repr__(self):
209 return f'dataclasses.InitVar[{self.type.__name__}]'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500210
Serhiy Storchakab4d0b392019-09-22 13:32:41 +0300211 def __class_getitem__(cls, type):
212 return InitVar(type)
213
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500214
215# Instances of Field are only ever created from within this module,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400216# and only from the field() function, although Field instances are
217# exposed externally as (conceptually) read-only objects.
218#
219# name and type are filled in after the fact, not in __init__.
220# They're not known at the time this class is instantiated, but it's
221# convenient if they're available later.
222#
Eric V. Smithf199bc62018-03-18 20:40:34 -0400223# When cls._FIELDS is filled in with a list of Field objects, the name
Eric V. Smithf8e75492018-05-16 05:14:53 -0400224# and type fields will have been populated.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500225class Field:
226 __slots__ = ('name',
227 'type',
228 'default',
229 'default_factory',
230 'repr',
231 'hash',
232 'init',
233 'compare',
234 'metadata',
235 '_field_type', # Private: not to be used by user code.
236 )
237
238 def __init__(self, default, default_factory, init, repr, hash, compare,
239 metadata):
240 self.name = None
241 self.type = None
242 self.default = default
243 self.default_factory = default_factory
244 self.init = init
245 self.repr = repr
246 self.hash = hash
247 self.compare = compare
248 self.metadata = (_EMPTY_METADATA
Christopher Huntb01786c2019-02-12 06:50:49 -0500249 if metadata is None else
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500250 types.MappingProxyType(metadata))
251 self._field_type = None
252
253 def __repr__(self):
254 return ('Field('
255 f'name={self.name!r},'
Eric V. Smith2473eea2018-05-14 11:37:28 -0400256 f'type={self.type!r},'
257 f'default={self.default!r},'
258 f'default_factory={self.default_factory!r},'
259 f'init={self.init!r},'
260 f'repr={self.repr!r},'
261 f'hash={self.hash!r},'
262 f'compare={self.compare!r},'
Eric V. Smith01abc6e2018-05-15 08:36:21 -0400263 f'metadata={self.metadata!r},'
264 f'_field_type={self._field_type}'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500265 ')')
266
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400267 # This is used to support the PEP 487 __set_name__ protocol in the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400268 # case where we're using a field that contains a descriptor as a
Artjome55ca3f2018-07-06 02:09:13 +0300269 # default value. For details on __set_name__, see
Eric V. Smithf8e75492018-05-16 05:14:53 -0400270 # https://www.python.org/dev/peps/pep-0487/#implementation-details.
271 #
272 # Note that in _process_class, this Field object is overwritten
273 # with the default value, so the end result is a descriptor that
274 # had __set_name__ called on it at the right time.
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400275 def __set_name__(self, owner, name):
Eric V. Smith52199522018-03-29 11:07:48 -0400276 func = getattr(type(self.default), '__set_name__', None)
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400277 if func:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400278 # There is a __set_name__ method on the descriptor, call
279 # it.
Eric V. Smith52199522018-03-29 11:07:48 -0400280 func(self.default, owner, name)
Eric V. Smithde7a2f02018-03-26 13:29:16 -0400281
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500282
Eric V. Smithf199bc62018-03-18 20:40:34 -0400283class _DataclassParams:
284 __slots__ = ('init',
285 'repr',
286 'eq',
287 'order',
288 'unsafe_hash',
289 'frozen',
290 )
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400291
Eric V. Smithf199bc62018-03-18 20:40:34 -0400292 def __init__(self, init, repr, eq, order, unsafe_hash, frozen):
293 self.init = init
294 self.repr = repr
295 self.eq = eq
296 self.order = order
297 self.unsafe_hash = unsafe_hash
298 self.frozen = frozen
299
300 def __repr__(self):
301 return ('_DataclassParams('
Eric V. Smith30590422018-05-14 17:16:52 -0400302 f'init={self.init!r},'
303 f'repr={self.repr!r},'
304 f'eq={self.eq!r},'
305 f'order={self.order!r},'
306 f'unsafe_hash={self.unsafe_hash!r},'
307 f'frozen={self.frozen!r}'
Eric V. Smithf199bc62018-03-18 20:40:34 -0400308 ')')
309
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400310
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500311# This function is used instead of exposing Field creation directly,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400312# so that a type checker can be told (via overloads) that this is a
313# function whose type depends on its parameters.
Eric V. Smith03220fd2017-12-29 13:59:58 -0500314def field(*, default=MISSING, default_factory=MISSING, init=True, repr=True,
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500315 hash=None, compare=True, metadata=None):
316 """Return an object to identify dataclass fields.
317
Eric V. Smithf8e75492018-05-16 05:14:53 -0400318 default is the default value of the field. default_factory is a
319 0-argument function called to initialize a field's value. If init
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500320 is True, the field will be a parameter to the class's __init__()
Eric V. Smithf8e75492018-05-16 05:14:53 -0400321 function. If repr is True, the field will be included in the
322 object's repr(). If hash is True, the field will be included in
323 the object's hash(). If compare is True, the field will be used
324 in comparison functions. metadata, if specified, must be a
325 mapping which is stored but not otherwise examined by dataclass.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500326
327 It is an error to specify both default and default_factory.
328 """
329
Eric V. Smith03220fd2017-12-29 13:59:58 -0500330 if default is not MISSING and default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500331 raise ValueError('cannot specify both default and default_factory')
332 return Field(default, default_factory, init, repr, hash, compare,
333 metadata)
334
335
336def _tuple_str(obj_name, fields):
337 # Return a string representing each field of obj_name as a tuple
Eric V. Smithf8e75492018-05-16 05:14:53 -0400338 # member. So, if fields is ['x', 'y'] and obj_name is "self",
339 # return "(self.x,self.y)".
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500340
341 # Special case for the 0-tuple.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500342 if not fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500343 return '()'
344 # Note the trailing comma, needed if this turns out to be a 1-tuple.
345 return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)'
346
347
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +0530348# This function's logic is copied from "recursive_repr" function in
349# reprlib module to avoid dependency.
350def _recursive_repr(user_function):
351 # Decorator to make a repr function return "..." for a recursive
352 # call.
353 repr_running = set()
354
355 @functools.wraps(user_function)
356 def wrapper(self):
357 key = id(self), _thread.get_ident()
358 if key in repr_running:
359 return '...'
360 repr_running.add(key)
361 try:
362 result = user_function(self)
363 finally:
364 repr_running.discard(key)
365 return result
366 return wrapper
367
368
Eric V. Smithea8fc522018-01-27 19:07:40 -0500369def _create_fn(name, args, body, *, globals=None, locals=None,
Eric V. Smith03220fd2017-12-29 13:59:58 -0500370 return_type=MISSING):
Eric V. Smithf8e75492018-05-16 05:14:53 -0400371 # Note that we mutate locals when exec() is called. Caller
372 # beware! The only callers are internal to this module, so no
373 # worries about external callers.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500374 if locals is None:
375 locals = {}
Vadim Pushtaev4d12e4d2018-08-12 14:46:05 +0300376 # __builtins__ may be the "builtins" module or
377 # the value of its "__dict__",
378 # so make sure "__builtins__" is the module.
379 if globals is not None and '__builtins__' not in globals:
380 globals['__builtins__'] = builtins
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500381 return_annotation = ''
Eric V. Smith03220fd2017-12-29 13:59:58 -0500382 if return_type is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500383 locals['_return_type'] = return_type
384 return_annotation = '->_return_type'
385 args = ','.join(args)
386 body = '\n'.join(f' {b}' for b in body)
387
Eric V. Smithf199bc62018-03-18 20:40:34 -0400388 # Compute the text of the entire function.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500389 txt = f'def {name}({args}){return_annotation}:\n{body}'
390
391 exec(txt, globals, locals)
392 return locals[name]
393
394
395def _field_assign(frozen, name, value, self_name):
396 # If we're a frozen class, then assign to our fields in __init__
Eric V. Smithf8e75492018-05-16 05:14:53 -0400397 # via object.__setattr__. Otherwise, just use a simple
398 # assignment.
399 #
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500400 # self_name is what "self" is called in this function: don't
Eric V. Smithf8e75492018-05-16 05:14:53 -0400401 # hard-code "self", since that might be a field name.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500402 if frozen:
Vadim Pushtaev4d12e4d2018-08-12 14:46:05 +0300403 return f'__builtins__.object.__setattr__({self_name},{name!r},{value})'
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500404 return f'{self_name}.{name}={value}'
405
406
407def _field_init(f, frozen, globals, self_name):
408 # Return the text of the line in the body of __init__ that will
Eric V. Smithf8e75492018-05-16 05:14:53 -0400409 # initialize this field.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500410
411 default_name = f'_dflt_{f.name}'
Eric V. Smith03220fd2017-12-29 13:59:58 -0500412 if f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500413 if f.init:
414 # This field has a default factory. If a parameter is
Eric V. Smithf8e75492018-05-16 05:14:53 -0400415 # given, use it. If not, call the factory.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500416 globals[default_name] = f.default_factory
417 value = (f'{default_name}() '
418 f'if {f.name} is _HAS_DEFAULT_FACTORY '
419 f'else {f.name}')
420 else:
421 # This is a field that's not in the __init__ params, but
Eric V. Smithf8e75492018-05-16 05:14:53 -0400422 # has a default factory function. It needs to be
423 # initialized here by calling the factory function,
424 # because there's no other way to initialize it.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500425
426 # For a field initialized with a default=defaultvalue, the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400427 # class dict just has the default value
428 # (cls.fieldname=defaultvalue). But that won't work for a
429 # default factory, the factory must be called in __init__
430 # and we must assign that to self.fieldname. We can't
431 # fall back to the class dict's value, both because it's
432 # not set, and because it might be different per-class
433 # (which, after all, is why we have a factory function!).
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500434
435 globals[default_name] = f.default_factory
436 value = f'{default_name}()'
437 else:
438 # No default factory.
439 if f.init:
Eric V. Smith03220fd2017-12-29 13:59:58 -0500440 if f.default is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500441 # There's no default, just do an assignment.
442 value = f.name
Eric V. Smith03220fd2017-12-29 13:59:58 -0500443 elif f.default is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500444 globals[default_name] = f.default
445 value = f.name
446 else:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400447 # This field does not need initialization. Signify that
448 # to the caller by returning None.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500449 return None
450
451 # Only test this now, so that we can create variables for the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400452 # default. However, return None to signify that we're not going
453 # to actually do the assignment statement for InitVars.
Eric V. Smithe7adf2b2018-06-07 14:43:59 -0400454 if f._field_type is _FIELD_INITVAR:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500455 return None
456
457 # Now, actually generate the field assignment.
458 return _field_assign(frozen, f.name, value, self_name)
459
460
461def _init_param(f):
Eric V. Smithf8e75492018-05-16 05:14:53 -0400462 # Return the __init__ parameter string for this field. For
463 # example, the equivalent of 'x:int=3' (except instead of 'int',
464 # reference a variable set to int, and instead of '3', reference a
465 # variable set to 3).
Eric V. Smith03220fd2017-12-29 13:59:58 -0500466 if f.default is MISSING and f.default_factory is MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400467 # There's no default, and no default_factory, just output the
468 # variable name and type.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500469 default = ''
Eric V. Smith03220fd2017-12-29 13:59:58 -0500470 elif f.default is not MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400471 # There's a default, this will be the name that's used to look
472 # it up.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500473 default = f'=_dflt_{f.name}'
Eric V. Smith03220fd2017-12-29 13:59:58 -0500474 elif f.default_factory is not MISSING:
Eric V. Smithf8e75492018-05-16 05:14:53 -0400475 # There's a factory function. Set a marker.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500476 default = '=_HAS_DEFAULT_FACTORY'
477 return f'{f.name}:_type_{f.name}{default}'
478
479
480def _init_fn(fields, frozen, has_post_init, self_name):
481 # fields contains both real fields and InitVar pseudo-fields.
482
483 # Make sure we don't have fields without defaults following fields
Eric V. Smithf8e75492018-05-16 05:14:53 -0400484 # with defaults. This actually would be caught when exec-ing the
485 # function source code, but catching it here gives a better error
486 # message, and future-proofs us in case we build up the function
487 # using ast.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500488 seen_default = False
489 for f in fields:
490 # Only consider fields in the __init__ call.
491 if f.init:
Eric V. Smith03220fd2017-12-29 13:59:58 -0500492 if not (f.default is MISSING and f.default_factory is MISSING):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500493 seen_default = True
494 elif seen_default:
495 raise TypeError(f'non-default argument {f.name!r} '
496 'follows default argument')
497
Eric V. Smith03220fd2017-12-29 13:59:58 -0500498 globals = {'MISSING': MISSING,
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500499 '_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY}
500
501 body_lines = []
502 for f in fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500503 line = _field_init(f, frozen, globals, self_name)
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400504 # line is None means that this field doesn't require
Eric V. Smithf8e75492018-05-16 05:14:53 -0400505 # initialization (it's a pseudo-field). Just skip it.
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400506 if line:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500507 body_lines.append(line)
508
509 # Does this class have a post-init function?
510 if has_post_init:
511 params_str = ','.join(f.name for f in fields
512 if f._field_type is _FIELD_INITVAR)
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400513 body_lines.append(f'{self_name}.{_POST_INIT_NAME}({params_str})')
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500514
515 # If no body lines, use 'pass'.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500516 if not body_lines:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500517 body_lines = ['pass']
518
519 locals = {f'_type_{f.name}': f.type for f in fields}
520 return _create_fn('__init__',
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400521 [self_name] + [_init_param(f) for f in fields if f.init],
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500522 body_lines,
523 locals=locals,
524 globals=globals,
525 return_type=None)
526
527
528def _repr_fn(fields):
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)dd13c882018-10-19 22:24:50 +0530529 fn = _create_fn('__repr__',
530 ('self',),
531 ['return self.__class__.__qualname__ + f"(' +
532 ', '.join([f"{f.name}={{self.{f.name}!r}}"
533 for f in fields]) +
534 ')"'])
535 return _recursive_repr(fn)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500536
537
Eric V. Smithf199bc62018-03-18 20:40:34 -0400538def _frozen_get_del_attr(cls, fields):
Eric V. Smithf8e75492018-05-16 05:14:53 -0400539 # XXX: globals is modified on the first call to _create_fn, then
540 # the modified version is used in the second call. Is this okay?
Eric V. Smithf199bc62018-03-18 20:40:34 -0400541 globals = {'cls': cls,
542 'FrozenInstanceError': FrozenInstanceError}
543 if fields:
544 fields_str = '(' + ','.join(repr(f.name) for f in fields) + ',)'
545 else:
546 # Special case for the zero-length tuple.
547 fields_str = '()'
548 return (_create_fn('__setattr__',
549 ('self', 'name', 'value'),
550 (f'if type(self) is cls or name in {fields_str}:',
551 ' raise FrozenInstanceError(f"cannot assign to field {name!r}")',
552 f'super(cls, self).__setattr__(name, value)'),
553 globals=globals),
554 _create_fn('__delattr__',
555 ('self', 'name'),
556 (f'if type(self) is cls or name in {fields_str}:',
557 ' raise FrozenInstanceError(f"cannot delete field {name!r}")',
558 f'super(cls, self).__delattr__(name)'),
559 globals=globals),
560 )
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500561
562
563def _cmp_fn(name, op, self_tuple, other_tuple):
564 # Create a comparison function. If the fields in the object are
Eric V. Smithf8e75492018-05-16 05:14:53 -0400565 # named 'x' and 'y', then self_tuple is the string
566 # '(self.x,self.y)' and other_tuple is the string
567 # '(other.x,other.y)'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500568
569 return _create_fn(name,
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400570 ('self', 'other'),
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500571 [ 'if other.__class__ is self.__class__:',
572 f' return {self_tuple}{op}{other_tuple}',
573 'return NotImplemented'])
574
575
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500576def _hash_fn(fields):
577 self_tuple = _tuple_str('self', fields)
578 return _create_fn('__hash__',
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400579 ('self',),
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500580 [f'return hash({self_tuple})'])
581
582
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400583def _is_classvar(a_type, typing):
Eric V. Smith92858352018-05-16 07:24:00 -0400584 # This test uses a typing internal class, but it's the best way to
585 # test if this is a ClassVar.
586 return (a_type is typing.ClassVar
587 or (type(a_type) is typing._GenericAlias
588 and a_type.__origin__ is typing.ClassVar))
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400589
590
591def _is_initvar(a_type, dataclasses):
592 # The module we're checking against is the module we're
593 # currently in (dataclasses.py).
Augusto Hack01ee12b2019-06-02 23:14:48 -0300594 return (a_type is dataclasses.InitVar
595 or type(a_type) is dataclasses.InitVar)
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400596
597
598def _is_type(annotation, cls, a_module, a_type, is_type_predicate):
599 # Given a type annotation string, does it refer to a_type in
600 # a_module? For example, when checking that annotation denotes a
601 # ClassVar, then a_module is typing, and a_type is
602 # typing.ClassVar.
603
604 # It's possible to look up a_module given a_type, but it involves
605 # looking in sys.modules (again!), and seems like a waste since
606 # the caller already knows a_module.
607
608 # - annotation is a string type annotation
609 # - cls is the class that this annotation was found in
610 # - a_module is the module we want to match
611 # - a_type is the type in that module we want to match
612 # - is_type_predicate is a function called with (obj, a_module)
613 # that determines if obj is of the desired type.
614
615 # Since this test does not do a local namespace lookup (and
616 # instead only a module (global) lookup), there are some things it
617 # gets wrong.
618
Eric V. Smithf8e75492018-05-16 05:14:53 -0400619 # With string annotations, cv0 will be detected as a ClassVar:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400620 # CV = ClassVar
621 # @dataclass
622 # class C0:
623 # cv0: CV
624
Eric V. Smithf8e75492018-05-16 05:14:53 -0400625 # But in this example cv1 will not be detected as a ClassVar:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400626 # @dataclass
627 # class C1:
628 # CV = ClassVar
629 # cv1: CV
630
Eric V. Smithf8e75492018-05-16 05:14:53 -0400631 # In C1, the code in this function (_is_type) will look up "CV" in
632 # the module and not find it, so it will not consider cv1 as a
633 # ClassVar. This is a fairly obscure corner case, and the best
634 # way to fix it would be to eval() the string "CV" with the
635 # correct global and local namespaces. However that would involve
636 # a eval() penalty for every single field of every dataclass
637 # that's defined. It was judged not worth it.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400638
639 match = _MODULE_IDENTIFIER_RE.match(annotation)
640 if match:
641 ns = None
642 module_name = match.group(1)
643 if not module_name:
644 # No module name, assume the class's module did
645 # "from dataclasses import InitVar".
646 ns = sys.modules.get(cls.__module__).__dict__
647 else:
648 # Look up module_name in the class's module.
649 module = sys.modules.get(cls.__module__)
650 if module and module.__dict__.get(module_name) is a_module:
651 ns = sys.modules.get(a_type.__module__).__dict__
652 if ns and is_type_predicate(ns.get(match.group(2)), a_module):
653 return True
654 return False
655
656
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500657def _get_field(cls, a_name, a_type):
Eric V. Smithf96ddad2018-03-24 17:20:26 -0400658 # Return a Field object for this field name and type. ClassVars
Eric V. Smithf8e75492018-05-16 05:14:53 -0400659 # and InitVars are also returned, but marked as such (see
660 # f._field_type).
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500661
Eric V. Smithf8e75492018-05-16 05:14:53 -0400662 # If the default value isn't derived from Field, then it's only a
663 # normal default value. Convert it to a Field().
Eric V. Smith03220fd2017-12-29 13:59:58 -0500664 default = getattr(cls, a_name, MISSING)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500665 if isinstance(default, Field):
666 f = default
667 else:
Eric V. Smith7389fd92018-03-19 21:07:51 -0400668 if isinstance(default, types.MemberDescriptorType):
669 # This is a field in __slots__, so it has no default value.
670 default = MISSING
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500671 f = field(default=default)
672
Eric V. Smithf8e75492018-05-16 05:14:53 -0400673 # Only at this point do we know the name and the type. Set them.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500674 f.name = a_name
675 f.type = a_type
676
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400677 # Assume it's a normal field until proven otherwise. We're next
Eric V. Smithf8e75492018-05-16 05:14:53 -0400678 # going to decide if it's a ClassVar or InitVar, everything else
679 # is just a normal field.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400680 f._field_type = _FIELD
681
682 # In addition to checking for actual types here, also check for
Eric V. Smithf8e75492018-05-16 05:14:53 -0400683 # string annotations. get_type_hints() won't always work for us
684 # (see https://github.com/python/typing/issues/508 for example),
685 # plus it's expensive and would require an eval for every stirng
686 # annotation. So, make a best effort to see if this is a ClassVar
687 # or InitVar using regex's and checking that the thing referenced
688 # is actually of the correct type.
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400689
690 # For the complete discussion, see https://bugs.python.org/issue33453
691
692 # If typing has not been imported, then it's impossible for any
Eric V. Smithf8e75492018-05-16 05:14:53 -0400693 # annotation to be a ClassVar. So, only look for ClassVar if
694 # typing has been imported by any module (not necessarily cls's
695 # module).
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500696 typing = sys.modules.get('typing')
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400697 if typing:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400698 if (_is_classvar(a_type, typing)
699 or (isinstance(f.type, str)
700 and _is_type(f.type, cls, typing, typing.ClassVar,
701 _is_classvar))):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500702 f._field_type = _FIELD_CLASSVAR
703
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400704 # If the type is InitVar, or if it's a matching string annotation,
705 # then it's an InitVar.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500706 if f._field_type is _FIELD:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400707 # The module we're checking against is the module we're
708 # currently in (dataclasses.py).
709 dataclasses = sys.modules[__name__]
710 if (_is_initvar(a_type, dataclasses)
711 or (isinstance(f.type, str)
712 and _is_type(f.type, cls, dataclasses, dataclasses.InitVar,
713 _is_initvar))):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500714 f._field_type = _FIELD_INITVAR
715
Eric V. Smith2a7bacb2018-05-15 22:44:27 -0400716 # Validations for individual fields. This is delayed until now,
717 # instead of in the Field() constructor, since only here do we
718 # know the field name, which allows for better error reporting.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500719
720 # Special restrictions for ClassVar and InitVar.
721 if f._field_type in (_FIELD_CLASSVAR, _FIELD_INITVAR):
Eric V. Smith03220fd2017-12-29 13:59:58 -0500722 if f.default_factory is not MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500723 raise TypeError(f'field {f.name} cannot have a '
724 'default factory')
725 # Should I check for other field settings? default_factory
Eric V. Smithf8e75492018-05-16 05:14:53 -0400726 # seems the most serious to check for. Maybe add others. For
727 # example, how about init=False (or really,
728 # init=<not-the-default-init-value>)? It makes no sense for
729 # ClassVar and InitVar to specify init=<anything>.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500730
731 # For real fields, disallow mutable defaults for known types.
732 if f._field_type is _FIELD and isinstance(f.default, (list, dict, set)):
733 raise ValueError(f'mutable default {type(f.default)} for field '
734 f'{f.name} is not allowed: use default_factory')
735
736 return f
737
738
Eric V. Smithea8fc522018-01-27 19:07:40 -0500739def _set_new_attribute(cls, name, value):
740 # Never overwrites an existing attribute. Returns True if the
Eric V. Smithf8e75492018-05-16 05:14:53 -0400741 # attribute already exists.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500742 if name in cls.__dict__:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500743 return True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500744 setattr(cls, name, value)
Eric V. Smithea8fc522018-01-27 19:07:40 -0500745 return False
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500746
747
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500748# Decide if/how we're going to create a hash function. Key is
Eric V. Smithf8e75492018-05-16 05:14:53 -0400749# (unsafe_hash, eq, frozen, does-hash-exist). Value is the action to
750# take. The common case is to do nothing, so instead of providing a
751# function that is a no-op, use None to signify that.
Eric V. Smith01d618c2018-03-24 22:10:14 -0400752
753def _hash_set_none(cls, fields):
754 return None
755
756def _hash_add(cls, fields):
757 flds = [f for f in fields if (f.compare if f.hash is None else f.hash)]
758 return _hash_fn(flds)
759
760def _hash_exception(cls, fields):
761 # Raise an exception.
762 raise TypeError(f'Cannot overwrite attribute __hash__ '
763 f'in class {cls.__name__}')
764
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500765#
766# +-------------------------------------- unsafe_hash?
767# | +------------------------------- eq?
768# | | +------------------------ frozen?
769# | | | +---------------- has-explicit-hash?
770# | | | |
771# | | | | +------- action
772# | | | | |
773# v v v v v
Eric V. Smith01d618c2018-03-24 22:10:14 -0400774_hash_action = {(False, False, False, False): None,
775 (False, False, False, True ): None,
776 (False, False, True, False): None,
777 (False, False, True, True ): None,
778 (False, True, False, False): _hash_set_none,
779 (False, True, False, True ): None,
780 (False, True, True, False): _hash_add,
781 (False, True, True, True ): None,
782 (True, False, False, False): _hash_add,
783 (True, False, False, True ): _hash_exception,
784 (True, False, True, False): _hash_add,
785 (True, False, True, True ): _hash_exception,
786 (True, True, False, False): _hash_add,
787 (True, True, False, True ): _hash_exception,
788 (True, True, True, False): _hash_add,
789 (True, True, True, True ): _hash_exception,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500790 }
791# See https://bugs.python.org/issue32929#msg312829 for an if-statement
Eric V. Smithf8e75492018-05-16 05:14:53 -0400792# version of this table.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500793
794
Eric V. Smithf199bc62018-03-18 20:40:34 -0400795def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
Eric V. Smithd1388922018-01-07 14:30:17 -0500796 # Now that dicts retain insertion order, there's no reason to use
Eric V. Smithf8e75492018-05-16 05:14:53 -0400797 # an ordered dict. I am leveraging that ordering here, because
798 # derived class fields overwrite base class fields, but the order
799 # is defined by the base class, which is found first.
Eric V. Smithd1388922018-01-07 14:30:17 -0500800 fields = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500801
Eric V. Smithf199bc62018-03-18 20:40:34 -0400802 setattr(cls, _PARAMS, _DataclassParams(init, repr, eq, order,
803 unsafe_hash, frozen))
804
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500805 # Find our base classes in reverse MRO order, and exclude
Eric V. Smithf8e75492018-05-16 05:14:53 -0400806 # ourselves. In reversed order so that more derived classes
807 # override earlier field definitions in base classes. As long as
808 # we're iterating over them, see if any are frozen.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400809 any_frozen_base = False
810 has_dataclass_bases = False
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500811 for b in cls.__mro__[-1:0:-1]:
812 # Only process classes that have been processed by our
Eric V. Smithf8e75492018-05-16 05:14:53 -0400813 # decorator. That is, they have a _FIELDS attribute.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400814 base_fields = getattr(b, _FIELDS, None)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500815 if base_fields:
Eric V. Smithf199bc62018-03-18 20:40:34 -0400816 has_dataclass_bases = True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500817 for f in base_fields.values():
818 fields[f.name] = f
Eric V. Smithf199bc62018-03-18 20:40:34 -0400819 if getattr(b, _PARAMS).frozen:
820 any_frozen_base = True
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500821
Eric V. Smith56970b82018-03-22 16:28:48 -0400822 # Annotations that are defined in this class (not in base
Eric V. Smithf8e75492018-05-16 05:14:53 -0400823 # classes). If __annotations__ isn't present, then this class
824 # adds no new annotations. We use this to compute fields that are
825 # added by this class.
826 #
Eric V. Smith56970b82018-03-22 16:28:48 -0400827 # Fields are found from cls_annotations, which is guaranteed to be
Eric V. Smithf8e75492018-05-16 05:14:53 -0400828 # ordered. Default values are from class attributes, if a field
829 # has a default. If the default value is a Field(), then it
830 # contains additional info beyond (and possibly including) the
831 # actual default value. Pseudo-fields ClassVars and InitVars are
832 # included, despite the fact that they're not real fields. That's
833 # dealt with later.
Eric V. Smith56970b82018-03-22 16:28:48 -0400834 cls_annotations = cls.__dict__.get('__annotations__', {})
835
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500836 # Now find fields in our class. While doing so, validate some
Eric V. Smithf8e75492018-05-16 05:14:53 -0400837 # things, and set the default values (as class attributes) where
838 # we can.
Eric V. Smith56970b82018-03-22 16:28:48 -0400839 cls_fields = [_get_field(cls, name, type)
840 for name, type in cls_annotations.items()]
841 for f in cls_fields:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500842 fields[f.name] = f
843
Eric V. Smithf8e75492018-05-16 05:14:53 -0400844 # If the class attribute (which is the default value for this
845 # field) exists and is of type 'Field', replace it with the
846 # real default. This is so that normal class introspection
847 # sees a real default value, not a Field.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500848 if isinstance(getattr(cls, f.name, None), Field):
Eric V. Smith03220fd2017-12-29 13:59:58 -0500849 if f.default is MISSING:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500850 # If there's no default, delete the class attribute.
Eric V. Smithf8e75492018-05-16 05:14:53 -0400851 # This happens if we specify field(repr=False), for
852 # example (that is, we specified a field object, but
853 # no default value). Also if we're using a default
854 # factory. The class attribute should not be set at
855 # all in the post-processed class.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500856 delattr(cls, f.name)
857 else:
858 setattr(cls, f.name, f.default)
859
Eric V. Smith56970b82018-03-22 16:28:48 -0400860 # Do we have any Field members that don't also have annotations?
861 for name, value in cls.__dict__.items():
862 if isinstance(value, Field) and not name in cls_annotations:
863 raise TypeError(f'{name!r} is a field but has no type annotation')
864
Eric V. Smithf199bc62018-03-18 20:40:34 -0400865 # Check rules that apply if we are derived from any dataclasses.
866 if has_dataclass_bases:
867 # Raise an exception if any of our bases are frozen, but we're not.
868 if any_frozen_base and not frozen:
869 raise TypeError('cannot inherit non-frozen dataclass from a '
870 'frozen one')
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500871
Eric V. Smithf199bc62018-03-18 20:40:34 -0400872 # Raise an exception if we're frozen, but none of our bases are.
873 if not any_frozen_base and frozen:
874 raise TypeError('cannot inherit frozen dataclass from a '
875 'non-frozen one')
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500876
Eric V. Smithf8e75492018-05-16 05:14:53 -0400877 # Remember all of the fields on our class (including bases). This
878 # also marks this class as being a dataclass.
Eric V. Smithf199bc62018-03-18 20:40:34 -0400879 setattr(cls, _FIELDS, fields)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500880
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500881 # Was this class defined with an explicit __hash__? Note that if
Eric V. Smithf8e75492018-05-16 05:14:53 -0400882 # __eq__ is defined in this class, then python will automatically
883 # set __hash__ to None. This is a heuristic, as it's possible
884 # that such a __hash__ == None was not auto-generated, but it
885 # close enough.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500886 class_hash = cls.__dict__.get('__hash__', MISSING)
887 has_explicit_hash = not (class_hash is MISSING or
888 (class_hash is None and '__eq__' in cls.__dict__))
Eric V. Smithea8fc522018-01-27 19:07:40 -0500889
Eric V. Smithf8e75492018-05-16 05:14:53 -0400890 # If we're generating ordering methods, we must be generating the
891 # eq methods.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500892 if order and not eq:
893 raise ValueError('eq must be true if order is true')
894
895 if init:
896 # Does this class have a post-init function?
897 has_post_init = hasattr(cls, _POST_INIT_NAME)
898
899 # Include InitVars and regular fields (so, not ClassVars).
Eric V. Smithea8fc522018-01-27 19:07:40 -0500900 flds = [f for f in fields.values()
901 if f._field_type in (_FIELD, _FIELD_INITVAR)]
902 _set_new_attribute(cls, '__init__',
903 _init_fn(flds,
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500904 frozen,
Eric V. Smithea8fc522018-01-27 19:07:40 -0500905 has_post_init,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400906 # The name to use for the "self"
907 # param in __init__. Use "self"
908 # if possible.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500909 '__dataclass_self__' if 'self' in fields
910 else 'self',
911 ))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500912
913 # Get the fields as a list, and include only real fields. This is
Eric V. Smithf8e75492018-05-16 05:14:53 -0400914 # used in all of the following methods.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500915 field_list = [f for f in fields.values() if f._field_type is _FIELD]
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500916
917 if repr:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500918 flds = [f for f in field_list if f.repr]
919 _set_new_attribute(cls, '__repr__', _repr_fn(flds))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500920
921 if eq:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500922 # Create _eq__ method. There's no need for a __ne__ method,
Eric V. Smithf8e75492018-05-16 05:14:53 -0400923 # since python will call __eq__ and negate it.
Eric V. Smithea8fc522018-01-27 19:07:40 -0500924 flds = [f for f in field_list if f.compare]
925 self_tuple = _tuple_str('self', flds)
926 other_tuple = _tuple_str('other', flds)
927 _set_new_attribute(cls, '__eq__',
928 _cmp_fn('__eq__', '==',
929 self_tuple, other_tuple))
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500930
931 if order:
Eric V. Smithea8fc522018-01-27 19:07:40 -0500932 # Create and set the ordering methods.
933 flds = [f for f in field_list if f.compare]
934 self_tuple = _tuple_str('self', flds)
935 other_tuple = _tuple_str('other', flds)
936 for name, op in [('__lt__', '<'),
937 ('__le__', '<='),
938 ('__gt__', '>'),
939 ('__ge__', '>='),
940 ]:
941 if _set_new_attribute(cls, name,
942 _cmp_fn(name, op, self_tuple, other_tuple)):
943 raise TypeError(f'Cannot overwrite attribute {name} '
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500944 f'in class {cls.__name__}. Consider using '
Eric V. Smithea8fc522018-01-27 19:07:40 -0500945 'functools.total_ordering')
946
Eric V. Smith2fa6b9e2018-02-26 20:38:33 -0500947 if frozen:
Eric V. Smithf199bc62018-03-18 20:40:34 -0400948 for fn in _frozen_get_del_attr(cls, field_list):
949 if _set_new_attribute(cls, fn.__name__, fn):
950 raise TypeError(f'Cannot overwrite attribute {fn.__name__} '
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500951 f'in class {cls.__name__}')
Eric V. Smithea8fc522018-01-27 19:07:40 -0500952
953 # Decide if/how we're going to create a hash function.
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500954 hash_action = _hash_action[bool(unsafe_hash),
955 bool(eq),
956 bool(frozen),
957 has_explicit_hash]
Eric V. Smith01d618c2018-03-24 22:10:14 -0400958 if hash_action:
959 # No need to call _set_new_attribute here, since by the time
Eric V. Smithf8e75492018-05-16 05:14:53 -0400960 # we're here the overwriting is unconditional.
Eric V. Smith01d618c2018-03-24 22:10:14 -0400961 cls.__hash__ = hash_action(cls, field_list)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500962
963 if not getattr(cls, '__doc__'):
964 # Create a class doc-string.
965 cls.__doc__ = (cls.__name__ +
966 str(inspect.signature(cls)).replace(' -> None', ''))
967
968 return cls
969
970
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300971def dataclass(cls=None, /, *, init=True, repr=True, eq=True, order=False,
Eric V. Smith5da8cfb2018-03-01 08:01:41 -0500972 unsafe_hash=False, frozen=False):
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500973 """Returns the same class as was passed in, with dunder methods
974 added based on the fields defined in the class.
975
976 Examines PEP 526 __annotations__ to determine fields.
977
978 If init is true, an __init__() method is added to the class. If
979 repr is true, a __repr__() method is added. If order is true, rich
Eric V. Smithdbf9cff2018-02-25 21:30:17 -0500980 comparison dunder methods are added. If unsafe_hash is true, a
981 __hash__() method function is added. If frozen is true, fields may
982 not be assigned to after instance creation.
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500983 """
984
985 def wrap(cls):
Eric V. Smithf199bc62018-03-18 20:40:34 -0400986 return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500987
988 # See if we're being called as @dataclass or @dataclass().
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300989 if cls is None:
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500990 # We're called with parens.
991 return wrap
992
993 # We're called as @dataclass without parens.
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300994 return wrap(cls)
Eric V. Smithf0db54a2017-12-04 16:58:55 -0500995
996
997def fields(class_or_instance):
998 """Return a tuple describing the fields of this dataclass.
999
1000 Accepts a dataclass or an instance of one. Tuple elements are of
1001 type Field.
1002 """
1003
1004 # Might it be worth caching this, per class?
1005 try:
Eric V. Smith2a7bacb2018-05-15 22:44:27 -04001006 fields = getattr(class_or_instance, _FIELDS)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001007 except AttributeError:
1008 raise TypeError('must be called with a dataclass type or instance')
1009
Eric V. Smithd1388922018-01-07 14:30:17 -05001010 # Exclude pseudo-fields. Note that fields is sorted by insertion
Eric V. Smithf8e75492018-05-16 05:14:53 -04001011 # order, so the order of the tuple is as the fields were defined.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001012 return tuple(f for f in fields.values() if f._field_type is _FIELD)
1013
1014
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001015def _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001016 """Returns True if obj is an instance of a dataclass."""
Eric V. Smithb0f4dab2019-08-20 01:40:28 -04001017 return hasattr(type(obj), _FIELDS)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001018
1019
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001020def is_dataclass(obj):
1021 """Returns True if obj is a dataclass or an instance of a
1022 dataclass."""
Eric V. Smithb0f4dab2019-08-20 01:40:28 -04001023 cls = obj if isinstance(obj, type) else type(obj)
1024 return hasattr(cls, _FIELDS)
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001025
1026
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001027def asdict(obj, *, dict_factory=dict):
1028 """Return the fields of a dataclass instance as a new dictionary mapping
1029 field names to field values.
1030
1031 Example usage:
1032
1033 @dataclass
1034 class C:
1035 x: int
1036 y: int
1037
1038 c = C(1, 2)
1039 assert asdict(c) == {'x': 1, 'y': 2}
1040
1041 If given, 'dict_factory' will be used instead of built-in dict.
1042 The function applies recursively to field values that are
1043 dataclass instances. This will also look into built-in containers:
1044 tuples, lists, and dicts.
1045 """
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001046 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001047 raise TypeError("asdict() should be called on dataclass instances")
1048 return _asdict_inner(obj, dict_factory)
1049
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001050
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001051def _asdict_inner(obj, dict_factory):
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001052 if _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001053 result = []
1054 for f in fields(obj):
1055 value = _asdict_inner(getattr(obj, f.name), dict_factory)
1056 result.append((f.name, value))
1057 return dict_factory(result)
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001058 elif isinstance(obj, tuple) and hasattr(obj, '_fields'):
1059 # obj is a namedtuple. Recurse into it, but the returned
1060 # object is another namedtuple of the same type. This is
1061 # similar to how other list- or tuple-derived classes are
1062 # treated (see below), but we just need to create them
1063 # differently because a namedtuple's __init__ needs to be
1064 # called differently (see bpo-34363).
1065
1066 # I'm not using namedtuple's _asdict()
1067 # method, because:
1068 # - it does not recurse in to the namedtuple fields and
1069 # convert them to dicts (using dict_factory).
1070 # - I don't actually want to return a dict here. The the main
1071 # use case here is json.dumps, and it handles converting
1072 # namedtuples to lists. Admittedly we're losing some
1073 # information here when we produce a json list instead of a
1074 # dict. Note that if we returned dicts here instead of
1075 # namedtuples, we could no longer call asdict() on a data
1076 # structure where a namedtuple was used as a dict key.
1077
1078 return type(obj)(*[_asdict_inner(v, dict_factory) for v in obj])
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001079 elif isinstance(obj, (list, tuple)):
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001080 # Assume we can create an object of this type by passing in a
1081 # generator (which is not true for namedtuples, handled
1082 # above).
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001083 return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
1084 elif isinstance(obj, dict):
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001085 return type(obj)((_asdict_inner(k, dict_factory),
1086 _asdict_inner(v, dict_factory))
1087 for k, v in obj.items())
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001088 else:
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001089 return copy.deepcopy(obj)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001090
1091
1092def astuple(obj, *, tuple_factory=tuple):
1093 """Return the fields of a dataclass instance as a new tuple of field values.
1094
1095 Example usage::
1096
1097 @dataclass
1098 class C:
1099 x: int
1100 y: int
1101
1102 c = C(1, 2)
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001103 assert astuple(c) == (1, 2)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001104
1105 If given, 'tuple_factory' will be used instead of built-in tuple.
1106 The function applies recursively to field values that are
1107 dataclass instances. This will also look into built-in containers:
1108 tuples, lists, and dicts.
1109 """
1110
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001111 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001112 raise TypeError("astuple() should be called on dataclass instances")
1113 return _astuple_inner(obj, tuple_factory)
1114
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001115
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001116def _astuple_inner(obj, tuple_factory):
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001117 if _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001118 result = []
1119 for f in fields(obj):
1120 value = _astuple_inner(getattr(obj, f.name), tuple_factory)
1121 result.append(value)
1122 return tuple_factory(result)
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001123 elif isinstance(obj, tuple) and hasattr(obj, '_fields'):
1124 # obj is a namedtuple. Recurse into it, but the returned
1125 # object is another namedtuple of the same type. This is
1126 # similar to how other list- or tuple-derived classes are
1127 # treated (see below), but we just need to create them
1128 # differently because a namedtuple's __init__ needs to be
1129 # called differently (see bpo-34363).
1130 return type(obj)(*[_astuple_inner(v, tuple_factory) for v in obj])
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001131 elif isinstance(obj, (list, tuple)):
Eric V. Smith9b9d97d2018-09-14 11:32:16 -04001132 # Assume we can create an object of this type by passing in a
1133 # generator (which is not true for namedtuples, handled
1134 # above).
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001135 return type(obj)(_astuple_inner(v, tuple_factory) for v in obj)
1136 elif isinstance(obj, dict):
1137 return type(obj)((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory))
1138 for k, v in obj.items())
1139 else:
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001140 return copy.deepcopy(obj)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001141
1142
Eric V. Smithd80b4432018-01-06 17:09:58 -05001143def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
Eric V. Smith5da8cfb2018-03-01 08:01:41 -05001144 repr=True, eq=True, order=False, unsafe_hash=False,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001145 frozen=False):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001146 """Return a new dynamically created dataclass.
1147
Eric V. Smithed7d4292018-01-06 16:14:03 -05001148 The dataclass name will be 'cls_name'. 'fields' is an iterable
1149 of either (name), (name, type) or (name, type, Field) objects. If type is
1150 omitted, use the string 'typing.Any'. Field objects are created by
Eric V. Smithd327ae62018-01-07 08:19:45 -05001151 the equivalent of calling 'field(name, type [, Field-info])'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001152
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001153 C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001154
1155 is equivalent to:
1156
1157 @dataclass
1158 class C(Base):
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001159 x: 'typing.Any'
1160 y: int
1161 z: int = field(init=False)
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001162
Raymond Hettingerd55209d2018-01-10 20:56:41 -08001163 For the bases and namespace parameters, see the builtin type() function.
Eric V. Smithd80b4432018-01-06 17:09:58 -05001164
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001165 The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to
Eric V. Smithd80b4432018-01-06 17:09:58 -05001166 dataclass().
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001167 """
1168
1169 if namespace is None:
1170 namespace = {}
1171 else:
1172 # Copy namespace since we're going to mutate it.
1173 namespace = namespace.copy()
1174
Eric V. Smith4e812962018-05-16 11:31:29 -04001175 # While we're looking through the field names, validate that they
1176 # are identifiers, are not keywords, and not duplicates.
1177 seen = set()
Eric V. Smithd1388922018-01-07 14:30:17 -05001178 anns = {}
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001179 for item in fields:
Eric V. Smithed7d4292018-01-06 16:14:03 -05001180 if isinstance(item, str):
1181 name = item
1182 tp = 'typing.Any'
1183 elif len(item) == 2:
1184 name, tp, = item
1185 elif len(item) == 3:
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001186 name, tp, spec = item
1187 namespace[name] = spec
Eric V. Smith4e812962018-05-16 11:31:29 -04001188 else:
1189 raise TypeError(f'Invalid field: {item!r}')
1190
1191 if not isinstance(name, str) or not name.isidentifier():
Min ho Kim96e12d52019-07-22 06:12:33 +10001192 raise TypeError(f'Field names must be valid identifiers: {name!r}')
Eric V. Smith4e812962018-05-16 11:31:29 -04001193 if keyword.iskeyword(name):
1194 raise TypeError(f'Field names must not be keywords: {name!r}')
1195 if name in seen:
1196 raise TypeError(f'Field name duplicated: {name!r}')
1197
1198 seen.add(name)
Eric V. Smithed7d4292018-01-06 16:14:03 -05001199 anns[name] = tp
1200
1201 namespace['__annotations__'] = anns
Ivan Levkivskyi5a7092d2018-03-31 13:41:17 +01001202 # We use `types.new_class()` instead of simply `type()` to allow dynamic creation
1203 # of generic dataclassses.
1204 cls = types.new_class(cls_name, bases, {}, lambda ns: ns.update(namespace))
Eric V. Smithd80b4432018-01-06 17:09:58 -05001205 return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
Eric V. Smithdbf9cff2018-02-25 21:30:17 -05001206 unsafe_hash=unsafe_hash, frozen=frozen)
1207
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001208
Serhiy Storchaka2d88e632019-06-26 19:07:44 +03001209def replace(obj, /, **changes):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001210 """Return a new object replacing specified fields with new values.
1211
1212 This is especially useful for frozen classes. Example usage:
1213
1214 @dataclass(frozen=True)
1215 class C:
1216 x: int
1217 y: int
1218
1219 c = C(1, 2)
1220 c1 = replace(c, x=3)
1221 assert c1.x == 3 and c1.y == 2
1222 """
1223
Eric V. Smithf8e75492018-05-16 05:14:53 -04001224 # We're going to mutate 'changes', but that's okay because it's a
1225 # new dict, even if called with 'replace(obj, **my_changes)'.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001226
Eric V. Smithe7ba0132018-01-06 12:41:53 -05001227 if not _is_dataclass_instance(obj):
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001228 raise TypeError("replace() should be called on dataclass instances")
1229
1230 # It's an error to have init=False fields in 'changes'.
1231 # If a field is not in 'changes', read its value from the provided obj.
1232
Eric V. Smithf199bc62018-03-18 20:40:34 -04001233 for f in getattr(obj, _FIELDS).values():
Eric V. Smithe7adf2b2018-06-07 14:43:59 -04001234 # Only consider normal fields or InitVars.
1235 if f._field_type is _FIELD_CLASSVAR:
1236 continue
1237
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001238 if not f.init:
1239 # Error if this field is specified in changes.
1240 if f.name in changes:
1241 raise ValueError(f'field {f.name} is declared with '
1242 'init=False, it cannot be specified with '
1243 'replace()')
1244 continue
1245
1246 if f.name not in changes:
Dong-hee Na3d70f7a2018-06-23 23:46:32 +09001247 if f._field_type is _FIELD_INITVAR:
1248 raise ValueError(f"InitVar {f.name!r} "
1249 'must be specified with replace()')
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001250 changes[f.name] = getattr(obj, f.name)
1251
Eric V. Smithf96ddad2018-03-24 17:20:26 -04001252 # Create the new object, which calls __init__() and
Eric V. Smithf8e75492018-05-16 05:14:53 -04001253 # __post_init__() (if defined), using all of the init fields we've
1254 # added and/or left in 'changes'. If there are values supplied in
1255 # changes that aren't fields, this will correctly raise a
1256 # TypeError.
Eric V. Smithf0db54a2017-12-04 16:58:55 -05001257 return obj.__class__(**changes)