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