You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

469 rivejä
16 KiB

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