dataclasses.py 61 KB

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