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