_make.py 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052
  1. from __future__ import absolute_import, division, print_function
  2. import copy
  3. import inspect
  4. import linecache
  5. import sys
  6. import threading
  7. import uuid
  8. import warnings
  9. from operator import itemgetter
  10. from . import _config, setters
  11. from ._compat import (
  12. PY2,
  13. PYPY,
  14. isclass,
  15. iteritems,
  16. metadata_proxy,
  17. new_class,
  18. ordered_dict,
  19. set_closure_cell,
  20. )
  21. from .exceptions import (
  22. DefaultAlreadySetError,
  23. FrozenInstanceError,
  24. NotAnAttrsClassError,
  25. PythonTooOldError,
  26. UnannotatedAttributeError,
  27. )
  28. if not PY2:
  29. import typing
  30. # This is used at least twice, so cache it here.
  31. _obj_setattr = object.__setattr__
  32. _init_converter_pat = "__attr_converter_%s"
  33. _init_factory_pat = "__attr_factory_{}"
  34. _tuple_property_pat = (
  35. " {attr_name} = _attrs_property(_attrs_itemgetter({index}))"
  36. )
  37. _classvar_prefixes = (
  38. "typing.ClassVar",
  39. "t.ClassVar",
  40. "ClassVar",
  41. "typing_extensions.ClassVar",
  42. )
  43. # we don't use a double-underscore prefix because that triggers
  44. # name mangling when trying to create a slot for the field
  45. # (when slots=True)
  46. _hash_cache_field = "_attrs_cached_hash"
  47. _empty_metadata_singleton = metadata_proxy({})
  48. # Unique object for unequivocal getattr() defaults.
  49. _sentinel = object()
  50. class _Nothing(object):
  51. """
  52. Sentinel class to indicate the lack of a value when ``None`` is ambiguous.
  53. ``_Nothing`` is a singleton. There is only ever one of it.
  54. .. versionchanged:: 21.1.0 ``bool(NOTHING)`` is now False.
  55. """
  56. _singleton = None
  57. def __new__(cls):
  58. if _Nothing._singleton is None:
  59. _Nothing._singleton = super(_Nothing, cls).__new__(cls)
  60. return _Nothing._singleton
  61. def __repr__(self):
  62. return "NOTHING"
  63. def __bool__(self):
  64. return False
  65. def __len__(self):
  66. return 0 # __bool__ for Python 2
  67. NOTHING = _Nothing()
  68. """
  69. Sentinel to indicate the lack of a value when ``None`` is ambiguous.
  70. """
  71. class _CacheHashWrapper(int):
  72. """
  73. An integer subclass that pickles / copies as None
  74. This is used for non-slots classes with ``cache_hash=True``, to avoid
  75. serializing a potentially (even likely) invalid hash value. Since ``None``
  76. is the default value for uncalculated hashes, whenever this is copied,
  77. the copy's value for the hash should automatically reset.
  78. See GH #613 for more details.
  79. """
  80. if PY2:
  81. # For some reason `type(None)` isn't callable in Python 2, but we don't
  82. # actually need a constructor for None objects, we just need any
  83. # available function that returns None.
  84. def __reduce__(self, _none_constructor=getattr, _args=(0, "", None)):
  85. return _none_constructor, _args
  86. else:
  87. def __reduce__(self, _none_constructor=type(None), _args=()):
  88. return _none_constructor, _args
  89. def attrib(
  90. default=NOTHING,
  91. validator=None,
  92. repr=True,
  93. cmp=None,
  94. hash=None,
  95. init=True,
  96. metadata=None,
  97. type=None,
  98. converter=None,
  99. factory=None,
  100. kw_only=False,
  101. eq=None,
  102. order=None,
  103. on_setattr=None,
  104. ):
  105. """
  106. Create a new attribute on a class.
  107. .. warning::
  108. Does *not* do anything unless the class is also decorated with
  109. `attr.s`!
  110. :param default: A value that is used if an ``attrs``-generated ``__init__``
  111. is used and no value is passed while instantiating or the attribute is
  112. excluded using ``init=False``.
  113. If the value is an instance of `Factory`, its callable will be
  114. used to construct a new value (useful for mutable data types like lists
  115. or dicts).
  116. If a default is not set (or set manually to `attr.NOTHING`), a value
  117. *must* be supplied when instantiating; otherwise a `TypeError`
  118. will be raised.
  119. The default can also be set using decorator notation as shown below.
  120. :type default: Any value
  121. :param callable factory: Syntactic sugar for
  122. ``default=attr.Factory(factory)``.
  123. :param validator: `callable` that is called by ``attrs``-generated
  124. ``__init__`` methods after the instance has been initialized. They
  125. receive the initialized instance, the `Attribute`, and the
  126. passed value.
  127. The return value is *not* inspected so the validator has to throw an
  128. exception itself.
  129. If a `list` is passed, its items are treated as validators and must
  130. all pass.
  131. Validators can be globally disabled and re-enabled using
  132. `get_run_validators`.
  133. The validator can also be set using decorator notation as shown below.
  134. :type validator: `callable` or a `list` of `callable`\\ s.
  135. :param repr: Include this attribute in the generated ``__repr__``
  136. method. If ``True``, include the attribute; if ``False``, omit it. By
  137. default, the built-in ``repr()`` function is used. To override how the
  138. attribute value is formatted, pass a ``callable`` that takes a single
  139. value and returns a string. Note that the resulting string is used
  140. as-is, i.e. it will be used directly *instead* of calling ``repr()``
  141. (the default).
  142. :type repr: a `bool` or a `callable` to use a custom function.
  143. :param eq: If ``True`` (default), include this attribute in the
  144. generated ``__eq__`` and ``__ne__`` methods that check two instances
  145. for equality. To override how the attribute value is compared,
  146. pass a ``callable`` that takes a single value and returns the value
  147. to be compared.
  148. :type eq: a `bool` or a `callable`.
  149. :param order: If ``True`` (default), include this attributes in the
  150. generated ``__lt__``, ``__le__``, ``__gt__`` and ``__ge__`` methods.
  151. To override how the attribute value is ordered,
  152. pass a ``callable`` that takes a single value and returns the value
  153. to be ordered.
  154. :type order: a `bool` or a `callable`.
  155. :param cmp: Setting *cmp* is equivalent to setting *eq* and *order* to the
  156. same value. Must not be mixed with *eq* or *order*.
  157. :type cmp: a `bool` or a `callable`.
  158. :param Optional[bool] hash: Include this attribute in the generated
  159. ``__hash__`` method. If ``None`` (default), mirror *eq*'s value. This
  160. is the correct behavior according the Python spec. Setting this value
  161. to anything else than ``None`` is *discouraged*.
  162. :param bool init: Include this attribute in the generated ``__init__``
  163. method. It is possible to set this to ``False`` and set a default
  164. value. In that case this attributed is unconditionally initialized
  165. with the specified default value or factory.
  166. :param callable converter: `callable` that is called by
  167. ``attrs``-generated ``__init__`` methods to convert attribute's value
  168. to the desired format. It is given the passed-in value, and the
  169. returned value will be used as the new value of the attribute. The
  170. value is converted before being passed to the validator, if any.
  171. :param metadata: An arbitrary mapping, to be used by third-party
  172. components. See `extending_metadata`.
  173. :param type: The type of the attribute. In Python 3.6 or greater, the
  174. preferred method to specify the type is using a variable annotation
  175. (see `PEP 526 <https://www.python.org/dev/peps/pep-0526/>`_).
  176. This argument is provided for backward compatibility.
  177. Regardless of the approach used, the type will be stored on
  178. ``Attribute.type``.
  179. Please note that ``attrs`` doesn't do anything with this metadata by
  180. itself. You can use it as part of your own code or for
  181. `static type checking <types>`.
  182. :param kw_only: Make this attribute keyword-only (Python 3+)
  183. in the generated ``__init__`` (if ``init`` is ``False``, this
  184. parameter is ignored).
  185. :param on_setattr: Allows to overwrite the *on_setattr* setting from
  186. `attr.s`. If left `None`, the *on_setattr* value from `attr.s` is used.
  187. Set to `attr.setters.NO_OP` to run **no** `setattr` hooks for this
  188. attribute -- regardless of the setting in `attr.s`.
  189. :type on_setattr: `callable`, or a list of callables, or `None`, or
  190. `attr.setters.NO_OP`
  191. .. versionadded:: 15.2.0 *convert*
  192. .. versionadded:: 16.3.0 *metadata*
  193. .. versionchanged:: 17.1.0 *validator* can be a ``list`` now.
  194. .. versionchanged:: 17.1.0
  195. *hash* is ``None`` and therefore mirrors *eq* by default.
  196. .. versionadded:: 17.3.0 *type*
  197. .. deprecated:: 17.4.0 *convert*
  198. .. versionadded:: 17.4.0 *converter* as a replacement for the deprecated
  199. *convert* to achieve consistency with other noun-based arguments.
  200. .. versionadded:: 18.1.0
  201. ``factory=f`` is syntactic sugar for ``default=attr.Factory(f)``.
  202. .. versionadded:: 18.2.0 *kw_only*
  203. .. versionchanged:: 19.2.0 *convert* keyword argument removed.
  204. .. versionchanged:: 19.2.0 *repr* also accepts a custom callable.
  205. .. deprecated:: 19.2.0 *cmp* Removal on or after 2021-06-01.
  206. .. versionadded:: 19.2.0 *eq* and *order*
  207. .. versionadded:: 20.1.0 *on_setattr*
  208. .. versionchanged:: 20.3.0 *kw_only* backported to Python 2
  209. .. versionchanged:: 21.1.0
  210. *eq*, *order*, and *cmp* also accept a custom callable
  211. .. versionchanged:: 21.1.0 *cmp* undeprecated
  212. """
  213. eq, eq_key, order, order_key = _determine_attrib_eq_order(
  214. cmp, eq, order, True
  215. )
  216. if hash is not None and hash is not True and hash is not False:
  217. raise TypeError(
  218. "Invalid value for hash. Must be True, False, or None."
  219. )
  220. if factory is not None:
  221. if default is not NOTHING:
  222. raise ValueError(
  223. "The `default` and `factory` arguments are mutually "
  224. "exclusive."
  225. )
  226. if not callable(factory):
  227. raise ValueError("The `factory` argument must be a callable.")
  228. default = Factory(factory)
  229. if metadata is None:
  230. metadata = {}
  231. # Apply syntactic sugar by auto-wrapping.
  232. if isinstance(on_setattr, (list, tuple)):
  233. on_setattr = setters.pipe(*on_setattr)
  234. if validator and isinstance(validator, (list, tuple)):
  235. validator = and_(*validator)
  236. if converter and isinstance(converter, (list, tuple)):
  237. converter = pipe(*converter)
  238. return _CountingAttr(
  239. default=default,
  240. validator=validator,
  241. repr=repr,
  242. cmp=None,
  243. hash=hash,
  244. init=init,
  245. converter=converter,
  246. metadata=metadata,
  247. type=type,
  248. kw_only=kw_only,
  249. eq=eq,
  250. eq_key=eq_key,
  251. order=order,
  252. order_key=order_key,
  253. on_setattr=on_setattr,
  254. )
  255. def _compile_and_eval(script, globs, locs=None, filename=""):
  256. """
  257. "Exec" the script with the given global (globs) and local (locs) variables.
  258. """
  259. bytecode = compile(script, filename, "exec")
  260. eval(bytecode, globs, locs)
  261. def _make_method(name, script, filename, globs=None):
  262. """
  263. Create the method with the script given and return the method object.
  264. """
  265. locs = {}
  266. if globs is None:
  267. globs = {}
  268. _compile_and_eval(script, globs, locs, filename)
  269. # In order of debuggers like PDB being able to step through the code,
  270. # we add a fake linecache entry.
  271. linecache.cache[filename] = (
  272. len(script),
  273. None,
  274. script.splitlines(True),
  275. filename,
  276. )
  277. return locs[name]
  278. def _make_attr_tuple_class(cls_name, attr_names):
  279. """
  280. Create a tuple subclass to hold `Attribute`s for an `attrs` class.
  281. The subclass is a bare tuple with properties for names.
  282. class MyClassAttributes(tuple):
  283. __slots__ = ()
  284. x = property(itemgetter(0))
  285. """
  286. attr_class_name = "{}Attributes".format(cls_name)
  287. attr_class_template = [
  288. "class {}(tuple):".format(attr_class_name),
  289. " __slots__ = ()",
  290. ]
  291. if attr_names:
  292. for i, attr_name in enumerate(attr_names):
  293. attr_class_template.append(
  294. _tuple_property_pat.format(index=i, attr_name=attr_name)
  295. )
  296. else:
  297. attr_class_template.append(" pass")
  298. globs = {"_attrs_itemgetter": itemgetter, "_attrs_property": property}
  299. _compile_and_eval("\n".join(attr_class_template), globs)
  300. return globs[attr_class_name]
  301. # Tuple class for extracted attributes from a class definition.
  302. # `base_attrs` is a subset of `attrs`.
  303. _Attributes = _make_attr_tuple_class(
  304. "_Attributes",
  305. [
  306. # all attributes to build dunder methods for
  307. "attrs",
  308. # attributes that have been inherited
  309. "base_attrs",
  310. # map inherited attributes to their originating classes
  311. "base_attrs_map",
  312. ],
  313. )
  314. def _is_class_var(annot):
  315. """
  316. Check whether *annot* is a typing.ClassVar.
  317. The string comparison hack is used to avoid evaluating all string
  318. annotations which would put attrs-based classes at a performance
  319. disadvantage compared to plain old classes.
  320. """
  321. annot = str(annot)
  322. # Annotation can be quoted.
  323. if annot.startswith(("'", '"')) and annot.endswith(("'", '"')):
  324. annot = annot[1:-1]
  325. return annot.startswith(_classvar_prefixes)
  326. def _has_own_attribute(cls, attrib_name):
  327. """
  328. Check whether *cls* defines *attrib_name* (and doesn't just inherit it).
  329. Requires Python 3.
  330. """
  331. attr = getattr(cls, attrib_name, _sentinel)
  332. if attr is _sentinel:
  333. return False
  334. for base_cls in cls.__mro__[1:]:
  335. a = getattr(base_cls, attrib_name, None)
  336. if attr is a:
  337. return False
  338. return True
  339. def _get_annotations(cls):
  340. """
  341. Get annotations for *cls*.
  342. """
  343. if _has_own_attribute(cls, "__annotations__"):
  344. return cls.__annotations__
  345. return {}
  346. def _counter_getter(e):
  347. """
  348. Key function for sorting to avoid re-creating a lambda for every class.
  349. """
  350. return e[1].counter
  351. def _collect_base_attrs(cls, taken_attr_names):
  352. """
  353. Collect attr.ibs from base classes of *cls*, except *taken_attr_names*.
  354. """
  355. base_attrs = []
  356. base_attr_map = {} # A dictionary of base attrs to their classes.
  357. # Traverse the MRO and collect attributes.
  358. for base_cls in reversed(cls.__mro__[1:-1]):
  359. for a in getattr(base_cls, "__attrs_attrs__", []):
  360. if a.inherited or a.name in taken_attr_names:
  361. continue
  362. a = a.evolve(inherited=True)
  363. base_attrs.append(a)
  364. base_attr_map[a.name] = base_cls
  365. # For each name, only keep the freshest definition i.e. the furthest at the
  366. # back. base_attr_map is fine because it gets overwritten with every new
  367. # instance.
  368. filtered = []
  369. seen = set()
  370. for a in reversed(base_attrs):
  371. if a.name in seen:
  372. continue
  373. filtered.insert(0, a)
  374. seen.add(a.name)
  375. return filtered, base_attr_map
  376. def _collect_base_attrs_broken(cls, taken_attr_names):
  377. """
  378. Collect attr.ibs from base classes of *cls*, except *taken_attr_names*.
  379. N.B. *taken_attr_names* will be mutated.
  380. Adhere to the old incorrect behavior.
  381. Notably it collects from the front and considers inherited attributes which
  382. leads to the buggy behavior reported in #428.
  383. """
  384. base_attrs = []
  385. base_attr_map = {} # A dictionary of base attrs to their classes.
  386. # Traverse the MRO and collect attributes.
  387. for base_cls in cls.__mro__[1:-1]:
  388. for a in getattr(base_cls, "__attrs_attrs__", []):
  389. if a.name in taken_attr_names:
  390. continue
  391. a = a.evolve(inherited=True)
  392. taken_attr_names.add(a.name)
  393. base_attrs.append(a)
  394. base_attr_map[a.name] = base_cls
  395. return base_attrs, base_attr_map
  396. def _transform_attrs(
  397. cls, these, auto_attribs, kw_only, collect_by_mro, field_transformer
  398. ):
  399. """
  400. Transform all `_CountingAttr`s on a class into `Attribute`s.
  401. If *these* is passed, use that and don't look for them on the class.
  402. *collect_by_mro* is True, collect them in the correct MRO order, otherwise
  403. use the old -- incorrect -- order. See #428.
  404. Return an `_Attributes`.
  405. """
  406. cd = cls.__dict__
  407. anns = _get_annotations(cls)
  408. if these is not None:
  409. ca_list = [(name, ca) for name, ca in iteritems(these)]
  410. if not isinstance(these, ordered_dict):
  411. ca_list.sort(key=_counter_getter)
  412. elif auto_attribs is True:
  413. ca_names = {
  414. name
  415. for name, attr in cd.items()
  416. if isinstance(attr, _CountingAttr)
  417. }
  418. ca_list = []
  419. annot_names = set()
  420. for attr_name, type in anns.items():
  421. if _is_class_var(type):
  422. continue
  423. annot_names.add(attr_name)
  424. a = cd.get(attr_name, NOTHING)
  425. if not isinstance(a, _CountingAttr):
  426. if a is NOTHING:
  427. a = attrib()
  428. else:
  429. a = attrib(default=a)
  430. ca_list.append((attr_name, a))
  431. unannotated = ca_names - annot_names
  432. if len(unannotated) > 0:
  433. raise UnannotatedAttributeError(
  434. "The following `attr.ib`s lack a type annotation: "
  435. + ", ".join(
  436. sorted(unannotated, key=lambda n: cd.get(n).counter)
  437. )
  438. + "."
  439. )
  440. else:
  441. ca_list = sorted(
  442. (
  443. (name, attr)
  444. for name, attr in cd.items()
  445. if isinstance(attr, _CountingAttr)
  446. ),
  447. key=lambda e: e[1].counter,
  448. )
  449. own_attrs = [
  450. Attribute.from_counting_attr(
  451. name=attr_name, ca=ca, type=anns.get(attr_name)
  452. )
  453. for attr_name, ca in ca_list
  454. ]
  455. if collect_by_mro:
  456. base_attrs, base_attr_map = _collect_base_attrs(
  457. cls, {a.name for a in own_attrs}
  458. )
  459. else:
  460. base_attrs, base_attr_map = _collect_base_attrs_broken(
  461. cls, {a.name for a in own_attrs}
  462. )
  463. attr_names = [a.name for a in base_attrs + own_attrs]
  464. AttrsClass = _make_attr_tuple_class(cls.__name__, attr_names)
  465. if kw_only:
  466. own_attrs = [a.evolve(kw_only=True) for a in own_attrs]
  467. base_attrs = [a.evolve(kw_only=True) for a in base_attrs]
  468. attrs = AttrsClass(base_attrs + own_attrs)
  469. # Mandatory vs non-mandatory attr order only matters when they are part of
  470. # the __init__ signature and when they aren't kw_only (which are moved to
  471. # the end and can be mandatory or non-mandatory in any order, as they will
  472. # be specified as keyword args anyway). Check the order of those attrs:
  473. had_default = False
  474. for a in (a for a in attrs if a.init is not False and a.kw_only is False):
  475. if had_default is True and a.default is NOTHING:
  476. raise ValueError(
  477. "No mandatory attributes allowed after an attribute with a "
  478. "default value or factory. Attribute in question: %r" % (a,)
  479. )
  480. if had_default is False and a.default is not NOTHING:
  481. had_default = True
  482. if field_transformer is not None:
  483. attrs = field_transformer(cls, attrs)
  484. return _Attributes((attrs, base_attrs, base_attr_map))
  485. if PYPY:
  486. def _frozen_setattrs(self, name, value):
  487. """
  488. Attached to frozen classes as __setattr__.
  489. """
  490. if isinstance(self, BaseException) and name in (
  491. "__cause__",
  492. "__context__",
  493. ):
  494. BaseException.__setattr__(self, name, value)
  495. return
  496. raise FrozenInstanceError()
  497. else:
  498. def _frozen_setattrs(self, name, value):
  499. """
  500. Attached to frozen classes as __setattr__.
  501. """
  502. raise FrozenInstanceError()
  503. def _frozen_delattrs(self, name):
  504. """
  505. Attached to frozen classes as __delattr__.
  506. """
  507. raise FrozenInstanceError()
  508. class _ClassBuilder(object):
  509. """
  510. Iteratively build *one* class.
  511. """
  512. __slots__ = (
  513. "_attr_names",
  514. "_attrs",
  515. "_base_attr_map",
  516. "_base_names",
  517. "_cache_hash",
  518. "_cls",
  519. "_cls_dict",
  520. "_delete_attribs",
  521. "_frozen",
  522. "_has_pre_init",
  523. "_has_post_init",
  524. "_is_exc",
  525. "_on_setattr",
  526. "_slots",
  527. "_weakref_slot",
  528. "_has_own_setattr",
  529. "_has_custom_setattr",
  530. )
  531. def __init__(
  532. self,
  533. cls,
  534. these,
  535. slots,
  536. frozen,
  537. weakref_slot,
  538. getstate_setstate,
  539. auto_attribs,
  540. kw_only,
  541. cache_hash,
  542. is_exc,
  543. collect_by_mro,
  544. on_setattr,
  545. has_custom_setattr,
  546. field_transformer,
  547. ):
  548. attrs, base_attrs, base_map = _transform_attrs(
  549. cls,
  550. these,
  551. auto_attribs,
  552. kw_only,
  553. collect_by_mro,
  554. field_transformer,
  555. )
  556. self._cls = cls
  557. self._cls_dict = dict(cls.__dict__) if slots else {}
  558. self._attrs = attrs
  559. self._base_names = set(a.name for a in base_attrs)
  560. self._base_attr_map = base_map
  561. self._attr_names = tuple(a.name for a in attrs)
  562. self._slots = slots
  563. self._frozen = frozen
  564. self._weakref_slot = weakref_slot
  565. self._cache_hash = cache_hash
  566. self._has_pre_init = bool(getattr(cls, "__attrs_pre_init__", False))
  567. self._has_post_init = bool(getattr(cls, "__attrs_post_init__", False))
  568. self._delete_attribs = not bool(these)
  569. self._is_exc = is_exc
  570. self._on_setattr = on_setattr
  571. self._has_custom_setattr = has_custom_setattr
  572. self._has_own_setattr = False
  573. self._cls_dict["__attrs_attrs__"] = self._attrs
  574. if frozen:
  575. self._cls_dict["__setattr__"] = _frozen_setattrs
  576. self._cls_dict["__delattr__"] = _frozen_delattrs
  577. self._has_own_setattr = True
  578. if getstate_setstate:
  579. (
  580. self._cls_dict["__getstate__"],
  581. self._cls_dict["__setstate__"],
  582. ) = self._make_getstate_setstate()
  583. def __repr__(self):
  584. return "<_ClassBuilder(cls={cls})>".format(cls=self._cls.__name__)
  585. def build_class(self):
  586. """
  587. Finalize class based on the accumulated configuration.
  588. Builder cannot be used after calling this method.
  589. """
  590. if self._slots is True:
  591. return self._create_slots_class()
  592. else:
  593. return self._patch_original_class()
  594. def _patch_original_class(self):
  595. """
  596. Apply accumulated methods and return the class.
  597. """
  598. cls = self._cls
  599. base_names = self._base_names
  600. # Clean class of attribute definitions (`attr.ib()`s).
  601. if self._delete_attribs:
  602. for name in self._attr_names:
  603. if (
  604. name not in base_names
  605. and getattr(cls, name, _sentinel) is not _sentinel
  606. ):
  607. try:
  608. delattr(cls, name)
  609. except AttributeError:
  610. # This can happen if a base class defines a class
  611. # variable and we want to set an attribute with the
  612. # same name by using only a type annotation.
  613. pass
  614. # Attach our dunder methods.
  615. for name, value in self._cls_dict.items():
  616. setattr(cls, name, value)
  617. # If we've inherited an attrs __setattr__ and don't write our own,
  618. # reset it to object's.
  619. if not self._has_own_setattr and getattr(
  620. cls, "__attrs_own_setattr__", False
  621. ):
  622. cls.__attrs_own_setattr__ = False
  623. if not self._has_custom_setattr:
  624. cls.__setattr__ = object.__setattr__
  625. return cls
  626. def _create_slots_class(self):
  627. """
  628. Build and return a new class with a `__slots__` attribute.
  629. """
  630. cd = {
  631. k: v
  632. for k, v in iteritems(self._cls_dict)
  633. if k not in tuple(self._attr_names) + ("__dict__", "__weakref__")
  634. }
  635. # If our class doesn't have its own implementation of __setattr__
  636. # (either from the user or by us), check the bases, if one of them has
  637. # an attrs-made __setattr__, that needs to be reset. We don't walk the
  638. # MRO because we only care about our immediate base classes.
  639. # XXX: This can be confused by subclassing a slotted attrs class with
  640. # XXX: a non-attrs class and subclass the resulting class with an attrs
  641. # XXX: class. See `test_slotted_confused` for details. For now that's
  642. # XXX: OK with us.
  643. if not self._has_own_setattr:
  644. cd["__attrs_own_setattr__"] = False
  645. if not self._has_custom_setattr:
  646. for base_cls in self._cls.__bases__:
  647. if base_cls.__dict__.get("__attrs_own_setattr__", False):
  648. cd["__setattr__"] = object.__setattr__
  649. break
  650. # Traverse the MRO to collect existing slots
  651. # and check for an existing __weakref__.
  652. existing_slots = dict()
  653. weakref_inherited = False
  654. for base_cls in self._cls.__mro__[1:-1]:
  655. if base_cls.__dict__.get("__weakref__", None) is not None:
  656. weakref_inherited = True
  657. existing_slots.update(
  658. {
  659. name: getattr(base_cls, name)
  660. for name in getattr(base_cls, "__slots__", [])
  661. }
  662. )
  663. base_names = set(self._base_names)
  664. names = self._attr_names
  665. if (
  666. self._weakref_slot
  667. and "__weakref__" not in getattr(self._cls, "__slots__", ())
  668. and "__weakref__" not in names
  669. and not weakref_inherited
  670. ):
  671. names += ("__weakref__",)
  672. # We only add the names of attributes that aren't inherited.
  673. # Setting __slots__ to inherited attributes wastes memory.
  674. slot_names = [name for name in names if name not in base_names]
  675. # There are slots for attributes from current class
  676. # that are defined in parent classes.
  677. # As their descriptors may be overriden by a child class,
  678. # we collect them here and update the class dict
  679. reused_slots = {
  680. slot: slot_descriptor
  681. for slot, slot_descriptor in iteritems(existing_slots)
  682. if slot in slot_names
  683. }
  684. slot_names = [name for name in slot_names if name not in reused_slots]
  685. cd.update(reused_slots)
  686. if self._cache_hash:
  687. slot_names.append(_hash_cache_field)
  688. cd["__slots__"] = tuple(slot_names)
  689. qualname = getattr(self._cls, "__qualname__", None)
  690. if qualname is not None:
  691. cd["__qualname__"] = qualname
  692. # Create new class based on old class and our methods.
  693. cls = type(self._cls)(self._cls.__name__, self._cls.__bases__, cd)
  694. # The following is a fix for
  695. # https://github.com/python-attrs/attrs/issues/102. On Python 3,
  696. # if a method mentions `__class__` or uses the no-arg super(), the
  697. # compiler will bake a reference to the class in the method itself
  698. # as `method.__closure__`. Since we replace the class with a
  699. # clone, we rewrite these references so it keeps working.
  700. for item in cls.__dict__.values():
  701. if isinstance(item, (classmethod, staticmethod)):
  702. # Class- and staticmethods hide their functions inside.
  703. # These might need to be rewritten as well.
  704. closure_cells = getattr(item.__func__, "__closure__", None)
  705. elif isinstance(item, property):
  706. # Workaround for property `super()` shortcut (PY3-only).
  707. # There is no universal way for other descriptors.
  708. closure_cells = getattr(item.fget, "__closure__", None)
  709. else:
  710. closure_cells = getattr(item, "__closure__", None)
  711. if not closure_cells: # Catch None or the empty list.
  712. continue
  713. for cell in closure_cells:
  714. try:
  715. match = cell.cell_contents is self._cls
  716. except ValueError: # ValueError: Cell is empty
  717. pass
  718. else:
  719. if match:
  720. set_closure_cell(cell, cls)
  721. return cls
  722. def add_repr(self, ns):
  723. self._cls_dict["__repr__"] = self._add_method_dunders(
  724. _make_repr(self._attrs, ns=ns)
  725. )
  726. return self
  727. def add_str(self):
  728. repr = self._cls_dict.get("__repr__")
  729. if repr is None:
  730. raise ValueError(
  731. "__str__ can only be generated if a __repr__ exists."
  732. )
  733. def __str__(self):
  734. return self.__repr__()
  735. self._cls_dict["__str__"] = self._add_method_dunders(__str__)
  736. return self
  737. def _make_getstate_setstate(self):
  738. """
  739. Create custom __setstate__ and __getstate__ methods.
  740. """
  741. # __weakref__ is not writable.
  742. state_attr_names = tuple(
  743. an for an in self._attr_names if an != "__weakref__"
  744. )
  745. def slots_getstate(self):
  746. """
  747. Automatically created by attrs.
  748. """
  749. return tuple(getattr(self, name) for name in state_attr_names)
  750. hash_caching_enabled = self._cache_hash
  751. def slots_setstate(self, state):
  752. """
  753. Automatically created by attrs.
  754. """
  755. __bound_setattr = _obj_setattr.__get__(self, Attribute)
  756. for name, value in zip(state_attr_names, state):
  757. __bound_setattr(name, value)
  758. # The hash code cache is not included when the object is
  759. # serialized, but it still needs to be initialized to None to
  760. # indicate that the first call to __hash__ should be a cache
  761. # miss.
  762. if hash_caching_enabled:
  763. __bound_setattr(_hash_cache_field, None)
  764. return slots_getstate, slots_setstate
  765. def make_unhashable(self):
  766. self._cls_dict["__hash__"] = None
  767. return self
  768. def add_hash(self):
  769. self._cls_dict["__hash__"] = self._add_method_dunders(
  770. _make_hash(
  771. self._cls,
  772. self._attrs,
  773. frozen=self._frozen,
  774. cache_hash=self._cache_hash,
  775. )
  776. )
  777. return self
  778. def add_init(self):
  779. self._cls_dict["__init__"] = self._add_method_dunders(
  780. _make_init(
  781. self._cls,
  782. self._attrs,
  783. self._has_pre_init,
  784. self._has_post_init,
  785. self._frozen,
  786. self._slots,
  787. self._cache_hash,
  788. self._base_attr_map,
  789. self._is_exc,
  790. self._on_setattr is not None
  791. and self._on_setattr is not setters.NO_OP,
  792. attrs_init=False,
  793. )
  794. )
  795. return self
  796. def add_attrs_init(self):
  797. self._cls_dict["__attrs_init__"] = self._add_method_dunders(
  798. _make_init(
  799. self._cls,
  800. self._attrs,
  801. self._has_pre_init,
  802. self._has_post_init,
  803. self._frozen,
  804. self._slots,
  805. self._cache_hash,
  806. self._base_attr_map,
  807. self._is_exc,
  808. self._on_setattr is not None
  809. and self._on_setattr is not setters.NO_OP,
  810. attrs_init=True,
  811. )
  812. )
  813. return self
  814. def add_eq(self):
  815. cd = self._cls_dict
  816. cd["__eq__"] = self._add_method_dunders(
  817. _make_eq(self._cls, self._attrs)
  818. )
  819. cd["__ne__"] = self._add_method_dunders(_make_ne())
  820. return self
  821. def add_order(self):
  822. cd = self._cls_dict
  823. cd["__lt__"], cd["__le__"], cd["__gt__"], cd["__ge__"] = (
  824. self._add_method_dunders(meth)
  825. for meth in _make_order(self._cls, self._attrs)
  826. )
  827. return self
  828. def add_setattr(self):
  829. if self._frozen:
  830. return self
  831. sa_attrs = {}
  832. for a in self._attrs:
  833. on_setattr = a.on_setattr or self._on_setattr
  834. if on_setattr and on_setattr is not setters.NO_OP:
  835. sa_attrs[a.name] = a, on_setattr
  836. if not sa_attrs:
  837. return self
  838. if self._has_custom_setattr:
  839. # We need to write a __setattr__ but there already is one!
  840. raise ValueError(
  841. "Can't combine custom __setattr__ with on_setattr hooks."
  842. )
  843. # docstring comes from _add_method_dunders
  844. def __setattr__(self, name, val):
  845. try:
  846. a, hook = sa_attrs[name]
  847. except KeyError:
  848. nval = val
  849. else:
  850. nval = hook(self, a, val)
  851. _obj_setattr(self, name, nval)
  852. self._cls_dict["__attrs_own_setattr__"] = True
  853. self._cls_dict["__setattr__"] = self._add_method_dunders(__setattr__)
  854. self._has_own_setattr = True
  855. return self
  856. def _add_method_dunders(self, method):
  857. """
  858. Add __module__ and __qualname__ to a *method* if possible.
  859. """
  860. try:
  861. method.__module__ = self._cls.__module__
  862. except AttributeError:
  863. pass
  864. try:
  865. method.__qualname__ = ".".join(
  866. (self._cls.__qualname__, method.__name__)
  867. )
  868. except AttributeError:
  869. pass
  870. try:
  871. method.__doc__ = "Method generated by attrs for class %s." % (
  872. self._cls.__qualname__,
  873. )
  874. except AttributeError:
  875. pass
  876. return method
  877. _CMP_DEPRECATION = (
  878. "The usage of `cmp` is deprecated and will be removed on or after "
  879. "2021-06-01. Please use `eq` and `order` instead."
  880. )
  881. def _determine_attrs_eq_order(cmp, eq, order, default_eq):
  882. """
  883. Validate the combination of *cmp*, *eq*, and *order*. Derive the effective
  884. values of eq and order. If *eq* is None, set it to *default_eq*.
  885. """
  886. if cmp is not None and any((eq is not None, order is not None)):
  887. raise ValueError("Don't mix `cmp` with `eq' and `order`.")
  888. # cmp takes precedence due to bw-compatibility.
  889. if cmp is not None:
  890. return cmp, cmp
  891. # If left None, equality is set to the specified default and ordering
  892. # mirrors equality.
  893. if eq is None:
  894. eq = default_eq
  895. if order is None:
  896. order = eq
  897. if eq is False and order is True:
  898. raise ValueError("`order` can only be True if `eq` is True too.")
  899. return eq, order
  900. def _determine_attrib_eq_order(cmp, eq, order, default_eq):
  901. """
  902. Validate the combination of *cmp*, *eq*, and *order*. Derive the effective
  903. values of eq and order. If *eq* is None, set it to *default_eq*.
  904. """
  905. if cmp is not None and any((eq is not None, order is not None)):
  906. raise ValueError("Don't mix `cmp` with `eq' and `order`.")
  907. def decide_callable_or_boolean(value):
  908. """
  909. Decide whether a key function is used.
  910. """
  911. if callable(value):
  912. value, key = True, value
  913. else:
  914. key = None
  915. return value, key
  916. # cmp takes precedence due to bw-compatibility.
  917. if cmp is not None:
  918. cmp, cmp_key = decide_callable_or_boolean(cmp)
  919. return cmp, cmp_key, cmp, cmp_key
  920. # If left None, equality is set to the specified default and ordering
  921. # mirrors equality.
  922. if eq is None:
  923. eq, eq_key = default_eq, None
  924. else:
  925. eq, eq_key = decide_callable_or_boolean(eq)
  926. if order is None:
  927. order, order_key = eq, eq_key
  928. else:
  929. order, order_key = decide_callable_or_boolean(order)
  930. if eq is False and order is True:
  931. raise ValueError("`order` can only be True if `eq` is True too.")
  932. return eq, eq_key, order, order_key
  933. def _determine_whether_to_implement(
  934. cls, flag, auto_detect, dunders, default=True
  935. ):
  936. """
  937. Check whether we should implement a set of methods for *cls*.
  938. *flag* is the argument passed into @attr.s like 'init', *auto_detect* the
  939. same as passed into @attr.s and *dunders* is a tuple of attribute names
  940. whose presence signal that the user has implemented it themselves.
  941. Return *default* if no reason for either for or against is found.
  942. auto_detect must be False on Python 2.
  943. """
  944. if flag is True or flag is False:
  945. return flag
  946. if flag is None and auto_detect is False:
  947. return default
  948. # Logically, flag is None and auto_detect is True here.
  949. for dunder in dunders:
  950. if _has_own_attribute(cls, dunder):
  951. return False
  952. return default
  953. def attrs(
  954. maybe_cls=None,
  955. these=None,
  956. repr_ns=None,
  957. repr=None,
  958. cmp=None,
  959. hash=None,
  960. init=None,
  961. slots=False,
  962. frozen=False,
  963. weakref_slot=True,
  964. str=False,
  965. auto_attribs=False,
  966. kw_only=False,
  967. cache_hash=False,
  968. auto_exc=False,
  969. eq=None,
  970. order=None,
  971. auto_detect=False,
  972. collect_by_mro=False,
  973. getstate_setstate=None,
  974. on_setattr=None,
  975. field_transformer=None,
  976. ):
  977. r"""
  978. A class decorator that adds `dunder
  979. <https://wiki.python.org/moin/DunderAlias>`_\ -methods according to the
  980. specified attributes using `attr.ib` or the *these* argument.
  981. :param these: A dictionary of name to `attr.ib` mappings. This is
  982. useful to avoid the definition of your attributes within the class body
  983. because you can't (e.g. if you want to add ``__repr__`` methods to
  984. Django models) or don't want to.
  985. If *these* is not ``None``, ``attrs`` will *not* search the class body
  986. for attributes and will *not* remove any attributes from it.
  987. If *these* is an ordered dict (`dict` on Python 3.6+,
  988. `collections.OrderedDict` otherwise), the order is deduced from
  989. the order of the attributes inside *these*. Otherwise the order
  990. of the definition of the attributes is used.
  991. :type these: `dict` of `str` to `attr.ib`
  992. :param str repr_ns: When using nested classes, there's no way in Python 2
  993. to automatically detect that. Therefore it's possible to set the
  994. namespace explicitly for a more meaningful ``repr`` output.
  995. :param bool auto_detect: Instead of setting the *init*, *repr*, *eq*,
  996. *order*, and *hash* arguments explicitly, assume they are set to
  997. ``True`` **unless any** of the involved methods for one of the
  998. arguments is implemented in the *current* class (i.e. it is *not*
  999. inherited from some base class).
  1000. So for example by implementing ``__eq__`` on a class yourself,
  1001. ``attrs`` will deduce ``eq=False`` and will create *neither*
  1002. ``__eq__`` *nor* ``__ne__`` (but Python classes come with a sensible
  1003. ``__ne__`` by default, so it *should* be enough to only implement
  1004. ``__eq__`` in most cases).
  1005. .. warning::
  1006. If you prevent ``attrs`` from creating the ordering methods for you
  1007. (``order=False``, e.g. by implementing ``__le__``), it becomes
  1008. *your* responsibility to make sure its ordering is sound. The best
  1009. way is to use the `functools.total_ordering` decorator.
  1010. Passing ``True`` or ``False`` to *init*, *repr*, *eq*, *order*,
  1011. *cmp*, or *hash* overrides whatever *auto_detect* would determine.
  1012. *auto_detect* requires Python 3. Setting it ``True`` on Python 2 raises
  1013. a `PythonTooOldError`.
  1014. :param bool repr: Create a ``__repr__`` method with a human readable
  1015. representation of ``attrs`` attributes..
  1016. :param bool str: Create a ``__str__`` method that is identical to
  1017. ``__repr__``. This is usually not necessary except for
  1018. `Exception`\ s.
  1019. :param Optional[bool] eq: If ``True`` or ``None`` (default), add ``__eq__``
  1020. and ``__ne__`` methods that check two instances for equality.
  1021. They compare the instances as if they were tuples of their ``attrs``
  1022. attributes if and only if the types of both classes are *identical*!
  1023. :param Optional[bool] order: If ``True``, add ``__lt__``, ``__le__``,
  1024. ``__gt__``, and ``__ge__`` methods that behave like *eq* above and
  1025. allow instances to be ordered. If ``None`` (default) mirror value of
  1026. *eq*.
  1027. :param Optional[bool] cmp: Setting *cmp* is equivalent to setting *eq*
  1028. and *order* to the same value. Must not be mixed with *eq* or *order*.
  1029. :param Optional[bool] hash: If ``None`` (default), the ``__hash__`` method
  1030. is generated according how *eq* and *frozen* are set.
  1031. 1. If *both* are True, ``attrs`` will generate a ``__hash__`` for you.
  1032. 2. If *eq* is True and *frozen* is False, ``__hash__`` will be set to
  1033. None, marking it unhashable (which it is).
  1034. 3. If *eq* is False, ``__hash__`` will be left untouched meaning the
  1035. ``__hash__`` method of the base class will be used (if base class is
  1036. ``object``, this means it will fall back to id-based hashing.).
  1037. Although not recommended, you can decide for yourself and force
  1038. ``attrs`` to create one (e.g. if the class is immutable even though you
  1039. didn't freeze it programmatically) by passing ``True`` or not. Both of
  1040. these cases are rather special and should be used carefully.
  1041. See our documentation on `hashing`, Python's documentation on
  1042. `object.__hash__`, and the `GitHub issue that led to the default \
  1043. behavior <https://github.com/python-attrs/attrs/issues/136>`_ for more
  1044. details.
  1045. :param bool init: Create a ``__init__`` method that initializes the
  1046. ``attrs`` attributes. Leading underscores are stripped for the argument
  1047. name. If a ``__attrs_pre_init__`` method exists on the class, it will
  1048. be called before the class is initialized. If a ``__attrs_post_init__``
  1049. method exists on the class, it will be called after the class is fully
  1050. initialized.
  1051. If ``init`` is ``False``, an ``__attrs_init__`` method will be
  1052. injected instead. This allows you to define a custom ``__init__``
  1053. method that can do pre-init work such as ``super().__init__()``,
  1054. and then call ``__attrs_init__()`` and ``__attrs_post_init__()``.
  1055. :param bool slots: Create a `slotted class <slotted classes>` that's more
  1056. memory-efficient. Slotted classes are generally superior to the default
  1057. dict classes, but have some gotchas you should know about, so we
  1058. encourage you to read the `glossary entry <slotted classes>`.
  1059. :param bool frozen: Make instances immutable after initialization. If
  1060. someone attempts to modify a frozen instance,
  1061. `attr.exceptions.FrozenInstanceError` is raised.
  1062. .. note::
  1063. 1. This is achieved by installing a custom ``__setattr__`` method
  1064. on your class, so you can't implement your own.
  1065. 2. True immutability is impossible in Python.
  1066. 3. This *does* have a minor a runtime performance `impact
  1067. <how-frozen>` when initializing new instances. In other words:
  1068. ``__init__`` is slightly slower with ``frozen=True``.
  1069. 4. If a class is frozen, you cannot modify ``self`` in
  1070. ``__attrs_post_init__`` or a self-written ``__init__``. You can
  1071. circumvent that limitation by using
  1072. ``object.__setattr__(self, "attribute_name", value)``.
  1073. 5. Subclasses of a frozen class are frozen too.
  1074. :param bool weakref_slot: Make instances weak-referenceable. This has no
  1075. effect unless ``slots`` is also enabled.
  1076. :param bool auto_attribs: If ``True``, collect `PEP 526`_-annotated
  1077. attributes (Python 3.6 and later only) from the class body.
  1078. In this case, you **must** annotate every field. If ``attrs``
  1079. encounters a field that is set to an `attr.ib` but lacks a type
  1080. annotation, an `attr.exceptions.UnannotatedAttributeError` is
  1081. raised. Use ``field_name: typing.Any = attr.ib(...)`` if you don't
  1082. want to set a type.
  1083. If you assign a value to those attributes (e.g. ``x: int = 42``), that
  1084. value becomes the default value like if it were passed using
  1085. ``attr.ib(default=42)``. Passing an instance of `Factory` also
  1086. works as expected in most cases (see warning below).
  1087. Attributes annotated as `typing.ClassVar`, and attributes that are
  1088. neither annotated nor set to an `attr.ib` are **ignored**.
  1089. .. warning::
  1090. For features that use the attribute name to create decorators (e.g.
  1091. `validators <validators>`), you still *must* assign `attr.ib` to
  1092. them. Otherwise Python will either not find the name or try to use
  1093. the default value to call e.g. ``validator`` on it.
  1094. These errors can be quite confusing and probably the most common bug
  1095. report on our bug tracker.
  1096. .. _`PEP 526`: https://www.python.org/dev/peps/pep-0526/
  1097. :param bool kw_only: Make all attributes keyword-only (Python 3+)
  1098. in the generated ``__init__`` (if ``init`` is ``False``, this
  1099. parameter is ignored).
  1100. :param bool cache_hash: Ensure that the object's hash code is computed
  1101. only once and stored on the object. If this is set to ``True``,
  1102. hashing must be either explicitly or implicitly enabled for this
  1103. class. If the hash code is cached, avoid any reassignments of
  1104. fields involved in hash code computation or mutations of the objects
  1105. those fields point to after object creation. If such changes occur,
  1106. the behavior of the object's hash code is undefined.
  1107. :param bool auto_exc: If the class subclasses `BaseException`
  1108. (which implicitly includes any subclass of any exception), the
  1109. following happens to behave like a well-behaved Python exceptions
  1110. class:
  1111. - the values for *eq*, *order*, and *hash* are ignored and the
  1112. instances compare and hash by the instance's ids (N.B. ``attrs`` will
  1113. *not* remove existing implementations of ``__hash__`` or the equality
  1114. methods. It just won't add own ones.),
  1115. - all attributes that are either passed into ``__init__`` or have a
  1116. default value are additionally available as a tuple in the ``args``
  1117. attribute,
  1118. - the value of *str* is ignored leaving ``__str__`` to base classes.
  1119. :param bool collect_by_mro: Setting this to `True` fixes the way ``attrs``
  1120. collects attributes from base classes. The default behavior is
  1121. incorrect in certain cases of multiple inheritance. It should be on by
  1122. default but is kept off for backward-compatability.
  1123. See issue `#428 <https://github.com/python-attrs/attrs/issues/428>`_ for
  1124. more details.
  1125. :param Optional[bool] getstate_setstate:
  1126. .. note::
  1127. This is usually only interesting for slotted classes and you should
  1128. probably just set *auto_detect* to `True`.
  1129. If `True`, ``__getstate__`` and
  1130. ``__setstate__`` are generated and attached to the class. This is
  1131. necessary for slotted classes to be pickleable. If left `None`, it's
  1132. `True` by default for slotted classes and ``False`` for dict classes.
  1133. If *auto_detect* is `True`, and *getstate_setstate* is left `None`,
  1134. and **either** ``__getstate__`` or ``__setstate__`` is detected directly
  1135. on the class (i.e. not inherited), it is set to `False` (this is usually
  1136. what you want).
  1137. :param on_setattr: A callable that is run whenever the user attempts to set
  1138. an attribute (either by assignment like ``i.x = 42`` or by using
  1139. `setattr` like ``setattr(i, "x", 42)``). It receives the same arguments
  1140. as validators: the instance, the attribute that is being modified, and
  1141. the new value.
  1142. If no exception is raised, the attribute is set to the return value of
  1143. the callable.
  1144. If a list of callables is passed, they're automatically wrapped in an
  1145. `attr.setters.pipe`.
  1146. :param Optional[callable] field_transformer:
  1147. A function that is called with the original class object and all
  1148. fields right before ``attrs`` finalizes the class. You can use
  1149. this, e.g., to automatically add converters or validators to
  1150. fields based on their types. See `transform-fields` for more details.
  1151. .. versionadded:: 16.0.0 *slots*
  1152. .. versionadded:: 16.1.0 *frozen*
  1153. .. versionadded:: 16.3.0 *str*
  1154. .. versionadded:: 16.3.0 Support for ``__attrs_post_init__``.
  1155. .. versionchanged:: 17.1.0
  1156. *hash* supports ``None`` as value which is also the default now.
  1157. .. versionadded:: 17.3.0 *auto_attribs*
  1158. .. versionchanged:: 18.1.0
  1159. If *these* is passed, no attributes are deleted from the class body.
  1160. .. versionchanged:: 18.1.0 If *these* is ordered, the order is retained.
  1161. .. versionadded:: 18.2.0 *weakref_slot*
  1162. .. deprecated:: 18.2.0
  1163. ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__`` now raise a
  1164. `DeprecationWarning` if the classes compared are subclasses of
  1165. each other. ``__eq`` and ``__ne__`` never tried to compared subclasses
  1166. to each other.
  1167. .. versionchanged:: 19.2.0
  1168. ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__`` now do not consider
  1169. subclasses comparable anymore.
  1170. .. versionadded:: 18.2.0 *kw_only*
  1171. .. versionadded:: 18.2.0 *cache_hash*
  1172. .. versionadded:: 19.1.0 *auto_exc*
  1173. .. deprecated:: 19.2.0 *cmp* Removal on or after 2021-06-01.
  1174. .. versionadded:: 19.2.0 *eq* and *order*
  1175. .. versionadded:: 20.1.0 *auto_detect*
  1176. .. versionadded:: 20.1.0 *collect_by_mro*
  1177. .. versionadded:: 20.1.0 *getstate_setstate*
  1178. .. versionadded:: 20.1.0 *on_setattr*
  1179. .. versionadded:: 20.3.0 *field_transformer*
  1180. .. versionchanged:: 21.1.0
  1181. ``init=False`` injects ``__attrs_init__``
  1182. .. versionchanged:: 21.1.0 Support for ``__attrs_pre_init__``
  1183. .. versionchanged:: 21.1.0 *cmp* undeprecated
  1184. """
  1185. if auto_detect and PY2:
  1186. raise PythonTooOldError(
  1187. "auto_detect only works on Python 3 and later."
  1188. )
  1189. eq_, order_ = _determine_attrs_eq_order(cmp, eq, order, None)
  1190. hash_ = hash # work around the lack of nonlocal
  1191. if isinstance(on_setattr, (list, tuple)):
  1192. on_setattr = setters.pipe(*on_setattr)
  1193. def wrap(cls):
  1194. if getattr(cls, "__class__", None) is None:
  1195. raise TypeError("attrs only works with new-style classes.")
  1196. is_frozen = frozen or _has_frozen_base_class(cls)
  1197. is_exc = auto_exc is True and issubclass(cls, BaseException)
  1198. has_own_setattr = auto_detect and _has_own_attribute(
  1199. cls, "__setattr__"
  1200. )
  1201. if has_own_setattr and is_frozen:
  1202. raise ValueError("Can't freeze a class with a custom __setattr__.")
  1203. builder = _ClassBuilder(
  1204. cls,
  1205. these,
  1206. slots,
  1207. is_frozen,
  1208. weakref_slot,
  1209. _determine_whether_to_implement(
  1210. cls,
  1211. getstate_setstate,
  1212. auto_detect,
  1213. ("__getstate__", "__setstate__"),
  1214. default=slots,
  1215. ),
  1216. auto_attribs,
  1217. kw_only,
  1218. cache_hash,
  1219. is_exc,
  1220. collect_by_mro,
  1221. on_setattr,
  1222. has_own_setattr,
  1223. field_transformer,
  1224. )
  1225. if _determine_whether_to_implement(
  1226. cls, repr, auto_detect, ("__repr__",)
  1227. ):
  1228. builder.add_repr(repr_ns)
  1229. if str is True:
  1230. builder.add_str()
  1231. eq = _determine_whether_to_implement(
  1232. cls, eq_, auto_detect, ("__eq__", "__ne__")
  1233. )
  1234. if not is_exc and eq is True:
  1235. builder.add_eq()
  1236. if not is_exc and _determine_whether_to_implement(
  1237. cls, order_, auto_detect, ("__lt__", "__le__", "__gt__", "__ge__")
  1238. ):
  1239. builder.add_order()
  1240. builder.add_setattr()
  1241. if (
  1242. hash_ is None
  1243. and auto_detect is True
  1244. and _has_own_attribute(cls, "__hash__")
  1245. ):
  1246. hash = False
  1247. else:
  1248. hash = hash_
  1249. if hash is not True and hash is not False and hash is not None:
  1250. # Can't use `hash in` because 1 == True for example.
  1251. raise TypeError(
  1252. "Invalid value for hash. Must be True, False, or None."
  1253. )
  1254. elif hash is False or (hash is None and eq is False) or is_exc:
  1255. # Don't do anything. Should fall back to __object__'s __hash__
  1256. # which is by id.
  1257. if cache_hash:
  1258. raise TypeError(
  1259. "Invalid value for cache_hash. To use hash caching,"
  1260. " hashing must be either explicitly or implicitly "
  1261. "enabled."
  1262. )
  1263. elif hash is True or (
  1264. hash is None and eq is True and is_frozen is True
  1265. ):
  1266. # Build a __hash__ if told so, or if it's safe.
  1267. builder.add_hash()
  1268. else:
  1269. # Raise TypeError on attempts to hash.
  1270. if cache_hash:
  1271. raise TypeError(
  1272. "Invalid value for cache_hash. To use hash caching,"
  1273. " hashing must be either explicitly or implicitly "
  1274. "enabled."
  1275. )
  1276. builder.make_unhashable()
  1277. if _determine_whether_to_implement(
  1278. cls, init, auto_detect, ("__init__",)
  1279. ):
  1280. builder.add_init()
  1281. else:
  1282. builder.add_attrs_init()
  1283. if cache_hash:
  1284. raise TypeError(
  1285. "Invalid value for cache_hash. To use hash caching,"
  1286. " init must be True."
  1287. )
  1288. return builder.build_class()
  1289. # maybe_cls's type depends on the usage of the decorator. It's a class
  1290. # if it's used as `@attrs` but ``None`` if used as `@attrs()`.
  1291. if maybe_cls is None:
  1292. return wrap
  1293. else:
  1294. return wrap(maybe_cls)
  1295. _attrs = attrs
  1296. """
  1297. Internal alias so we can use it in functions that take an argument called
  1298. *attrs*.
  1299. """
  1300. if PY2:
  1301. def _has_frozen_base_class(cls):
  1302. """
  1303. Check whether *cls* has a frozen ancestor by looking at its
  1304. __setattr__.
  1305. """
  1306. return (
  1307. getattr(cls.__setattr__, "__module__", None)
  1308. == _frozen_setattrs.__module__
  1309. and cls.__setattr__.__name__ == _frozen_setattrs.__name__
  1310. )
  1311. else:
  1312. def _has_frozen_base_class(cls):
  1313. """
  1314. Check whether *cls* has a frozen ancestor by looking at its
  1315. __setattr__.
  1316. """
  1317. return cls.__setattr__ == _frozen_setattrs
  1318. def _generate_unique_filename(cls, func_name):
  1319. """
  1320. Create a "filename" suitable for a function being generated.
  1321. """
  1322. unique_id = uuid.uuid4()
  1323. extra = ""
  1324. count = 1
  1325. while True:
  1326. unique_filename = "<attrs generated {0} {1}.{2}{3}>".format(
  1327. func_name,
  1328. cls.__module__,
  1329. getattr(cls, "__qualname__", cls.__name__),
  1330. extra,
  1331. )
  1332. # To handle concurrency we essentially "reserve" our spot in
  1333. # the linecache with a dummy line. The caller can then
  1334. # set this value correctly.
  1335. cache_line = (1, None, (str(unique_id),), unique_filename)
  1336. if (
  1337. linecache.cache.setdefault(unique_filename, cache_line)
  1338. == cache_line
  1339. ):
  1340. return unique_filename
  1341. # Looks like this spot is taken. Try again.
  1342. count += 1
  1343. extra = "-{0}".format(count)
  1344. def _make_hash(cls, attrs, frozen, cache_hash):
  1345. attrs = tuple(
  1346. a for a in attrs if a.hash is True or (a.hash is None and a.eq is True)
  1347. )
  1348. tab = " "
  1349. unique_filename = _generate_unique_filename(cls, "hash")
  1350. type_hash = hash(unique_filename)
  1351. hash_def = "def __hash__(self"
  1352. hash_func = "hash(("
  1353. closing_braces = "))"
  1354. if not cache_hash:
  1355. hash_def += "):"
  1356. else:
  1357. if not PY2:
  1358. hash_def += ", *"
  1359. hash_def += (
  1360. ", _cache_wrapper="
  1361. + "__import__('attr._make')._make._CacheHashWrapper):"
  1362. )
  1363. hash_func = "_cache_wrapper(" + hash_func
  1364. closing_braces += ")"
  1365. method_lines = [hash_def]
  1366. def append_hash_computation_lines(prefix, indent):
  1367. """
  1368. Generate the code for actually computing the hash code.
  1369. Below this will either be returned directly or used to compute
  1370. a value which is then cached, depending on the value of cache_hash
  1371. """
  1372. method_lines.extend(
  1373. [
  1374. indent + prefix + hash_func,
  1375. indent + " %d," % (type_hash,),
  1376. ]
  1377. )
  1378. for a in attrs:
  1379. method_lines.append(indent + " self.%s," % a.name)
  1380. method_lines.append(indent + " " + closing_braces)
  1381. if cache_hash:
  1382. method_lines.append(tab + "if self.%s is None:" % _hash_cache_field)
  1383. if frozen:
  1384. append_hash_computation_lines(
  1385. "object.__setattr__(self, '%s', " % _hash_cache_field, tab * 2
  1386. )
  1387. method_lines.append(tab * 2 + ")") # close __setattr__
  1388. else:
  1389. append_hash_computation_lines(
  1390. "self.%s = " % _hash_cache_field, tab * 2
  1391. )
  1392. method_lines.append(tab + "return self.%s" % _hash_cache_field)
  1393. else:
  1394. append_hash_computation_lines("return ", tab)
  1395. script = "\n".join(method_lines)
  1396. return _make_method("__hash__", script, unique_filename)
  1397. def _add_hash(cls, attrs):
  1398. """
  1399. Add a hash method to *cls*.
  1400. """
  1401. cls.__hash__ = _make_hash(cls, attrs, frozen=False, cache_hash=False)
  1402. return cls
  1403. def _make_ne():
  1404. """
  1405. Create __ne__ method.
  1406. """
  1407. def __ne__(self, other):
  1408. """
  1409. Check equality and either forward a NotImplemented or
  1410. return the result negated.
  1411. """
  1412. result = self.__eq__(other)
  1413. if result is NotImplemented:
  1414. return NotImplemented
  1415. return not result
  1416. return __ne__
  1417. def _make_eq(cls, attrs):
  1418. """
  1419. Create __eq__ method for *cls* with *attrs*.
  1420. """
  1421. attrs = [a for a in attrs if a.eq]
  1422. unique_filename = _generate_unique_filename(cls, "eq")
  1423. lines = [
  1424. "def __eq__(self, other):",
  1425. " if other.__class__ is not self.__class__:",
  1426. " return NotImplemented",
  1427. ]
  1428. # We can't just do a big self.x = other.x and... clause due to
  1429. # irregularities like nan == nan is false but (nan,) == (nan,) is true.
  1430. globs = {}
  1431. if attrs:
  1432. lines.append(" return (")
  1433. others = [" ) == ("]
  1434. for a in attrs:
  1435. if a.eq_key:
  1436. cmp_name = "_%s_key" % (a.name,)
  1437. # Add the key function to the global namespace
  1438. # of the evaluated function.
  1439. globs[cmp_name] = a.eq_key
  1440. lines.append(
  1441. " %s(self.%s),"
  1442. % (
  1443. cmp_name,
  1444. a.name,
  1445. )
  1446. )
  1447. others.append(
  1448. " %s(other.%s),"
  1449. % (
  1450. cmp_name,
  1451. a.name,
  1452. )
  1453. )
  1454. else:
  1455. lines.append(" self.%s," % (a.name,))
  1456. others.append(" other.%s," % (a.name,))
  1457. lines += others + [" )"]
  1458. else:
  1459. lines.append(" return True")
  1460. script = "\n".join(lines)
  1461. return _make_method("__eq__", script, unique_filename, globs)
  1462. def _make_order(cls, attrs):
  1463. """
  1464. Create ordering methods for *cls* with *attrs*.
  1465. """
  1466. attrs = [a for a in attrs if a.order]
  1467. def attrs_to_tuple(obj):
  1468. """
  1469. Save us some typing.
  1470. """
  1471. return tuple(
  1472. key(value) if key else value
  1473. for value, key in (
  1474. (getattr(obj, a.name), a.order_key) for a in attrs
  1475. )
  1476. )
  1477. def __lt__(self, other):
  1478. """
  1479. Automatically created by attrs.
  1480. """
  1481. if other.__class__ is self.__class__:
  1482. return attrs_to_tuple(self) < attrs_to_tuple(other)
  1483. return NotImplemented
  1484. def __le__(self, other):
  1485. """
  1486. Automatically created by attrs.
  1487. """
  1488. if other.__class__ is self.__class__:
  1489. return attrs_to_tuple(self) <= attrs_to_tuple(other)
  1490. return NotImplemented
  1491. def __gt__(self, other):
  1492. """
  1493. Automatically created by attrs.
  1494. """
  1495. if other.__class__ is self.__class__:
  1496. return attrs_to_tuple(self) > attrs_to_tuple(other)
  1497. return NotImplemented
  1498. def __ge__(self, other):
  1499. """
  1500. Automatically created by attrs.
  1501. """
  1502. if other.__class__ is self.__class__:
  1503. return attrs_to_tuple(self) >= attrs_to_tuple(other)
  1504. return NotImplemented
  1505. return __lt__, __le__, __gt__, __ge__
  1506. def _add_eq(cls, attrs=None):
  1507. """
  1508. Add equality methods to *cls* with *attrs*.
  1509. """
  1510. if attrs is None:
  1511. attrs = cls.__attrs_attrs__
  1512. cls.__eq__ = _make_eq(cls, attrs)
  1513. cls.__ne__ = _make_ne()
  1514. return cls
  1515. _already_repring = threading.local()
  1516. def _make_repr(attrs, ns):
  1517. """
  1518. Make a repr method that includes relevant *attrs*, adding *ns* to the full
  1519. name.
  1520. """
  1521. # Figure out which attributes to include, and which function to use to
  1522. # format them. The a.repr value can be either bool or a custom callable.
  1523. attr_names_with_reprs = tuple(
  1524. (a.name, repr if a.repr is True else a.repr)
  1525. for a in attrs
  1526. if a.repr is not False
  1527. )
  1528. def __repr__(self):
  1529. """
  1530. Automatically created by attrs.
  1531. """
  1532. try:
  1533. working_set = _already_repring.working_set
  1534. except AttributeError:
  1535. working_set = set()
  1536. _already_repring.working_set = working_set
  1537. if id(self) in working_set:
  1538. return "..."
  1539. real_cls = self.__class__
  1540. if ns is None:
  1541. qualname = getattr(real_cls, "__qualname__", None)
  1542. if qualname is not None:
  1543. class_name = qualname.rsplit(">.", 1)[-1]
  1544. else:
  1545. class_name = real_cls.__name__
  1546. else:
  1547. class_name = ns + "." + real_cls.__name__
  1548. # Since 'self' remains on the stack (i.e.: strongly referenced) for the
  1549. # duration of this call, it's safe to depend on id(...) stability, and
  1550. # not need to track the instance and therefore worry about properties
  1551. # like weakref- or hash-ability.
  1552. working_set.add(id(self))
  1553. try:
  1554. result = [class_name, "("]
  1555. first = True
  1556. for name, attr_repr in attr_names_with_reprs:
  1557. if first:
  1558. first = False
  1559. else:
  1560. result.append(", ")
  1561. result.extend(
  1562. (name, "=", attr_repr(getattr(self, name, NOTHING)))
  1563. )
  1564. return "".join(result) + ")"
  1565. finally:
  1566. working_set.remove(id(self))
  1567. return __repr__
  1568. def _add_repr(cls, ns=None, attrs=None):
  1569. """
  1570. Add a repr method to *cls*.
  1571. """
  1572. if attrs is None:
  1573. attrs = cls.__attrs_attrs__
  1574. cls.__repr__ = _make_repr(attrs, ns)
  1575. return cls
  1576. def fields(cls):
  1577. """
  1578. Return the tuple of ``attrs`` attributes for a class.
  1579. The tuple also allows accessing the fields by their names (see below for
  1580. examples).
  1581. :param type cls: Class to introspect.
  1582. :raise TypeError: If *cls* is not a class.
  1583. :raise attr.exceptions.NotAnAttrsClassError: If *cls* is not an ``attrs``
  1584. class.
  1585. :rtype: tuple (with name accessors) of `attr.Attribute`
  1586. .. versionchanged:: 16.2.0 Returned tuple allows accessing the fields
  1587. by name.
  1588. """
  1589. if not isclass(cls):
  1590. raise TypeError("Passed object must be a class.")
  1591. attrs = getattr(cls, "__attrs_attrs__", None)
  1592. if attrs is None:
  1593. raise NotAnAttrsClassError(
  1594. "{cls!r} is not an attrs-decorated class.".format(cls=cls)
  1595. )
  1596. return attrs
  1597. def fields_dict(cls):
  1598. """
  1599. Return an ordered dictionary of ``attrs`` attributes for a class, whose
  1600. keys are the attribute names.
  1601. :param type cls: Class to introspect.
  1602. :raise TypeError: If *cls* is not a class.
  1603. :raise attr.exceptions.NotAnAttrsClassError: If *cls* is not an ``attrs``
  1604. class.
  1605. :rtype: an ordered dict where keys are attribute names and values are
  1606. `attr.Attribute`\\ s. This will be a `dict` if it's
  1607. naturally ordered like on Python 3.6+ or an
  1608. :class:`~collections.OrderedDict` otherwise.
  1609. .. versionadded:: 18.1.0
  1610. """
  1611. if not isclass(cls):
  1612. raise TypeError("Passed object must be a class.")
  1613. attrs = getattr(cls, "__attrs_attrs__", None)
  1614. if attrs is None:
  1615. raise NotAnAttrsClassError(
  1616. "{cls!r} is not an attrs-decorated class.".format(cls=cls)
  1617. )
  1618. return ordered_dict(((a.name, a) for a in attrs))
  1619. def validate(inst):
  1620. """
  1621. Validate all attributes on *inst* that have a validator.
  1622. Leaves all exceptions through.
  1623. :param inst: Instance of a class with ``attrs`` attributes.
  1624. """
  1625. if _config._run_validators is False:
  1626. return
  1627. for a in fields(inst.__class__):
  1628. v = a.validator
  1629. if v is not None:
  1630. v(inst, a, getattr(inst, a.name))
  1631. def _is_slot_cls(cls):
  1632. return "__slots__" in cls.__dict__
  1633. def _is_slot_attr(a_name, base_attr_map):
  1634. """
  1635. Check if the attribute name comes from a slot class.
  1636. """
  1637. return a_name in base_attr_map and _is_slot_cls(base_attr_map[a_name])
  1638. def _make_init(
  1639. cls,
  1640. attrs,
  1641. pre_init,
  1642. post_init,
  1643. frozen,
  1644. slots,
  1645. cache_hash,
  1646. base_attr_map,
  1647. is_exc,
  1648. has_global_on_setattr,
  1649. attrs_init,
  1650. ):
  1651. if frozen and has_global_on_setattr:
  1652. raise ValueError("Frozen classes can't use on_setattr.")
  1653. needs_cached_setattr = cache_hash or frozen
  1654. filtered_attrs = []
  1655. attr_dict = {}
  1656. for a in attrs:
  1657. if not a.init and a.default is NOTHING:
  1658. continue
  1659. filtered_attrs.append(a)
  1660. attr_dict[a.name] = a
  1661. if a.on_setattr is not None:
  1662. if frozen is True:
  1663. raise ValueError("Frozen classes can't use on_setattr.")
  1664. needs_cached_setattr = True
  1665. elif (
  1666. has_global_on_setattr and a.on_setattr is not setters.NO_OP
  1667. ) or _is_slot_attr(a.name, base_attr_map):
  1668. needs_cached_setattr = True
  1669. unique_filename = _generate_unique_filename(cls, "init")
  1670. script, globs, annotations = _attrs_to_init_script(
  1671. filtered_attrs,
  1672. frozen,
  1673. slots,
  1674. pre_init,
  1675. post_init,
  1676. cache_hash,
  1677. base_attr_map,
  1678. is_exc,
  1679. needs_cached_setattr,
  1680. has_global_on_setattr,
  1681. attrs_init,
  1682. )
  1683. if cls.__module__ in sys.modules:
  1684. # This makes typing.get_type_hints(CLS.__init__) resolve string types.
  1685. globs.update(sys.modules[cls.__module__].__dict__)
  1686. globs.update({"NOTHING": NOTHING, "attr_dict": attr_dict})
  1687. if needs_cached_setattr:
  1688. # Save the lookup overhead in __init__ if we need to circumvent
  1689. # setattr hooks.
  1690. globs["_cached_setattr"] = _obj_setattr
  1691. init = _make_method(
  1692. "__attrs_init__" if attrs_init else "__init__",
  1693. script,
  1694. unique_filename,
  1695. globs,
  1696. )
  1697. init.__annotations__ = annotations
  1698. return init
  1699. def _setattr(attr_name, value_var, has_on_setattr):
  1700. """
  1701. Use the cached object.setattr to set *attr_name* to *value_var*.
  1702. """
  1703. return "_setattr('%s', %s)" % (attr_name, value_var)
  1704. def _setattr_with_converter(attr_name, value_var, has_on_setattr):
  1705. """
  1706. Use the cached object.setattr to set *attr_name* to *value_var*, but run
  1707. its converter first.
  1708. """
  1709. return "_setattr('%s', %s(%s))" % (
  1710. attr_name,
  1711. _init_converter_pat % (attr_name,),
  1712. value_var,
  1713. )
  1714. def _assign(attr_name, value, has_on_setattr):
  1715. """
  1716. Unless *attr_name* has an on_setattr hook, use normal assignment. Otherwise
  1717. relegate to _setattr.
  1718. """
  1719. if has_on_setattr:
  1720. return _setattr(attr_name, value, True)
  1721. return "self.%s = %s" % (attr_name, value)
  1722. def _assign_with_converter(attr_name, value_var, has_on_setattr):
  1723. """
  1724. Unless *attr_name* has an on_setattr hook, use normal assignment after
  1725. conversion. Otherwise relegate to _setattr_with_converter.
  1726. """
  1727. if has_on_setattr:
  1728. return _setattr_with_converter(attr_name, value_var, True)
  1729. return "self.%s = %s(%s)" % (
  1730. attr_name,
  1731. _init_converter_pat % (attr_name,),
  1732. value_var,
  1733. )
  1734. if PY2:
  1735. def _unpack_kw_only_py2(attr_name, default=None):
  1736. """
  1737. Unpack *attr_name* from _kw_only dict.
  1738. """
  1739. if default is not None:
  1740. arg_default = ", %s" % default
  1741. else:
  1742. arg_default = ""
  1743. return "%s = _kw_only.pop('%s'%s)" % (
  1744. attr_name,
  1745. attr_name,
  1746. arg_default,
  1747. )
  1748. def _unpack_kw_only_lines_py2(kw_only_args):
  1749. """
  1750. Unpack all *kw_only_args* from _kw_only dict and handle errors.
  1751. Given a list of strings "{attr_name}" and "{attr_name}={default}"
  1752. generates list of lines of code that pop attrs from _kw_only dict and
  1753. raise TypeError similar to builtin if required attr is missing or
  1754. extra key is passed.
  1755. >>> print("\n".join(_unpack_kw_only_lines_py2(["a", "b=42"])))
  1756. try:
  1757. a = _kw_only.pop('a')
  1758. b = _kw_only.pop('b', 42)
  1759. except KeyError as _key_error:
  1760. raise TypeError(
  1761. ...
  1762. if _kw_only:
  1763. raise TypeError(
  1764. ...
  1765. """
  1766. lines = ["try:"]
  1767. lines.extend(
  1768. " " + _unpack_kw_only_py2(*arg.split("="))
  1769. for arg in kw_only_args
  1770. )
  1771. lines += """\
  1772. except KeyError as _key_error:
  1773. raise TypeError(
  1774. '__init__() missing required keyword-only argument: %s' % _key_error
  1775. )
  1776. if _kw_only:
  1777. raise TypeError(
  1778. '__init__() got an unexpected keyword argument %r'
  1779. % next(iter(_kw_only))
  1780. )
  1781. """.split(
  1782. "\n"
  1783. )
  1784. return lines
  1785. def _attrs_to_init_script(
  1786. attrs,
  1787. frozen,
  1788. slots,
  1789. pre_init,
  1790. post_init,
  1791. cache_hash,
  1792. base_attr_map,
  1793. is_exc,
  1794. needs_cached_setattr,
  1795. has_global_on_setattr,
  1796. attrs_init,
  1797. ):
  1798. """
  1799. Return a script of an initializer for *attrs* and a dict of globals.
  1800. The globals are expected by the generated script.
  1801. If *frozen* is True, we cannot set the attributes directly so we use
  1802. a cached ``object.__setattr__``.
  1803. """
  1804. lines = []
  1805. if pre_init:
  1806. lines.append("self.__attrs_pre_init__()")
  1807. if needs_cached_setattr:
  1808. lines.append(
  1809. # Circumvent the __setattr__ descriptor to save one lookup per
  1810. # assignment.
  1811. # Note _setattr will be used again below if cache_hash is True
  1812. "_setattr = _cached_setattr.__get__(self, self.__class__)"
  1813. )
  1814. if frozen is True:
  1815. if slots is True:
  1816. fmt_setter = _setattr
  1817. fmt_setter_with_converter = _setattr_with_converter
  1818. else:
  1819. # Dict frozen classes assign directly to __dict__.
  1820. # But only if the attribute doesn't come from an ancestor slot
  1821. # class.
  1822. # Note _inst_dict will be used again below if cache_hash is True
  1823. lines.append("_inst_dict = self.__dict__")
  1824. def fmt_setter(attr_name, value_var, has_on_setattr):
  1825. if _is_slot_attr(attr_name, base_attr_map):
  1826. return _setattr(attr_name, value_var, has_on_setattr)
  1827. return "_inst_dict['%s'] = %s" % (attr_name, value_var)
  1828. def fmt_setter_with_converter(
  1829. attr_name, value_var, has_on_setattr
  1830. ):
  1831. if has_on_setattr or _is_slot_attr(attr_name, base_attr_map):
  1832. return _setattr_with_converter(
  1833. attr_name, value_var, has_on_setattr
  1834. )
  1835. return "_inst_dict['%s'] = %s(%s)" % (
  1836. attr_name,
  1837. _init_converter_pat % (attr_name,),
  1838. value_var,
  1839. )
  1840. else:
  1841. # Not frozen.
  1842. fmt_setter = _assign
  1843. fmt_setter_with_converter = _assign_with_converter
  1844. args = []
  1845. kw_only_args = []
  1846. attrs_to_validate = []
  1847. # This is a dictionary of names to validator and converter callables.
  1848. # Injecting this into __init__ globals lets us avoid lookups.
  1849. names_for_globals = {}
  1850. annotations = {"return": None}
  1851. for a in attrs:
  1852. if a.validator:
  1853. attrs_to_validate.append(a)
  1854. attr_name = a.name
  1855. has_on_setattr = a.on_setattr is not None or (
  1856. a.on_setattr is not setters.NO_OP and has_global_on_setattr
  1857. )
  1858. arg_name = a.name.lstrip("_")
  1859. has_factory = isinstance(a.default, Factory)
  1860. if has_factory and a.default.takes_self:
  1861. maybe_self = "self"
  1862. else:
  1863. maybe_self = ""
  1864. if a.init is False:
  1865. if has_factory:
  1866. init_factory_name = _init_factory_pat.format(a.name)
  1867. if a.converter is not None:
  1868. lines.append(
  1869. fmt_setter_with_converter(
  1870. attr_name,
  1871. init_factory_name + "(%s)" % (maybe_self,),
  1872. has_on_setattr,
  1873. )
  1874. )
  1875. conv_name = _init_converter_pat % (a.name,)
  1876. names_for_globals[conv_name] = a.converter
  1877. else:
  1878. lines.append(
  1879. fmt_setter(
  1880. attr_name,
  1881. init_factory_name + "(%s)" % (maybe_self,),
  1882. has_on_setattr,
  1883. )
  1884. )
  1885. names_for_globals[init_factory_name] = a.default.factory
  1886. else:
  1887. if a.converter is not None:
  1888. lines.append(
  1889. fmt_setter_with_converter(
  1890. attr_name,
  1891. "attr_dict['%s'].default" % (attr_name,),
  1892. has_on_setattr,
  1893. )
  1894. )
  1895. conv_name = _init_converter_pat % (a.name,)
  1896. names_for_globals[conv_name] = a.converter
  1897. else:
  1898. lines.append(
  1899. fmt_setter(
  1900. attr_name,
  1901. "attr_dict['%s'].default" % (attr_name,),
  1902. has_on_setattr,
  1903. )
  1904. )
  1905. elif a.default is not NOTHING and not has_factory:
  1906. arg = "%s=attr_dict['%s'].default" % (arg_name, attr_name)
  1907. if a.kw_only:
  1908. kw_only_args.append(arg)
  1909. else:
  1910. args.append(arg)
  1911. if a.converter is not None:
  1912. lines.append(
  1913. fmt_setter_with_converter(
  1914. attr_name, arg_name, has_on_setattr
  1915. )
  1916. )
  1917. names_for_globals[
  1918. _init_converter_pat % (a.name,)
  1919. ] = a.converter
  1920. else:
  1921. lines.append(fmt_setter(attr_name, arg_name, has_on_setattr))
  1922. elif has_factory:
  1923. arg = "%s=NOTHING" % (arg_name,)
  1924. if a.kw_only:
  1925. kw_only_args.append(arg)
  1926. else:
  1927. args.append(arg)
  1928. lines.append("if %s is not NOTHING:" % (arg_name,))
  1929. init_factory_name = _init_factory_pat.format(a.name)
  1930. if a.converter is not None:
  1931. lines.append(
  1932. " "
  1933. + fmt_setter_with_converter(
  1934. attr_name, arg_name, has_on_setattr
  1935. )
  1936. )
  1937. lines.append("else:")
  1938. lines.append(
  1939. " "
  1940. + fmt_setter_with_converter(
  1941. attr_name,
  1942. init_factory_name + "(" + maybe_self + ")",
  1943. has_on_setattr,
  1944. )
  1945. )
  1946. names_for_globals[
  1947. _init_converter_pat % (a.name,)
  1948. ] = a.converter
  1949. else:
  1950. lines.append(
  1951. " " + fmt_setter(attr_name, arg_name, has_on_setattr)
  1952. )
  1953. lines.append("else:")
  1954. lines.append(
  1955. " "
  1956. + fmt_setter(
  1957. attr_name,
  1958. init_factory_name + "(" + maybe_self + ")",
  1959. has_on_setattr,
  1960. )
  1961. )
  1962. names_for_globals[init_factory_name] = a.default.factory
  1963. else:
  1964. if a.kw_only:
  1965. kw_only_args.append(arg_name)
  1966. else:
  1967. args.append(arg_name)
  1968. if a.converter is not None:
  1969. lines.append(
  1970. fmt_setter_with_converter(
  1971. attr_name, arg_name, has_on_setattr
  1972. )
  1973. )
  1974. names_for_globals[
  1975. _init_converter_pat % (a.name,)
  1976. ] = a.converter
  1977. else:
  1978. lines.append(fmt_setter(attr_name, arg_name, has_on_setattr))
  1979. if a.init is True:
  1980. if a.type is not None and a.converter is None:
  1981. annotations[arg_name] = a.type
  1982. elif a.converter is not None and not PY2:
  1983. # Try to get the type from the converter.
  1984. sig = None
  1985. try:
  1986. sig = inspect.signature(a.converter)
  1987. except (ValueError, TypeError): # inspect failed
  1988. pass
  1989. if sig:
  1990. sig_params = list(sig.parameters.values())
  1991. if (
  1992. sig_params
  1993. and sig_params[0].annotation
  1994. is not inspect.Parameter.empty
  1995. ):
  1996. annotations[arg_name] = sig_params[0].annotation
  1997. if attrs_to_validate: # we can skip this if there are no validators.
  1998. names_for_globals["_config"] = _config
  1999. lines.append("if _config._run_validators is True:")
  2000. for a in attrs_to_validate:
  2001. val_name = "__attr_validator_" + a.name
  2002. attr_name = "__attr_" + a.name
  2003. lines.append(
  2004. " %s(self, %s, self.%s)" % (val_name, attr_name, a.name)
  2005. )
  2006. names_for_globals[val_name] = a.validator
  2007. names_for_globals[attr_name] = a
  2008. if post_init:
  2009. lines.append("self.__attrs_post_init__()")
  2010. # because this is set only after __attrs_post_init is called, a crash
  2011. # will result if post-init tries to access the hash code. This seemed
  2012. # preferable to setting this beforehand, in which case alteration to
  2013. # field values during post-init combined with post-init accessing the
  2014. # hash code would result in silent bugs.
  2015. if cache_hash:
  2016. if frozen:
  2017. if slots:
  2018. # if frozen and slots, then _setattr defined above
  2019. init_hash_cache = "_setattr('%s', %s)"
  2020. else:
  2021. # if frozen and not slots, then _inst_dict defined above
  2022. init_hash_cache = "_inst_dict['%s'] = %s"
  2023. else:
  2024. init_hash_cache = "self.%s = %s"
  2025. lines.append(init_hash_cache % (_hash_cache_field, "None"))
  2026. # For exceptions we rely on BaseException.__init__ for proper
  2027. # initialization.
  2028. if is_exc:
  2029. vals = ",".join("self." + a.name for a in attrs if a.init)
  2030. lines.append("BaseException.__init__(self, %s)" % (vals,))
  2031. args = ", ".join(args)
  2032. if kw_only_args:
  2033. if PY2:
  2034. lines = _unpack_kw_only_lines_py2(kw_only_args) + lines
  2035. args += "%s**_kw_only" % (", " if args else "",) # leading comma
  2036. else:
  2037. args += "%s*, %s" % (
  2038. ", " if args else "", # leading comma
  2039. ", ".join(kw_only_args), # kw_only args
  2040. )
  2041. return (
  2042. """\
  2043. def {init_name}(self, {args}):
  2044. {lines}
  2045. """.format(
  2046. init_name=("__attrs_init__" if attrs_init else "__init__"),
  2047. args=args,
  2048. lines="\n ".join(lines) if lines else "pass",
  2049. ),
  2050. names_for_globals,
  2051. annotations,
  2052. )
  2053. class Attribute(object):
  2054. """
  2055. *Read-only* representation of an attribute.
  2056. Instances of this class are frequently used for introspection purposes
  2057. like:
  2058. - `fields` returns a tuple of them.
  2059. - Validators get them passed as the first argument.
  2060. - The *field transformer* hook receives a list of them.
  2061. :attribute name: The name of the attribute.
  2062. :attribute inherited: Whether or not that attribute has been inherited from
  2063. a base class.
  2064. Plus *all* arguments of `attr.ib` (except for ``factory``
  2065. which is only syntactic sugar for ``default=Factory(...)``.
  2066. .. versionadded:: 20.1.0 *inherited*
  2067. .. versionadded:: 20.1.0 *on_setattr*
  2068. .. versionchanged:: 20.2.0 *inherited* is not taken into account for
  2069. equality checks and hashing anymore.
  2070. .. versionadded:: 21.1.0 *eq_key* and *order_key*
  2071. For the full version history of the fields, see `attr.ib`.
  2072. """
  2073. __slots__ = (
  2074. "name",
  2075. "default",
  2076. "validator",
  2077. "repr",
  2078. "eq",
  2079. "eq_key",
  2080. "order",
  2081. "order_key",
  2082. "hash",
  2083. "init",
  2084. "metadata",
  2085. "type",
  2086. "converter",
  2087. "kw_only",
  2088. "inherited",
  2089. "on_setattr",
  2090. )
  2091. def __init__(
  2092. self,
  2093. name,
  2094. default,
  2095. validator,
  2096. repr,
  2097. cmp, # XXX: unused, remove along with other cmp code.
  2098. hash,
  2099. init,
  2100. inherited,
  2101. metadata=None,
  2102. type=None,
  2103. converter=None,
  2104. kw_only=False,
  2105. eq=None,
  2106. eq_key=None,
  2107. order=None,
  2108. order_key=None,
  2109. on_setattr=None,
  2110. ):
  2111. eq, eq_key, order, order_key = _determine_attrib_eq_order(
  2112. cmp, eq_key or eq, order_key or order, True
  2113. )
  2114. # Cache this descriptor here to speed things up later.
  2115. bound_setattr = _obj_setattr.__get__(self, Attribute)
  2116. # Despite the big red warning, people *do* instantiate `Attribute`
  2117. # themselves.
  2118. bound_setattr("name", name)
  2119. bound_setattr("default", default)
  2120. bound_setattr("validator", validator)
  2121. bound_setattr("repr", repr)
  2122. bound_setattr("eq", eq)
  2123. bound_setattr("eq_key", eq_key)
  2124. bound_setattr("order", order)
  2125. bound_setattr("order_key", order_key)
  2126. bound_setattr("hash", hash)
  2127. bound_setattr("init", init)
  2128. bound_setattr("converter", converter)
  2129. bound_setattr(
  2130. "metadata",
  2131. (
  2132. metadata_proxy(metadata)
  2133. if metadata
  2134. else _empty_metadata_singleton
  2135. ),
  2136. )
  2137. bound_setattr("type", type)
  2138. bound_setattr("kw_only", kw_only)
  2139. bound_setattr("inherited", inherited)
  2140. bound_setattr("on_setattr", on_setattr)
  2141. def __setattr__(self, name, value):
  2142. raise FrozenInstanceError()
  2143. @classmethod
  2144. def from_counting_attr(cls, name, ca, type=None):
  2145. # type holds the annotated value. deal with conflicts:
  2146. if type is None:
  2147. type = ca.type
  2148. elif ca.type is not None:
  2149. raise ValueError(
  2150. "Type annotation and type argument cannot both be present"
  2151. )
  2152. inst_dict = {
  2153. k: getattr(ca, k)
  2154. for k in Attribute.__slots__
  2155. if k
  2156. not in (
  2157. "name",
  2158. "validator",
  2159. "default",
  2160. "type",
  2161. "inherited",
  2162. ) # exclude methods and deprecated alias
  2163. }
  2164. return cls(
  2165. name=name,
  2166. validator=ca._validator,
  2167. default=ca._default,
  2168. type=type,
  2169. cmp=None,
  2170. inherited=False,
  2171. **inst_dict
  2172. )
  2173. @property
  2174. def cmp(self):
  2175. """
  2176. Simulate the presence of a cmp attribute and warn.
  2177. """
  2178. warnings.warn(_CMP_DEPRECATION, DeprecationWarning, stacklevel=2)
  2179. return self.eq and self.order
  2180. # Don't use attr.evolve since fields(Attribute) doesn't work
  2181. def evolve(self, **changes):
  2182. """
  2183. Copy *self* and apply *changes*.
  2184. This works similarly to `attr.evolve` but that function does not work
  2185. with ``Attribute``.
  2186. It is mainly meant to be used for `transform-fields`.
  2187. .. versionadded:: 20.3.0
  2188. """
  2189. new = copy.copy(self)
  2190. new._setattrs(changes.items())
  2191. return new
  2192. # Don't use _add_pickle since fields(Attribute) doesn't work
  2193. def __getstate__(self):
  2194. """
  2195. Play nice with pickle.
  2196. """
  2197. return tuple(
  2198. getattr(self, name) if name != "metadata" else dict(self.metadata)
  2199. for name in self.__slots__
  2200. )
  2201. def __setstate__(self, state):
  2202. """
  2203. Play nice with pickle.
  2204. """
  2205. self._setattrs(zip(self.__slots__, state))
  2206. def _setattrs(self, name_values_pairs):
  2207. bound_setattr = _obj_setattr.__get__(self, Attribute)
  2208. for name, value in name_values_pairs:
  2209. if name != "metadata":
  2210. bound_setattr(name, value)
  2211. else:
  2212. bound_setattr(
  2213. name,
  2214. metadata_proxy(value)
  2215. if value
  2216. else _empty_metadata_singleton,
  2217. )
  2218. _a = [
  2219. Attribute(
  2220. name=name,
  2221. default=NOTHING,
  2222. validator=None,
  2223. repr=True,
  2224. cmp=None,
  2225. eq=True,
  2226. order=False,
  2227. hash=(name != "metadata"),
  2228. init=True,
  2229. inherited=False,
  2230. )
  2231. for name in Attribute.__slots__
  2232. ]
  2233. Attribute = _add_hash(
  2234. _add_eq(
  2235. _add_repr(Attribute, attrs=_a),
  2236. attrs=[a for a in _a if a.name != "inherited"],
  2237. ),
  2238. attrs=[a for a in _a if a.hash and a.name != "inherited"],
  2239. )
  2240. class _CountingAttr(object):
  2241. """
  2242. Intermediate representation of attributes that uses a counter to preserve
  2243. the order in which the attributes have been defined.
  2244. *Internal* data structure of the attrs library. Running into is most
  2245. likely the result of a bug like a forgotten `@attr.s` decorator.
  2246. """
  2247. __slots__ = (
  2248. "counter",
  2249. "_default",
  2250. "repr",
  2251. "eq",
  2252. "eq_key",
  2253. "order",
  2254. "order_key",
  2255. "hash",
  2256. "init",
  2257. "metadata",
  2258. "_validator",
  2259. "converter",
  2260. "type",
  2261. "kw_only",
  2262. "on_setattr",
  2263. )
  2264. __attrs_attrs__ = tuple(
  2265. Attribute(
  2266. name=name,
  2267. default=NOTHING,
  2268. validator=None,
  2269. repr=True,
  2270. cmp=None,
  2271. hash=True,
  2272. init=True,
  2273. kw_only=False,
  2274. eq=True,
  2275. eq_key=None,
  2276. order=False,
  2277. order_key=None,
  2278. inherited=False,
  2279. on_setattr=None,
  2280. )
  2281. for name in (
  2282. "counter",
  2283. "_default",
  2284. "repr",
  2285. "eq",
  2286. "order",
  2287. "hash",
  2288. "init",
  2289. "on_setattr",
  2290. )
  2291. ) + (
  2292. Attribute(
  2293. name="metadata",
  2294. default=None,
  2295. validator=None,
  2296. repr=True,
  2297. cmp=None,
  2298. hash=False,
  2299. init=True,
  2300. kw_only=False,
  2301. eq=True,
  2302. eq_key=None,
  2303. order=False,
  2304. order_key=None,
  2305. inherited=False,
  2306. on_setattr=None,
  2307. ),
  2308. )
  2309. cls_counter = 0
  2310. def __init__(
  2311. self,
  2312. default,
  2313. validator,
  2314. repr,
  2315. cmp,
  2316. hash,
  2317. init,
  2318. converter,
  2319. metadata,
  2320. type,
  2321. kw_only,
  2322. eq,
  2323. eq_key,
  2324. order,
  2325. order_key,
  2326. on_setattr,
  2327. ):
  2328. _CountingAttr.cls_counter += 1
  2329. self.counter = _CountingAttr.cls_counter
  2330. self._default = default
  2331. self._validator = validator
  2332. self.converter = converter
  2333. self.repr = repr
  2334. self.eq = eq
  2335. self.eq_key = eq_key
  2336. self.order = order
  2337. self.order_key = order_key
  2338. self.hash = hash
  2339. self.init = init
  2340. self.metadata = metadata
  2341. self.type = type
  2342. self.kw_only = kw_only
  2343. self.on_setattr = on_setattr
  2344. def validator(self, meth):
  2345. """
  2346. Decorator that adds *meth* to the list of validators.
  2347. Returns *meth* unchanged.
  2348. .. versionadded:: 17.1.0
  2349. """
  2350. if self._validator is None:
  2351. self._validator = meth
  2352. else:
  2353. self._validator = and_(self._validator, meth)
  2354. return meth
  2355. def default(self, meth):
  2356. """
  2357. Decorator that allows to set the default for an attribute.
  2358. Returns *meth* unchanged.
  2359. :raises DefaultAlreadySetError: If default has been set before.
  2360. .. versionadded:: 17.1.0
  2361. """
  2362. if self._default is not NOTHING:
  2363. raise DefaultAlreadySetError()
  2364. self._default = Factory(meth, takes_self=True)
  2365. return meth
  2366. _CountingAttr = _add_eq(_add_repr(_CountingAttr))
  2367. class Factory(object):
  2368. """
  2369. Stores a factory callable.
  2370. If passed as the default value to `attr.ib`, the factory is used to
  2371. generate a new value.
  2372. :param callable factory: A callable that takes either none or exactly one
  2373. mandatory positional argument depending on *takes_self*.
  2374. :param bool takes_self: Pass the partially initialized instance that is
  2375. being initialized as a positional argument.
  2376. .. versionadded:: 17.1.0 *takes_self*
  2377. """
  2378. __slots__ = ("factory", "takes_self")
  2379. def __init__(self, factory, takes_self=False):
  2380. """
  2381. `Factory` is part of the default machinery so if we want a default
  2382. value here, we have to implement it ourselves.
  2383. """
  2384. self.factory = factory
  2385. self.takes_self = takes_self
  2386. def __getstate__(self):
  2387. """
  2388. Play nice with pickle.
  2389. """
  2390. return tuple(getattr(self, name) for name in self.__slots__)
  2391. def __setstate__(self, state):
  2392. """
  2393. Play nice with pickle.
  2394. """
  2395. for name, value in zip(self.__slots__, state):
  2396. setattr(self, name, value)
  2397. _f = [
  2398. Attribute(
  2399. name=name,
  2400. default=NOTHING,
  2401. validator=None,
  2402. repr=True,
  2403. cmp=None,
  2404. eq=True,
  2405. order=False,
  2406. hash=True,
  2407. init=True,
  2408. inherited=False,
  2409. )
  2410. for name in Factory.__slots__
  2411. ]
  2412. Factory = _add_hash(_add_eq(_add_repr(Factory, attrs=_f), attrs=_f), attrs=_f)
  2413. def make_class(name, attrs, bases=(object,), **attributes_arguments):
  2414. """
  2415. A quick way to create a new class called *name* with *attrs*.
  2416. :param str name: The name for the new class.
  2417. :param attrs: A list of names or a dictionary of mappings of names to
  2418. attributes.
  2419. If *attrs* is a list or an ordered dict (`dict` on Python 3.6+,
  2420. `collections.OrderedDict` otherwise), the order is deduced from
  2421. the order of the names or attributes inside *attrs*. Otherwise the
  2422. order of the definition of the attributes is used.
  2423. :type attrs: `list` or `dict`
  2424. :param tuple bases: Classes that the new class will subclass.
  2425. :param attributes_arguments: Passed unmodified to `attr.s`.
  2426. :return: A new class with *attrs*.
  2427. :rtype: type
  2428. .. versionadded:: 17.1.0 *bases*
  2429. .. versionchanged:: 18.1.0 If *attrs* is ordered, the order is retained.
  2430. """
  2431. if isinstance(attrs, dict):
  2432. cls_dict = attrs
  2433. elif isinstance(attrs, (list, tuple)):
  2434. cls_dict = dict((a, attrib()) for a in attrs)
  2435. else:
  2436. raise TypeError("attrs argument must be a dict or a list.")
  2437. pre_init = cls_dict.pop("__attrs_pre_init__", None)
  2438. post_init = cls_dict.pop("__attrs_post_init__", None)
  2439. user_init = cls_dict.pop("__init__", None)
  2440. body = {}
  2441. if pre_init is not None:
  2442. body["__attrs_pre_init__"] = pre_init
  2443. if post_init is not None:
  2444. body["__attrs_post_init__"] = post_init
  2445. if user_init is not None:
  2446. body["__init__"] = user_init
  2447. type_ = new_class(name, bases, {}, lambda ns: ns.update(body))
  2448. # For pickling to work, the __module__ variable needs to be set to the
  2449. # frame where the class is created. Bypass this step in environments where
  2450. # sys._getframe is not defined (Jython for example) or sys._getframe is not
  2451. # defined for arguments greater than 0 (IronPython).
  2452. try:
  2453. type_.__module__ = sys._getframe(1).f_globals.get(
  2454. "__name__", "__main__"
  2455. )
  2456. except (AttributeError, ValueError):
  2457. pass
  2458. # We do it here for proper warnings with meaningful stacklevel.
  2459. cmp = attributes_arguments.pop("cmp", None)
  2460. (
  2461. attributes_arguments["eq"],
  2462. attributes_arguments["order"],
  2463. ) = _determine_attrs_eq_order(
  2464. cmp,
  2465. attributes_arguments.get("eq"),
  2466. attributes_arguments.get("order"),
  2467. True,
  2468. )
  2469. return _attrs(these=cls_dict, **attributes_arguments)(type_)
  2470. # These are required by within this module so we define them here and merely
  2471. # import into .validators / .converters.
  2472. @attrs(slots=True, hash=True)
  2473. class _AndValidator(object):
  2474. """
  2475. Compose many validators to a single one.
  2476. """
  2477. _validators = attrib()
  2478. def __call__(self, inst, attr, value):
  2479. for v in self._validators:
  2480. v(inst, attr, value)
  2481. def and_(*validators):
  2482. """
  2483. A validator that composes multiple validators into one.
  2484. When called on a value, it runs all wrapped validators.
  2485. :param callables validators: Arbitrary number of validators.
  2486. .. versionadded:: 17.1.0
  2487. """
  2488. vals = []
  2489. for validator in validators:
  2490. vals.extend(
  2491. validator._validators
  2492. if isinstance(validator, _AndValidator)
  2493. else [validator]
  2494. )
  2495. return _AndValidator(tuple(vals))
  2496. def pipe(*converters):
  2497. """
  2498. A converter that composes multiple converters into one.
  2499. When called on a value, it runs all wrapped converters, returning the
  2500. *last* value.
  2501. Type annotations will be inferred from the wrapped converters', if
  2502. they have any.
  2503. :param callables converters: Arbitrary number of converters.
  2504. .. versionadded:: 20.1.0
  2505. """
  2506. def pipe_converter(val):
  2507. for converter in converters:
  2508. val = converter(val)
  2509. return val
  2510. if not PY2:
  2511. if not converters:
  2512. # If the converter list is empty, pipe_converter is the identity.
  2513. A = typing.TypeVar("A")
  2514. pipe_converter.__annotations__ = {"val": A, "return": A}
  2515. else:
  2516. # Get parameter type.
  2517. sig = None
  2518. try:
  2519. sig = inspect.signature(converters[0])
  2520. except (ValueError, TypeError): # inspect failed
  2521. pass
  2522. if sig:
  2523. params = list(sig.parameters.values())
  2524. if (
  2525. params
  2526. and params[0].annotation is not inspect.Parameter.empty
  2527. ):
  2528. pipe_converter.__annotations__["val"] = params[
  2529. 0
  2530. ].annotation
  2531. # Get return type.
  2532. sig = None
  2533. try:
  2534. sig = inspect.signature(converters[-1])
  2535. except (ValueError, TypeError): # inspect failed
  2536. pass
  2537. if sig and sig.return_annotation is not inspect.Signature().empty:
  2538. pipe_converter.__annotations__[
  2539. "return"
  2540. ] = sig.return_annotation
  2541. return pipe_converter