Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 1 | import sys |
| 2 | import types |
| 3 | from copy import deepcopy |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 4 | import inspect |
| 5 | |
| 6 | __all__ = ['dataclass', |
| 7 | 'field', |
| 8 | 'FrozenInstanceError', |
| 9 | 'InitVar', |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 10 | 'MISSING', |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 11 | |
| 12 | # Helper functions. |
| 13 | 'fields', |
| 14 | 'asdict', |
| 15 | 'astuple', |
| 16 | 'make_dataclass', |
| 17 | 'replace', |
Eric V. Smith | e7ba013 | 2018-01-06 12:41:53 -0500 | [diff] [blame] | 18 | 'is_dataclass', |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 19 | ] |
| 20 | |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 21 | # Conditions for adding methods. The boxes indicate what action the |
| 22 | # dataclass decorator takes. For all of these tables, when I talk |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 23 | # about init=, repr=, eq=, order=, unsafe_hash=, or frozen=, I'm |
| 24 | # referring to the arguments to the @dataclass decorator. When |
| 25 | # checking if a dunder method already exists, I mean check for an |
| 26 | # entry in the class's __dict__. I never check to see if an |
| 27 | # attribute is defined in a base class. |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 28 | |
| 29 | # Key: |
| 30 | # +=========+=========================================+ |
| 31 | # + Value | Meaning | |
| 32 | # +=========+=========================================+ |
| 33 | # | <blank> | No action: no method is added. | |
| 34 | # +---------+-----------------------------------------+ |
| 35 | # | add | Generated method is added. | |
| 36 | # +---------+-----------------------------------------+ |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 37 | # | raise | TypeError is raised. | |
| 38 | # +---------+-----------------------------------------+ |
| 39 | # | None | Attribute is set to None. | |
| 40 | # +=========+=========================================+ |
| 41 | |
| 42 | # __init__ |
| 43 | # |
| 44 | # +--- init= parameter |
| 45 | # | |
| 46 | # v | | | |
| 47 | # | no | yes | <--- class has __init__ in __dict__? |
| 48 | # +=======+=======+=======+ |
| 49 | # | False | | | |
| 50 | # +-------+-------+-------+ |
| 51 | # | True | add | | <- the default |
| 52 | # +=======+=======+=======+ |
| 53 | |
| 54 | # __repr__ |
| 55 | # |
| 56 | # +--- repr= parameter |
| 57 | # | |
| 58 | # v | | | |
| 59 | # | no | yes | <--- class has __repr__ in __dict__? |
| 60 | # +=======+=======+=======+ |
| 61 | # | False | | | |
| 62 | # +-------+-------+-------+ |
| 63 | # | True | add | | <- the default |
| 64 | # +=======+=======+=======+ |
| 65 | |
| 66 | |
| 67 | # __setattr__ |
| 68 | # __delattr__ |
| 69 | # |
| 70 | # +--- frozen= parameter |
| 71 | # | |
| 72 | # v | | | |
| 73 | # | no | yes | <--- class has __setattr__ or __delattr__ in __dict__? |
| 74 | # +=======+=======+=======+ |
| 75 | # | False | | | <- the default |
| 76 | # +-------+-------+-------+ |
| 77 | # | True | add | raise | |
| 78 | # +=======+=======+=======+ |
| 79 | # Raise because not adding these methods would break the "frozen-ness" |
| 80 | # of the class. |
| 81 | |
| 82 | # __eq__ |
| 83 | # |
| 84 | # +--- eq= parameter |
| 85 | # | |
| 86 | # v | | | |
| 87 | # | no | yes | <--- class has __eq__ in __dict__? |
| 88 | # +=======+=======+=======+ |
| 89 | # | False | | | |
| 90 | # +-------+-------+-------+ |
| 91 | # | True | add | | <- the default |
| 92 | # +=======+=======+=======+ |
| 93 | |
| 94 | # __lt__ |
| 95 | # __le__ |
| 96 | # __gt__ |
| 97 | # __ge__ |
| 98 | # |
| 99 | # +--- order= parameter |
| 100 | # | |
| 101 | # v | | | |
| 102 | # | no | yes | <--- class has any comparison method in __dict__? |
| 103 | # +=======+=======+=======+ |
| 104 | # | False | | | <- the default |
| 105 | # +-------+-------+-------+ |
| 106 | # | True | add | raise | |
| 107 | # +=======+=======+=======+ |
| 108 | # Raise because to allow this case would interfere with using |
| 109 | # functools.total_ordering. |
| 110 | |
| 111 | # __hash__ |
| 112 | |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 113 | # +------------------- unsafe_hash= parameter |
| 114 | # | +----------- eq= parameter |
| 115 | # | | +--- frozen= parameter |
| 116 | # | | | |
| 117 | # v v v | | | |
| 118 | # | no | yes | <--- class has explicitly defined __hash__ |
| 119 | # +=======+=======+=======+========+========+ |
| 120 | # | False | False | False | | | No __eq__, use the base class __hash__ |
| 121 | # +-------+-------+-------+--------+--------+ |
| 122 | # | False | False | True | | | No __eq__, use the base class __hash__ |
| 123 | # +-------+-------+-------+--------+--------+ |
| 124 | # | False | True | False | None | | <-- the default, not hashable |
| 125 | # +-------+-------+-------+--------+--------+ |
| 126 | # | False | True | True | add | | Frozen, so hashable, allows override |
| 127 | # +-------+-------+-------+--------+--------+ |
| 128 | # | True | False | False | add | raise | Has no __eq__, but hashable |
| 129 | # +-------+-------+-------+--------+--------+ |
| 130 | # | True | False | True | add | raise | Has no __eq__, but hashable |
| 131 | # +-------+-------+-------+--------+--------+ |
| 132 | # | True | True | False | add | raise | Not frozen, but hashable |
| 133 | # +-------+-------+-------+--------+--------+ |
| 134 | # | True | True | True | add | raise | Frozen, so hashable |
| 135 | # +=======+=======+=======+========+========+ |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 136 | # For boxes that are blank, __hash__ is untouched and therefore |
| 137 | # inherited from the base class. If the base is object, then |
| 138 | # id-based hashing is used. |
| 139 | # Note that a class may have already __hash__=None if it specified an |
| 140 | # __eq__ method in the class body (not one that was created by |
| 141 | # @dataclass). |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 142 | # See _hash_action (below) for a coded version of this table. |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 143 | |
| 144 | |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 145 | # Raised when an attempt is made to modify a frozen class. |
| 146 | class FrozenInstanceError(AttributeError): pass |
| 147 | |
| 148 | # A sentinel object for default values to signal that a |
| 149 | # default-factory will be used. |
| 150 | # This is given a nice repr() which will appear in the function |
| 151 | # signature of dataclasses' constructors. |
| 152 | class _HAS_DEFAULT_FACTORY_CLASS: |
| 153 | def __repr__(self): |
| 154 | return '<factory>' |
| 155 | _HAS_DEFAULT_FACTORY = _HAS_DEFAULT_FACTORY_CLASS() |
| 156 | |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 157 | # A sentinel object to detect if a parameter is supplied or not. Use |
| 158 | # a class to give it a better repr. |
| 159 | class _MISSING_TYPE: |
| 160 | pass |
| 161 | MISSING = _MISSING_TYPE() |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 162 | |
| 163 | # Since most per-field metadata will be unused, create an empty |
| 164 | # read-only proxy that can be shared among all fields. |
| 165 | _EMPTY_METADATA = types.MappingProxyType({}) |
| 166 | |
| 167 | # Markers for the various kinds of fields and pseudo-fields. |
| 168 | _FIELD = object() # An actual field. |
| 169 | _FIELD_CLASSVAR = object() # Not a field, but a ClassVar. |
| 170 | _FIELD_INITVAR = object() # Not a field, but an InitVar. |
| 171 | |
| 172 | # The name of an attribute on the class where we store the Field |
| 173 | # objects. Also used to check if a class is a Data Class. |
| 174 | _MARKER = '__dataclass_fields__' |
| 175 | |
| 176 | # The name of the function, that if it exists, is called at the end of |
| 177 | # __init__. |
| 178 | _POST_INIT_NAME = '__post_init__' |
| 179 | |
| 180 | |
| 181 | class _InitVarMeta(type): |
| 182 | def __getitem__(self, params): |
| 183 | return self |
| 184 | |
| 185 | class InitVar(metaclass=_InitVarMeta): |
| 186 | pass |
| 187 | |
| 188 | |
| 189 | # Instances of Field are only ever created from within this module, |
| 190 | # and only from the field() function, although Field instances are |
| 191 | # exposed externally as (conceptually) read-only objects. |
| 192 | # name and type are filled in after the fact, not in __init__. They're |
| 193 | # not known at the time this class is instantiated, but it's |
| 194 | # convenient if they're available later. |
| 195 | # When cls._MARKER is filled in with a list of Field objects, the name |
| 196 | # and type fields will have been populated. |
| 197 | class Field: |
| 198 | __slots__ = ('name', |
| 199 | 'type', |
| 200 | 'default', |
| 201 | 'default_factory', |
| 202 | 'repr', |
| 203 | 'hash', |
| 204 | 'init', |
| 205 | 'compare', |
| 206 | 'metadata', |
| 207 | '_field_type', # Private: not to be used by user code. |
| 208 | ) |
| 209 | |
| 210 | def __init__(self, default, default_factory, init, repr, hash, compare, |
| 211 | metadata): |
| 212 | self.name = None |
| 213 | self.type = None |
| 214 | self.default = default |
| 215 | self.default_factory = default_factory |
| 216 | self.init = init |
| 217 | self.repr = repr |
| 218 | self.hash = hash |
| 219 | self.compare = compare |
| 220 | self.metadata = (_EMPTY_METADATA |
| 221 | if metadata is None or len(metadata) == 0 else |
| 222 | types.MappingProxyType(metadata)) |
| 223 | self._field_type = None |
| 224 | |
| 225 | def __repr__(self): |
| 226 | return ('Field(' |
| 227 | f'name={self.name!r},' |
| 228 | f'type={self.type},' |
| 229 | f'default={self.default},' |
| 230 | f'default_factory={self.default_factory},' |
| 231 | f'init={self.init},' |
| 232 | f'repr={self.repr},' |
| 233 | f'hash={self.hash},' |
| 234 | f'compare={self.compare},' |
| 235 | f'metadata={self.metadata}' |
| 236 | ')') |
| 237 | |
| 238 | |
| 239 | # This function is used instead of exposing Field creation directly, |
| 240 | # so that a type checker can be told (via overloads) that this is a |
| 241 | # function whose type depends on its parameters. |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 242 | def field(*, default=MISSING, default_factory=MISSING, init=True, repr=True, |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 243 | hash=None, compare=True, metadata=None): |
| 244 | """Return an object to identify dataclass fields. |
| 245 | |
| 246 | default is the default value of the field. default_factory is a |
| 247 | 0-argument function called to initialize a field's value. If init |
| 248 | is True, the field will be a parameter to the class's __init__() |
| 249 | function. If repr is True, the field will be included in the |
| 250 | object's repr(). If hash is True, the field will be included in |
| 251 | the object's hash(). If compare is True, the field will be used in |
| 252 | comparison functions. metadata, if specified, must be a mapping |
| 253 | which is stored but not otherwise examined by dataclass. |
| 254 | |
| 255 | It is an error to specify both default and default_factory. |
| 256 | """ |
| 257 | |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 258 | if default is not MISSING and default_factory is not MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 259 | raise ValueError('cannot specify both default and default_factory') |
| 260 | return Field(default, default_factory, init, repr, hash, compare, |
| 261 | metadata) |
| 262 | |
| 263 | |
| 264 | def _tuple_str(obj_name, fields): |
| 265 | # Return a string representing each field of obj_name as a tuple |
| 266 | # member. So, if fields is ['x', 'y'] and obj_name is "self", |
| 267 | # return "(self.x,self.y)". |
| 268 | |
| 269 | # Special case for the 0-tuple. |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 270 | if not fields: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 271 | return '()' |
| 272 | # Note the trailing comma, needed if this turns out to be a 1-tuple. |
| 273 | return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)' |
| 274 | |
| 275 | |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 276 | def _create_fn(name, args, body, *, globals=None, locals=None, |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 277 | return_type=MISSING): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 278 | # Note that we mutate locals when exec() is called. Caller beware! |
| 279 | if locals is None: |
| 280 | locals = {} |
| 281 | return_annotation = '' |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 282 | if return_type is not MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 283 | locals['_return_type'] = return_type |
| 284 | return_annotation = '->_return_type' |
| 285 | args = ','.join(args) |
| 286 | body = '\n'.join(f' {b}' for b in body) |
| 287 | |
| 288 | txt = f'def {name}({args}){return_annotation}:\n{body}' |
| 289 | |
| 290 | exec(txt, globals, locals) |
| 291 | return locals[name] |
| 292 | |
| 293 | |
| 294 | def _field_assign(frozen, name, value, self_name): |
| 295 | # If we're a frozen class, then assign to our fields in __init__ |
| 296 | # via object.__setattr__. Otherwise, just use a simple |
| 297 | # assignment. |
| 298 | # self_name is what "self" is called in this function: don't |
| 299 | # hard-code "self", since that might be a field name. |
| 300 | if frozen: |
| 301 | return f'object.__setattr__({self_name},{name!r},{value})' |
| 302 | return f'{self_name}.{name}={value}' |
| 303 | |
| 304 | |
| 305 | def _field_init(f, frozen, globals, self_name): |
| 306 | # Return the text of the line in the body of __init__ that will |
| 307 | # initialize this field. |
| 308 | |
| 309 | default_name = f'_dflt_{f.name}' |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 310 | if f.default_factory is not MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 311 | if f.init: |
| 312 | # This field has a default factory. If a parameter is |
| 313 | # given, use it. If not, call the factory. |
| 314 | globals[default_name] = f.default_factory |
| 315 | value = (f'{default_name}() ' |
| 316 | f'if {f.name} is _HAS_DEFAULT_FACTORY ' |
| 317 | f'else {f.name}') |
| 318 | else: |
| 319 | # This is a field that's not in the __init__ params, but |
| 320 | # has a default factory function. It needs to be |
| 321 | # initialized here by calling the factory function, |
| 322 | # because there's no other way to initialize it. |
| 323 | |
| 324 | # For a field initialized with a default=defaultvalue, the |
| 325 | # class dict just has the default value |
| 326 | # (cls.fieldname=defaultvalue). But that won't work for a |
| 327 | # default factory, the factory must be called in __init__ |
| 328 | # and we must assign that to self.fieldname. We can't |
| 329 | # fall back to the class dict's value, both because it's |
| 330 | # not set, and because it might be different per-class |
| 331 | # (which, after all, is why we have a factory function!). |
| 332 | |
| 333 | globals[default_name] = f.default_factory |
| 334 | value = f'{default_name}()' |
| 335 | else: |
| 336 | # No default factory. |
| 337 | if f.init: |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 338 | if f.default is MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 339 | # There's no default, just do an assignment. |
| 340 | value = f.name |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 341 | elif f.default is not MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 342 | globals[default_name] = f.default |
| 343 | value = f.name |
| 344 | else: |
| 345 | # This field does not need initialization. Signify that to |
| 346 | # the caller by returning None. |
| 347 | return None |
| 348 | |
| 349 | # Only test this now, so that we can create variables for the |
| 350 | # default. However, return None to signify that we're not going |
| 351 | # to actually do the assignment statement for InitVars. |
| 352 | if f._field_type == _FIELD_INITVAR: |
| 353 | return None |
| 354 | |
| 355 | # Now, actually generate the field assignment. |
| 356 | return _field_assign(frozen, f.name, value, self_name) |
| 357 | |
| 358 | |
| 359 | def _init_param(f): |
| 360 | # Return the __init__ parameter string for this field. |
| 361 | # For example, the equivalent of 'x:int=3' (except instead of 'int', |
| 362 | # reference a variable set to int, and instead of '3', reference a |
| 363 | # variable set to 3). |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 364 | if f.default is MISSING and f.default_factory is MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 365 | # There's no default, and no default_factory, just |
| 366 | # output the variable name and type. |
| 367 | default = '' |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 368 | elif f.default is not MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 369 | # There's a default, this will be the name that's used to look it up. |
| 370 | default = f'=_dflt_{f.name}' |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 371 | elif f.default_factory is not MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 372 | # There's a factory function. Set a marker. |
| 373 | default = '=_HAS_DEFAULT_FACTORY' |
| 374 | return f'{f.name}:_type_{f.name}{default}' |
| 375 | |
| 376 | |
| 377 | def _init_fn(fields, frozen, has_post_init, self_name): |
| 378 | # fields contains both real fields and InitVar pseudo-fields. |
| 379 | |
| 380 | # Make sure we don't have fields without defaults following fields |
| 381 | # with defaults. This actually would be caught when exec-ing the |
| 382 | # function source code, but catching it here gives a better error |
| 383 | # message, and future-proofs us in case we build up the function |
| 384 | # using ast. |
| 385 | seen_default = False |
| 386 | for f in fields: |
| 387 | # Only consider fields in the __init__ call. |
| 388 | if f.init: |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 389 | if not (f.default is MISSING and f.default_factory is MISSING): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 390 | seen_default = True |
| 391 | elif seen_default: |
| 392 | raise TypeError(f'non-default argument {f.name!r} ' |
| 393 | 'follows default argument') |
| 394 | |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 395 | globals = {'MISSING': MISSING, |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 396 | '_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY} |
| 397 | |
| 398 | body_lines = [] |
| 399 | for f in fields: |
| 400 | # Do not initialize the pseudo-fields, only the real ones. |
| 401 | line = _field_init(f, frozen, globals, self_name) |
| 402 | if line is not None: |
| 403 | # line is None means that this field doesn't require |
| 404 | # initialization. Just skip it. |
| 405 | body_lines.append(line) |
| 406 | |
| 407 | # Does this class have a post-init function? |
| 408 | if has_post_init: |
| 409 | params_str = ','.join(f.name for f in fields |
| 410 | if f._field_type is _FIELD_INITVAR) |
| 411 | body_lines += [f'{self_name}.{_POST_INIT_NAME}({params_str})'] |
| 412 | |
| 413 | # If no body lines, use 'pass'. |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 414 | if not body_lines: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 415 | body_lines = ['pass'] |
| 416 | |
| 417 | locals = {f'_type_{f.name}': f.type for f in fields} |
| 418 | return _create_fn('__init__', |
| 419 | [self_name] +[_init_param(f) for f in fields if f.init], |
| 420 | body_lines, |
| 421 | locals=locals, |
| 422 | globals=globals, |
| 423 | return_type=None) |
| 424 | |
| 425 | |
| 426 | def _repr_fn(fields): |
| 427 | return _create_fn('__repr__', |
| 428 | ['self'], |
| 429 | ['return self.__class__.__qualname__ + f"(' + |
| 430 | ', '.join([f"{f.name}={{self.{f.name}!r}}" |
| 431 | for f in fields]) + |
| 432 | ')"']) |
| 433 | |
| 434 | |
| 435 | def _frozen_setattr(self, name, value): |
| 436 | raise FrozenInstanceError(f'cannot assign to field {name!r}') |
| 437 | |
| 438 | |
| 439 | def _frozen_delattr(self, name): |
| 440 | raise FrozenInstanceError(f'cannot delete field {name!r}') |
| 441 | |
| 442 | |
| 443 | def _cmp_fn(name, op, self_tuple, other_tuple): |
| 444 | # Create a comparison function. If the fields in the object are |
| 445 | # named 'x' and 'y', then self_tuple is the string |
| 446 | # '(self.x,self.y)' and other_tuple is the string |
| 447 | # '(other.x,other.y)'. |
| 448 | |
| 449 | return _create_fn(name, |
| 450 | ['self', 'other'], |
| 451 | [ 'if other.__class__ is self.__class__:', |
| 452 | f' return {self_tuple}{op}{other_tuple}', |
| 453 | 'return NotImplemented']) |
| 454 | |
| 455 | |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 456 | def _hash_fn(fields): |
| 457 | self_tuple = _tuple_str('self', fields) |
| 458 | return _create_fn('__hash__', |
| 459 | ['self'], |
| 460 | [f'return hash({self_tuple})']) |
| 461 | |
| 462 | |
| 463 | def _get_field(cls, a_name, a_type): |
| 464 | # Return a Field object, for this field name and type. ClassVars |
| 465 | # and InitVars are also returned, but marked as such (see |
| 466 | # f._field_type). |
| 467 | |
| 468 | # If the default value isn't derived from field, then it's |
| 469 | # only a normal default value. Convert it to a Field(). |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 470 | default = getattr(cls, a_name, MISSING) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 471 | if isinstance(default, Field): |
| 472 | f = default |
| 473 | else: |
| 474 | f = field(default=default) |
| 475 | |
| 476 | # Assume it's a normal field until proven otherwise. |
| 477 | f._field_type = _FIELD |
| 478 | |
| 479 | # Only at this point do we know the name and the type. Set them. |
| 480 | f.name = a_name |
| 481 | f.type = a_type |
| 482 | |
| 483 | # If typing has not been imported, then it's impossible for |
| 484 | # any annotation to be a ClassVar. So, only look for ClassVar |
| 485 | # if typing has been imported. |
| 486 | typing = sys.modules.get('typing') |
| 487 | if typing is not None: |
| 488 | # This test uses a typing internal class, but it's the best |
| 489 | # way to test if this is a ClassVar. |
Ivan Levkivskyi | d911e40 | 2018-01-20 11:23:59 +0000 | [diff] [blame] | 490 | if (type(a_type) is typing._GenericAlias and |
| 491 | a_type.__origin__ is typing.ClassVar): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 492 | # This field is a ClassVar, so it's not a field. |
| 493 | f._field_type = _FIELD_CLASSVAR |
| 494 | |
| 495 | if f._field_type is _FIELD: |
| 496 | # Check if this is an InitVar. |
| 497 | if a_type is InitVar: |
| 498 | # InitVars are not fields, either. |
| 499 | f._field_type = _FIELD_INITVAR |
| 500 | |
| 501 | # Validations for fields. This is delayed until now, instead of |
| 502 | # in the Field() constructor, since only here do we know the field |
| 503 | # name, which allows better error reporting. |
| 504 | |
| 505 | # Special restrictions for ClassVar and InitVar. |
| 506 | if f._field_type in (_FIELD_CLASSVAR, _FIELD_INITVAR): |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 507 | if f.default_factory is not MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 508 | raise TypeError(f'field {f.name} cannot have a ' |
| 509 | 'default factory') |
| 510 | # Should I check for other field settings? default_factory |
| 511 | # seems the most serious to check for. Maybe add others. For |
| 512 | # example, how about init=False (or really, |
| 513 | # init=<not-the-default-init-value>)? It makes no sense for |
| 514 | # ClassVar and InitVar to specify init=<anything>. |
| 515 | |
| 516 | # For real fields, disallow mutable defaults for known types. |
| 517 | if f._field_type is _FIELD and isinstance(f.default, (list, dict, set)): |
| 518 | raise ValueError(f'mutable default {type(f.default)} for field ' |
| 519 | f'{f.name} is not allowed: use default_factory') |
| 520 | |
| 521 | return f |
| 522 | |
| 523 | |
| 524 | def _find_fields(cls): |
| 525 | # Return a list of Field objects, in order, for this class (and no |
| 526 | # base classes). Fields are found from __annotations__ (which is |
| 527 | # guaranteed to be ordered). Default values are from class |
| 528 | # attributes, if a field has a default. If the default value is |
| 529 | # a Field(), then it contains additional info beyond (and |
| 530 | # possibly including) the actual default value. Pseudo-fields |
| 531 | # ClassVars and InitVars are included, despite the fact that |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 532 | # they're not real fields. That's dealt with later. |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 533 | |
| 534 | annotations = getattr(cls, '__annotations__', {}) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 535 | return [_get_field(cls, a_name, a_type) |
| 536 | for a_name, a_type in annotations.items()] |
| 537 | |
| 538 | |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 539 | def _set_new_attribute(cls, name, value): |
| 540 | # Never overwrites an existing attribute. Returns True if the |
| 541 | # attribute already exists. |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 542 | if name in cls.__dict__: |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 543 | return True |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 544 | setattr(cls, name, value) |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 545 | return False |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 546 | |
| 547 | |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 548 | # Decide if/how we're going to create a hash function. Key is |
| 549 | # (unsafe_hash, eq, frozen, does-hash-exist). Value is the action to |
| 550 | # take. |
| 551 | # Actions: |
| 552 | # '': Do nothing. |
| 553 | # 'none': Set __hash__ to None. |
| 554 | # 'add': Always add a generated __hash__function. |
| 555 | # 'exception': Raise an exception. |
| 556 | # |
| 557 | # +-------------------------------------- unsafe_hash? |
| 558 | # | +------------------------------- eq? |
| 559 | # | | +------------------------ frozen? |
| 560 | # | | | +---------------- has-explicit-hash? |
| 561 | # | | | | |
| 562 | # | | | | +------- action |
| 563 | # | | | | | |
| 564 | # v v v v v |
| 565 | _hash_action = {(False, False, False, False): (''), |
| 566 | (False, False, False, True ): (''), |
| 567 | (False, False, True, False): (''), |
| 568 | (False, False, True, True ): (''), |
| 569 | (False, True, False, False): ('none'), |
| 570 | (False, True, False, True ): (''), |
| 571 | (False, True, True, False): ('add'), |
| 572 | (False, True, True, True ): (''), |
| 573 | (True, False, False, False): ('add'), |
| 574 | (True, False, False, True ): ('exception'), |
| 575 | (True, False, True, False): ('add'), |
| 576 | (True, False, True, True ): ('exception'), |
| 577 | (True, True, False, False): ('add'), |
| 578 | (True, True, False, True ): ('exception'), |
| 579 | (True, True, True, False): ('add'), |
| 580 | (True, True, True, True ): ('exception'), |
| 581 | } |
| 582 | # See https://bugs.python.org/issue32929#msg312829 for an if-statement |
| 583 | # version of this table. |
| 584 | |
| 585 | |
| 586 | def _process_class(cls, repr, eq, order, unsafe_hash, init, frozen): |
Eric V. Smith | d138892 | 2018-01-07 14:30:17 -0500 | [diff] [blame] | 587 | # Now that dicts retain insertion order, there's no reason to use |
| 588 | # an ordered dict. I am leveraging that ordering here, because |
| 589 | # derived class fields overwrite base class fields, but the order |
| 590 | # is defined by the base class, which is found first. |
| 591 | fields = {} |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 592 | |
| 593 | # Find our base classes in reverse MRO order, and exclude |
| 594 | # ourselves. In reversed order so that more derived classes |
| 595 | # override earlier field definitions in base classes. |
| 596 | for b in cls.__mro__[-1:0:-1]: |
| 597 | # Only process classes that have been processed by our |
| 598 | # decorator. That is, they have a _MARKER attribute. |
| 599 | base_fields = getattr(b, _MARKER, None) |
| 600 | if base_fields: |
| 601 | for f in base_fields.values(): |
| 602 | fields[f.name] = f |
| 603 | |
| 604 | # Now find fields in our class. While doing so, validate some |
| 605 | # things, and set the default values (as class attributes) |
| 606 | # where we can. |
| 607 | for f in _find_fields(cls): |
| 608 | fields[f.name] = f |
| 609 | |
| 610 | # If the class attribute (which is the default value for |
| 611 | # this field) exists and is of type 'Field', replace it |
| 612 | # with the real default. This is so that normal class |
| 613 | # introspection sees a real default value, not a Field. |
| 614 | if isinstance(getattr(cls, f.name, None), Field): |
Eric V. Smith | 03220fd | 2017-12-29 13:59:58 -0500 | [diff] [blame] | 615 | if f.default is MISSING: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 616 | # If there's no default, delete the class attribute. |
| 617 | # This happens if we specify field(repr=False), for |
| 618 | # example (that is, we specified a field object, but |
| 619 | # no default value). Also if we're using a default |
| 620 | # factory. The class attribute should not be set at |
| 621 | # all in the post-processed class. |
| 622 | delattr(cls, f.name) |
| 623 | else: |
| 624 | setattr(cls, f.name, f.default) |
| 625 | |
Eric V. Smith | 2fa6b9e | 2018-02-26 20:38:33 -0500 | [diff] [blame] | 626 | # We're inheriting from a frozen dataclass, but we're not frozen. |
| 627 | if cls.__setattr__ is _frozen_setattr and not frozen: |
| 628 | raise TypeError('cannot inherit non-frozen dataclass from a ' |
| 629 | 'frozen one') |
| 630 | |
| 631 | # We're inheriting from a non-frozen dataclass, but we're frozen. |
| 632 | if (hasattr(cls, _MARKER) and cls.__setattr__ is not _frozen_setattr |
| 633 | and frozen): |
| 634 | raise TypeError('cannot inherit frozen dataclass from a ' |
| 635 | 'non-frozen one') |
| 636 | |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 637 | # Remember all of the fields on our class (including bases). This |
| 638 | # marks this class as being a dataclass. |
| 639 | setattr(cls, _MARKER, fields) |
| 640 | |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 641 | # Was this class defined with an explicit __hash__? Note that if |
| 642 | # __eq__ is defined in this class, then python will automatically |
| 643 | # set __hash__ to None. This is a heuristic, as it's possible |
| 644 | # that such a __hash__ == None was not auto-generated, but it |
| 645 | # close enough. |
| 646 | class_hash = cls.__dict__.get('__hash__', MISSING) |
| 647 | has_explicit_hash = not (class_hash is MISSING or |
| 648 | (class_hash is None and '__eq__' in cls.__dict__)) |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 649 | |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 650 | # If we're generating ordering methods, we must be generating |
| 651 | # the eq methods. |
| 652 | if order and not eq: |
| 653 | raise ValueError('eq must be true if order is true') |
| 654 | |
| 655 | if init: |
| 656 | # Does this class have a post-init function? |
| 657 | has_post_init = hasattr(cls, _POST_INIT_NAME) |
| 658 | |
| 659 | # Include InitVars and regular fields (so, not ClassVars). |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 660 | flds = [f for f in fields.values() |
| 661 | if f._field_type in (_FIELD, _FIELD_INITVAR)] |
| 662 | _set_new_attribute(cls, '__init__', |
| 663 | _init_fn(flds, |
Eric V. Smith | 2fa6b9e | 2018-02-26 20:38:33 -0500 | [diff] [blame] | 664 | frozen, |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 665 | has_post_init, |
| 666 | # The name to use for the "self" param |
| 667 | # in __init__. Use "self" if possible. |
| 668 | '__dataclass_self__' if 'self' in fields |
| 669 | else 'self', |
| 670 | )) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 671 | |
| 672 | # Get the fields as a list, and include only real fields. This is |
| 673 | # used in all of the following methods. |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 674 | field_list = [f for f in fields.values() if f._field_type is _FIELD] |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 675 | |
| 676 | if repr: |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 677 | flds = [f for f in field_list if f.repr] |
| 678 | _set_new_attribute(cls, '__repr__', _repr_fn(flds)) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 679 | |
| 680 | if eq: |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 681 | # Create _eq__ method. There's no need for a __ne__ method, |
| 682 | # since python will call __eq__ and negate it. |
| 683 | flds = [f for f in field_list if f.compare] |
| 684 | self_tuple = _tuple_str('self', flds) |
| 685 | other_tuple = _tuple_str('other', flds) |
| 686 | _set_new_attribute(cls, '__eq__', |
| 687 | _cmp_fn('__eq__', '==', |
| 688 | self_tuple, other_tuple)) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 689 | |
| 690 | if order: |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 691 | # Create and set the ordering methods. |
| 692 | flds = [f for f in field_list if f.compare] |
| 693 | self_tuple = _tuple_str('self', flds) |
| 694 | other_tuple = _tuple_str('other', flds) |
| 695 | for name, op in [('__lt__', '<'), |
| 696 | ('__le__', '<='), |
| 697 | ('__gt__', '>'), |
| 698 | ('__ge__', '>='), |
| 699 | ]: |
| 700 | if _set_new_attribute(cls, name, |
| 701 | _cmp_fn(name, op, self_tuple, other_tuple)): |
| 702 | raise TypeError(f'Cannot overwrite attribute {name} ' |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 703 | f'in class {cls.__name__}. Consider using ' |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 704 | 'functools.total_ordering') |
| 705 | |
Eric V. Smith | 2fa6b9e | 2018-02-26 20:38:33 -0500 | [diff] [blame] | 706 | if frozen: |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 707 | for name, fn in [('__setattr__', _frozen_setattr), |
| 708 | ('__delattr__', _frozen_delattr)]: |
| 709 | if _set_new_attribute(cls, name, fn): |
| 710 | raise TypeError(f'Cannot overwrite attribute {name} ' |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 711 | f'in class {cls.__name__}') |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 712 | |
| 713 | # Decide if/how we're going to create a hash function. |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 714 | hash_action = _hash_action[bool(unsafe_hash), |
| 715 | bool(eq), |
| 716 | bool(frozen), |
| 717 | has_explicit_hash] |
| 718 | |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 719 | # No need to call _set_new_attribute here, since we already know if |
| 720 | # we're overwriting a __hash__ or not. |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 721 | if hash_action == '': |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 722 | # Do nothing. |
| 723 | pass |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 724 | elif hash_action == 'none': |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 725 | cls.__hash__ = None |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 726 | elif hash_action == 'add': |
| 727 | flds = [f for f in field_list if (f.compare if f.hash is None else f.hash)] |
| 728 | cls.__hash__ = _hash_fn(flds) |
| 729 | elif hash_action == 'exception': |
| 730 | # Raise an exception. |
| 731 | raise TypeError(f'Cannot overwrite attribute __hash__ ' |
| 732 | f'in class {cls.__name__}') |
Eric V. Smith | ea8fc52 | 2018-01-27 19:07:40 -0500 | [diff] [blame] | 733 | else: |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 734 | assert False, f"can't get here: {hash_action}" |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 735 | |
| 736 | if not getattr(cls, '__doc__'): |
| 737 | # Create a class doc-string. |
| 738 | cls.__doc__ = (cls.__name__ + |
| 739 | str(inspect.signature(cls)).replace(' -> None', '')) |
| 740 | |
| 741 | return cls |
| 742 | |
| 743 | |
| 744 | # _cls should never be specified by keyword, so start it with an |
Raymond Hettinger | d55209d | 2018-01-10 20:56:41 -0800 | [diff] [blame] | 745 | # underscore. The presence of _cls is used to detect if this |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 746 | # decorator is being called with parameters or not. |
| 747 | def dataclass(_cls=None, *, init=True, repr=True, eq=True, order=False, |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 748 | unsafe_hash=None, frozen=False): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 749 | """Returns the same class as was passed in, with dunder methods |
| 750 | added based on the fields defined in the class. |
| 751 | |
| 752 | Examines PEP 526 __annotations__ to determine fields. |
| 753 | |
| 754 | If init is true, an __init__() method is added to the class. If |
| 755 | repr is true, a __repr__() method is added. If order is true, rich |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 756 | comparison dunder methods are added. If unsafe_hash is true, a |
| 757 | __hash__() method function is added. If frozen is true, fields may |
| 758 | not be assigned to after instance creation. |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 759 | """ |
| 760 | |
| 761 | def wrap(cls): |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 762 | return _process_class(cls, repr, eq, order, unsafe_hash, init, frozen) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 763 | |
| 764 | # See if we're being called as @dataclass or @dataclass(). |
| 765 | if _cls is None: |
| 766 | # We're called with parens. |
| 767 | return wrap |
| 768 | |
| 769 | # We're called as @dataclass without parens. |
| 770 | return wrap(_cls) |
| 771 | |
| 772 | |
| 773 | def fields(class_or_instance): |
| 774 | """Return a tuple describing the fields of this dataclass. |
| 775 | |
| 776 | Accepts a dataclass or an instance of one. Tuple elements are of |
| 777 | type Field. |
| 778 | """ |
| 779 | |
| 780 | # Might it be worth caching this, per class? |
| 781 | try: |
| 782 | fields = getattr(class_or_instance, _MARKER) |
| 783 | except AttributeError: |
| 784 | raise TypeError('must be called with a dataclass type or instance') |
| 785 | |
Eric V. Smith | d138892 | 2018-01-07 14:30:17 -0500 | [diff] [blame] | 786 | # Exclude pseudo-fields. Note that fields is sorted by insertion |
| 787 | # order, so the order of the tuple is as the fields were defined. |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 788 | return tuple(f for f in fields.values() if f._field_type is _FIELD) |
| 789 | |
| 790 | |
Eric V. Smith | e7ba013 | 2018-01-06 12:41:53 -0500 | [diff] [blame] | 791 | def _is_dataclass_instance(obj): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 792 | """Returns True if obj is an instance of a dataclass.""" |
| 793 | return not isinstance(obj, type) and hasattr(obj, _MARKER) |
| 794 | |
| 795 | |
Eric V. Smith | e7ba013 | 2018-01-06 12:41:53 -0500 | [diff] [blame] | 796 | def is_dataclass(obj): |
| 797 | """Returns True if obj is a dataclass or an instance of a |
| 798 | dataclass.""" |
| 799 | return hasattr(obj, _MARKER) |
| 800 | |
| 801 | |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 802 | def asdict(obj, *, dict_factory=dict): |
| 803 | """Return the fields of a dataclass instance as a new dictionary mapping |
| 804 | field names to field values. |
| 805 | |
| 806 | Example usage: |
| 807 | |
| 808 | @dataclass |
| 809 | class C: |
| 810 | x: int |
| 811 | y: int |
| 812 | |
| 813 | c = C(1, 2) |
| 814 | assert asdict(c) == {'x': 1, 'y': 2} |
| 815 | |
| 816 | If given, 'dict_factory' will be used instead of built-in dict. |
| 817 | The function applies recursively to field values that are |
| 818 | dataclass instances. This will also look into built-in containers: |
| 819 | tuples, lists, and dicts. |
| 820 | """ |
Eric V. Smith | e7ba013 | 2018-01-06 12:41:53 -0500 | [diff] [blame] | 821 | if not _is_dataclass_instance(obj): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 822 | raise TypeError("asdict() should be called on dataclass instances") |
| 823 | return _asdict_inner(obj, dict_factory) |
| 824 | |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 825 | |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 826 | def _asdict_inner(obj, dict_factory): |
Eric V. Smith | e7ba013 | 2018-01-06 12:41:53 -0500 | [diff] [blame] | 827 | if _is_dataclass_instance(obj): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 828 | result = [] |
| 829 | for f in fields(obj): |
| 830 | value = _asdict_inner(getattr(obj, f.name), dict_factory) |
| 831 | result.append((f.name, value)) |
| 832 | return dict_factory(result) |
| 833 | elif isinstance(obj, (list, tuple)): |
| 834 | return type(obj)(_asdict_inner(v, dict_factory) for v in obj) |
| 835 | elif isinstance(obj, dict): |
| 836 | return type(obj)((_asdict_inner(k, dict_factory), _asdict_inner(v, dict_factory)) |
| 837 | for k, v in obj.items()) |
| 838 | else: |
| 839 | return deepcopy(obj) |
| 840 | |
| 841 | |
| 842 | def astuple(obj, *, tuple_factory=tuple): |
| 843 | """Return the fields of a dataclass instance as a new tuple of field values. |
| 844 | |
| 845 | Example usage:: |
| 846 | |
| 847 | @dataclass |
| 848 | class C: |
| 849 | x: int |
| 850 | y: int |
| 851 | |
| 852 | c = C(1, 2) |
Raymond Hettinger | d55209d | 2018-01-10 20:56:41 -0800 | [diff] [blame] | 853 | assert astuple(c) == (1, 2) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 854 | |
| 855 | If given, 'tuple_factory' will be used instead of built-in tuple. |
| 856 | The function applies recursively to field values that are |
| 857 | dataclass instances. This will also look into built-in containers: |
| 858 | tuples, lists, and dicts. |
| 859 | """ |
| 860 | |
Eric V. Smith | e7ba013 | 2018-01-06 12:41:53 -0500 | [diff] [blame] | 861 | if not _is_dataclass_instance(obj): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 862 | raise TypeError("astuple() should be called on dataclass instances") |
| 863 | return _astuple_inner(obj, tuple_factory) |
| 864 | |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 865 | |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 866 | def _astuple_inner(obj, tuple_factory): |
Eric V. Smith | e7ba013 | 2018-01-06 12:41:53 -0500 | [diff] [blame] | 867 | if _is_dataclass_instance(obj): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 868 | result = [] |
| 869 | for f in fields(obj): |
| 870 | value = _astuple_inner(getattr(obj, f.name), tuple_factory) |
| 871 | result.append(value) |
| 872 | return tuple_factory(result) |
| 873 | elif isinstance(obj, (list, tuple)): |
| 874 | return type(obj)(_astuple_inner(v, tuple_factory) for v in obj) |
| 875 | elif isinstance(obj, dict): |
| 876 | return type(obj)((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory)) |
| 877 | for k, v in obj.items()) |
| 878 | else: |
| 879 | return deepcopy(obj) |
| 880 | |
| 881 | |
Eric V. Smith | d80b443 | 2018-01-06 17:09:58 -0500 | [diff] [blame] | 882 | def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 883 | repr=True, eq=True, order=False, unsafe_hash=None, |
| 884 | frozen=False): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 885 | """Return a new dynamically created dataclass. |
| 886 | |
Eric V. Smith | ed7d429 | 2018-01-06 16:14:03 -0500 | [diff] [blame] | 887 | The dataclass name will be 'cls_name'. 'fields' is an iterable |
| 888 | of either (name), (name, type) or (name, type, Field) objects. If type is |
| 889 | omitted, use the string 'typing.Any'. Field objects are created by |
Eric V. Smith | d327ae6 | 2018-01-07 08:19:45 -0500 | [diff] [blame] | 890 | the equivalent of calling 'field(name, type [, Field-info])'. |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 891 | |
Raymond Hettinger | d55209d | 2018-01-10 20:56:41 -0800 | [diff] [blame] | 892 | C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,)) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 893 | |
| 894 | is equivalent to: |
| 895 | |
| 896 | @dataclass |
| 897 | class C(Base): |
Raymond Hettinger | d55209d | 2018-01-10 20:56:41 -0800 | [diff] [blame] | 898 | x: 'typing.Any' |
| 899 | y: int |
| 900 | z: int = field(init=False) |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 901 | |
Raymond Hettinger | d55209d | 2018-01-10 20:56:41 -0800 | [diff] [blame] | 902 | For the bases and namespace parameters, see the builtin type() function. |
Eric V. Smith | d80b443 | 2018-01-06 17:09:58 -0500 | [diff] [blame] | 903 | |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 904 | The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to |
Eric V. Smith | d80b443 | 2018-01-06 17:09:58 -0500 | [diff] [blame] | 905 | dataclass(). |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 906 | """ |
| 907 | |
| 908 | if namespace is None: |
| 909 | namespace = {} |
| 910 | else: |
| 911 | # Copy namespace since we're going to mutate it. |
| 912 | namespace = namespace.copy() |
| 913 | |
Eric V. Smith | d138892 | 2018-01-07 14:30:17 -0500 | [diff] [blame] | 914 | anns = {} |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 915 | for item in fields: |
Eric V. Smith | ed7d429 | 2018-01-06 16:14:03 -0500 | [diff] [blame] | 916 | if isinstance(item, str): |
| 917 | name = item |
| 918 | tp = 'typing.Any' |
| 919 | elif len(item) == 2: |
| 920 | name, tp, = item |
| 921 | elif len(item) == 3: |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 922 | name, tp, spec = item |
| 923 | namespace[name] = spec |
Eric V. Smith | ed7d429 | 2018-01-06 16:14:03 -0500 | [diff] [blame] | 924 | anns[name] = tp |
| 925 | |
| 926 | namespace['__annotations__'] = anns |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 927 | cls = type(cls_name, bases, namespace) |
Eric V. Smith | d80b443 | 2018-01-06 17:09:58 -0500 | [diff] [blame] | 928 | return dataclass(cls, init=init, repr=repr, eq=eq, order=order, |
Eric V. Smith | dbf9cff | 2018-02-25 21:30:17 -0500 | [diff] [blame] | 929 | unsafe_hash=unsafe_hash, frozen=frozen) |
| 930 | |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 931 | |
| 932 | def replace(obj, **changes): |
| 933 | """Return a new object replacing specified fields with new values. |
| 934 | |
| 935 | This is especially useful for frozen classes. Example usage: |
| 936 | |
| 937 | @dataclass(frozen=True) |
| 938 | class C: |
| 939 | x: int |
| 940 | y: int |
| 941 | |
| 942 | c = C(1, 2) |
| 943 | c1 = replace(c, x=3) |
| 944 | assert c1.x == 3 and c1.y == 2 |
| 945 | """ |
| 946 | |
| 947 | # We're going to mutate 'changes', but that's okay because it's a new |
| 948 | # dict, even if called with 'replace(obj, **my_changes)'. |
| 949 | |
Eric V. Smith | e7ba013 | 2018-01-06 12:41:53 -0500 | [diff] [blame] | 950 | if not _is_dataclass_instance(obj): |
Eric V. Smith | f0db54a | 2017-12-04 16:58:55 -0500 | [diff] [blame] | 951 | raise TypeError("replace() should be called on dataclass instances") |
| 952 | |
| 953 | # It's an error to have init=False fields in 'changes'. |
| 954 | # If a field is not in 'changes', read its value from the provided obj. |
| 955 | |
| 956 | for f in getattr(obj, _MARKER).values(): |
| 957 | if not f.init: |
| 958 | # Error if this field is specified in changes. |
| 959 | if f.name in changes: |
| 960 | raise ValueError(f'field {f.name} is declared with ' |
| 961 | 'init=False, it cannot be specified with ' |
| 962 | 'replace()') |
| 963 | continue |
| 964 | |
| 965 | if f.name not in changes: |
| 966 | changes[f.name] = getattr(obj, f.name) |
| 967 | |
| 968 | # Create the new object, which calls __init__() and __post_init__ |
| 969 | # (if defined), using all of the init fields we've added and/or |
| 970 | # left in 'changes'. |
| 971 | # If there are values supplied in changes that aren't fields, this |
| 972 | # will correctly raise a TypeError. |
| 973 | return obj.__class__(**changes) |