_funcs.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. # SPDX-License-Identifier: MIT
  2. import copy
  3. from ._compat import get_generic_base
  4. from ._make import _OBJ_SETATTR, NOTHING, fields
  5. from .exceptions import AttrsAttributeNotFoundError
  6. _ATOMIC_TYPES = frozenset(
  7. {
  8. type(None),
  9. bool,
  10. int,
  11. float,
  12. str,
  13. complex,
  14. bytes,
  15. type(...),
  16. type,
  17. range,
  18. property,
  19. }
  20. )
  21. def asdict(
  22. inst,
  23. recurse=True,
  24. filter=None,
  25. dict_factory=dict,
  26. retain_collection_types=False,
  27. value_serializer=None,
  28. ):
  29. """
  30. Return the *attrs* attribute values of *inst* as a dict.
  31. Optionally recurse into other *attrs*-decorated classes.
  32. Args:
  33. inst: Instance of an *attrs*-decorated class.
  34. recurse (bool): Recurse into classes that are also *attrs*-decorated.
  35. filter (~typing.Callable):
  36. A callable whose return code determines whether an attribute or
  37. element is included (`True`) or dropped (`False`). Is called with
  38. the `attrs.Attribute` as the first argument and the value as the
  39. second argument.
  40. dict_factory (~typing.Callable):
  41. A callable to produce dictionaries from. For example, to produce
  42. ordered dictionaries instead of normal Python dictionaries, pass in
  43. ``collections.OrderedDict``.
  44. retain_collection_types (bool):
  45. Do not convert to `list` when encountering an attribute whose type
  46. is `tuple` or `set`. Only meaningful if *recurse* is `True`.
  47. value_serializer (typing.Callable | None):
  48. A hook that is called for every attribute or dict key/value. It
  49. receives the current instance, field and value and must return the
  50. (updated) value. The hook is run *after* the optional *filter* has
  51. been applied.
  52. Returns:
  53. Return type of *dict_factory*.
  54. Raises:
  55. attrs.exceptions.NotAnAttrsClassError:
  56. If *cls* is not an *attrs* class.
  57. .. versionadded:: 16.0.0 *dict_factory*
  58. .. versionadded:: 16.1.0 *retain_collection_types*
  59. .. versionadded:: 20.3.0 *value_serializer*
  60. .. versionadded:: 21.3.0
  61. If a dict has a collection for a key, it is serialized as a tuple.
  62. """
  63. attrs = fields(inst.__class__)
  64. rv = dict_factory()
  65. for a in attrs:
  66. v = getattr(inst, a.name)
  67. if filter is not None and not filter(a, v):
  68. continue
  69. if value_serializer is not None:
  70. v = value_serializer(inst, a, v)
  71. if recurse is True:
  72. value_type = type(v)
  73. if value_type in _ATOMIC_TYPES:
  74. rv[a.name] = v
  75. elif has(value_type):
  76. rv[a.name] = asdict(
  77. v,
  78. recurse=True,
  79. filter=filter,
  80. dict_factory=dict_factory,
  81. retain_collection_types=retain_collection_types,
  82. value_serializer=value_serializer,
  83. )
  84. elif issubclass(value_type, (tuple, list, set, frozenset)):
  85. cf = value_type if retain_collection_types is True else list
  86. items = [
  87. _asdict_anything(
  88. i,
  89. is_key=False,
  90. filter=filter,
  91. dict_factory=dict_factory,
  92. retain_collection_types=retain_collection_types,
  93. value_serializer=value_serializer,
  94. )
  95. for i in v
  96. ]
  97. try:
  98. rv[a.name] = cf(items)
  99. except TypeError:
  100. if not issubclass(cf, tuple):
  101. raise
  102. # Workaround for TypeError: cf.__new__() missing 1 required
  103. # positional argument (which appears, for a namedturle)
  104. rv[a.name] = cf(*items)
  105. elif issubclass(value_type, dict):
  106. df = dict_factory
  107. rv[a.name] = df(
  108. (
  109. _asdict_anything(
  110. kk,
  111. is_key=True,
  112. filter=filter,
  113. dict_factory=df,
  114. retain_collection_types=retain_collection_types,
  115. value_serializer=value_serializer,
  116. ),
  117. _asdict_anything(
  118. vv,
  119. is_key=False,
  120. filter=filter,
  121. dict_factory=df,
  122. retain_collection_types=retain_collection_types,
  123. value_serializer=value_serializer,
  124. ),
  125. )
  126. for kk, vv in v.items()
  127. )
  128. else:
  129. rv[a.name] = v
  130. else:
  131. rv[a.name] = v
  132. return rv
  133. def _asdict_anything(
  134. val,
  135. is_key,
  136. filter,
  137. dict_factory,
  138. retain_collection_types,
  139. value_serializer,
  140. ):
  141. """
  142. ``asdict`` only works on attrs instances, this works on anything.
  143. """
  144. val_type = type(val)
  145. if val_type in _ATOMIC_TYPES:
  146. rv = val
  147. if value_serializer is not None:
  148. rv = value_serializer(None, None, rv)
  149. elif getattr(val_type, "__attrs_attrs__", None) is not None:
  150. # Attrs class.
  151. rv = asdict(
  152. val,
  153. recurse=True,
  154. filter=filter,
  155. dict_factory=dict_factory,
  156. retain_collection_types=retain_collection_types,
  157. value_serializer=value_serializer,
  158. )
  159. elif issubclass(val_type, (tuple, list, set, frozenset)):
  160. if retain_collection_types is True:
  161. cf = val.__class__
  162. elif is_key:
  163. cf = tuple
  164. else:
  165. cf = list
  166. rv = cf(
  167. [
  168. _asdict_anything(
  169. i,
  170. is_key=False,
  171. filter=filter,
  172. dict_factory=dict_factory,
  173. retain_collection_types=retain_collection_types,
  174. value_serializer=value_serializer,
  175. )
  176. for i in val
  177. ]
  178. )
  179. elif issubclass(val_type, dict):
  180. df = dict_factory
  181. rv = df(
  182. (
  183. _asdict_anything(
  184. kk,
  185. is_key=True,
  186. filter=filter,
  187. dict_factory=df,
  188. retain_collection_types=retain_collection_types,
  189. value_serializer=value_serializer,
  190. ),
  191. _asdict_anything(
  192. vv,
  193. is_key=False,
  194. filter=filter,
  195. dict_factory=df,
  196. retain_collection_types=retain_collection_types,
  197. value_serializer=value_serializer,
  198. ),
  199. )
  200. for kk, vv in val.items()
  201. )
  202. else:
  203. rv = val
  204. if value_serializer is not None:
  205. rv = value_serializer(None, None, rv)
  206. return rv
  207. def astuple(
  208. inst,
  209. recurse=True,
  210. filter=None,
  211. tuple_factory=tuple,
  212. retain_collection_types=False,
  213. ):
  214. """
  215. Return the *attrs* attribute values of *inst* as a tuple.
  216. Optionally recurse into other *attrs*-decorated classes.
  217. Args:
  218. inst: Instance of an *attrs*-decorated class.
  219. recurse (bool):
  220. Recurse into classes that are also *attrs*-decorated.
  221. filter (~typing.Callable):
  222. A callable whose return code determines whether an attribute or
  223. element is included (`True`) or dropped (`False`). Is called with
  224. the `attrs.Attribute` as the first argument and the value as the
  225. second argument.
  226. tuple_factory (~typing.Callable):
  227. A callable to produce tuples from. For example, to produce lists
  228. instead of tuples.
  229. retain_collection_types (bool):
  230. Do not convert to `list` or `dict` when encountering an attribute
  231. which type is `tuple`, `dict` or `set`. Only meaningful if
  232. *recurse* is `True`.
  233. Returns:
  234. Return type of *tuple_factory*
  235. Raises:
  236. attrs.exceptions.NotAnAttrsClassError:
  237. If *cls* is not an *attrs* class.
  238. .. versionadded:: 16.2.0
  239. """
  240. attrs = fields(inst.__class__)
  241. rv = []
  242. retain = retain_collection_types # Very long. :/
  243. for a in attrs:
  244. v = getattr(inst, a.name)
  245. if filter is not None and not filter(a, v):
  246. continue
  247. value_type = type(v)
  248. if recurse is True:
  249. if value_type in _ATOMIC_TYPES:
  250. rv.append(v)
  251. elif has(value_type):
  252. rv.append(
  253. astuple(
  254. v,
  255. recurse=True,
  256. filter=filter,
  257. tuple_factory=tuple_factory,
  258. retain_collection_types=retain,
  259. )
  260. )
  261. elif issubclass(value_type, (tuple, list, set, frozenset)):
  262. cf = v.__class__ if retain is True else list
  263. items = [
  264. (
  265. astuple(
  266. j,
  267. recurse=True,
  268. filter=filter,
  269. tuple_factory=tuple_factory,
  270. retain_collection_types=retain,
  271. )
  272. if has(j.__class__)
  273. else j
  274. )
  275. for j in v
  276. ]
  277. try:
  278. rv.append(cf(items))
  279. except TypeError:
  280. if not issubclass(cf, tuple):
  281. raise
  282. # Workaround for TypeError: cf.__new__() missing 1 required
  283. # positional argument (which appears, for a namedturle)
  284. rv.append(cf(*items))
  285. elif issubclass(value_type, dict):
  286. df = value_type if retain is True else dict
  287. rv.append(
  288. df(
  289. (
  290. (
  291. astuple(
  292. kk,
  293. tuple_factory=tuple_factory,
  294. retain_collection_types=retain,
  295. )
  296. if has(kk.__class__)
  297. else kk
  298. ),
  299. (
  300. astuple(
  301. vv,
  302. tuple_factory=tuple_factory,
  303. retain_collection_types=retain,
  304. )
  305. if has(vv.__class__)
  306. else vv
  307. ),
  308. )
  309. for kk, vv in v.items()
  310. )
  311. )
  312. else:
  313. rv.append(v)
  314. else:
  315. rv.append(v)
  316. return rv if tuple_factory is list else tuple_factory(rv)
  317. def has(cls):
  318. """
  319. Check whether *cls* is a class with *attrs* attributes.
  320. Args:
  321. cls (type): Class to introspect.
  322. Raises:
  323. TypeError: If *cls* is not a class.
  324. Returns:
  325. bool:
  326. """
  327. attrs = getattr(cls, "__attrs_attrs__", None)
  328. if attrs is not None:
  329. return True
  330. # No attrs, maybe it's a specialized generic (A[str])?
  331. generic_base = get_generic_base(cls)
  332. if generic_base is not None:
  333. generic_attrs = getattr(generic_base, "__attrs_attrs__", None)
  334. if generic_attrs is not None:
  335. # Stick it on here for speed next time.
  336. cls.__attrs_attrs__ = generic_attrs
  337. return generic_attrs is not None
  338. return False
  339. def assoc(inst, **changes):
  340. """
  341. Copy *inst* and apply *changes*.
  342. This is different from `evolve` that applies the changes to the arguments
  343. that create the new instance.
  344. `evolve`'s behavior is preferable, but there are `edge cases`_ where it
  345. doesn't work. Therefore `assoc` is deprecated, but will not be removed.
  346. .. _`edge cases`: https://github.com/python-attrs/attrs/issues/251
  347. Args:
  348. inst: Instance of a class with *attrs* attributes.
  349. changes: Keyword changes in the new copy.
  350. Returns:
  351. A copy of inst with *changes* incorporated.
  352. Raises:
  353. attrs.exceptions.AttrsAttributeNotFoundError:
  354. If *attr_name* couldn't be found on *cls*.
  355. attrs.exceptions.NotAnAttrsClassError:
  356. If *cls* is not an *attrs* class.
  357. .. deprecated:: 17.1.0
  358. Use `attrs.evolve` instead if you can. This function will not be
  359. removed du to the slightly different approach compared to
  360. `attrs.evolve`, though.
  361. """
  362. new = copy.copy(inst)
  363. attrs = fields(inst.__class__)
  364. for k, v in changes.items():
  365. a = getattr(attrs, k, NOTHING)
  366. if a is NOTHING:
  367. msg = f"{k} is not an attrs attribute on {new.__class__}."
  368. raise AttrsAttributeNotFoundError(msg)
  369. _OBJ_SETATTR(new, k, v)
  370. return new
  371. def resolve_types(
  372. cls, globalns=None, localns=None, attribs=None, include_extras=True
  373. ):
  374. """
  375. Resolve any strings and forward annotations in type annotations.
  376. This is only required if you need concrete types in :class:`Attribute`'s
  377. *type* field. In other words, you don't need to resolve your types if you
  378. only use them for static type checking.
  379. With no arguments, names will be looked up in the module in which the class
  380. was created. If this is not what you want, for example, if the name only
  381. exists inside a method, you may pass *globalns* or *localns* to specify
  382. other dictionaries in which to look up these names. See the docs of
  383. `typing.get_type_hints` for more details.
  384. Args:
  385. cls (type): Class to resolve.
  386. globalns (dict | None): Dictionary containing global variables.
  387. localns (dict | None): Dictionary containing local variables.
  388. attribs (list | None):
  389. List of attribs for the given class. This is necessary when calling
  390. from inside a ``field_transformer`` since *cls* is not an *attrs*
  391. class yet.
  392. include_extras (bool):
  393. Resolve more accurately, if possible. Pass ``include_extras`` to
  394. ``typing.get_hints``, if supported by the typing module. On
  395. supported Python versions (3.9+), this resolves the types more
  396. accurately.
  397. Raises:
  398. TypeError: If *cls* is not a class.
  399. attrs.exceptions.NotAnAttrsClassError:
  400. If *cls* is not an *attrs* class and you didn't pass any attribs.
  401. NameError: If types cannot be resolved because of missing variables.
  402. Returns:
  403. *cls* so you can use this function also as a class decorator. Please
  404. note that you have to apply it **after** `attrs.define`. That means the
  405. decorator has to come in the line **before** `attrs.define`.
  406. .. versionadded:: 20.1.0
  407. .. versionadded:: 21.1.0 *attribs*
  408. .. versionadded:: 23.1.0 *include_extras*
  409. """
  410. # Since calling get_type_hints is expensive we cache whether we've
  411. # done it already.
  412. if getattr(cls, "__attrs_types_resolved__", None) != cls:
  413. import typing
  414. kwargs = {
  415. "globalns": globalns,
  416. "localns": localns,
  417. "include_extras": include_extras,
  418. }
  419. hints = typing.get_type_hints(cls, **kwargs)
  420. for field in fields(cls) if attribs is None else attribs:
  421. if field.name in hints:
  422. # Since fields have been frozen we must work around it.
  423. _OBJ_SETATTR(field, "type", hints[field.name])
  424. # We store the class we resolved so that subclasses know they haven't
  425. # been resolved.
  426. cls.__attrs_types_resolved__ = cls
  427. # Return the class so you can use it as a decorator too.
  428. return cls