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