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.

2034 lines
64 KiB

4 years ago
  1. from __future__ import absolute_import, division, print_function
  2. import copy
  3. import hashlib
  4. import linecache
  5. import sys
  6. import threading
  7. import warnings
  8. from operator import itemgetter
  9. from . import _config
  10. from ._compat import (
  11. PY2,
  12. isclass,
  13. iteritems,
  14. metadata_proxy,
  15. ordered_dict,
  16. set_closure_cell,
  17. )
  18. from .exceptions import (
  19. DefaultAlreadySetError,
  20. FrozenInstanceError,
  21. NotAnAttrsClassError,
  22. PythonTooOldError,
  23. UnannotatedAttributeError,
  24. )
  25. # This is used at least twice, so cache it here.
  26. _obj_setattr = object.__setattr__
  27. _init_converter_pat = "__attr_converter_{}"
  28. _init_factory_pat = "__attr_factory_{}"
  29. _tuple_property_pat = (
  30. " {attr_name} = _attrs_property(_attrs_itemgetter({index}))"
  31. )
  32. _classvar_prefixes = ("typing.ClassVar", "t.ClassVar", "ClassVar")
  33. # we don't use a double-underscore prefix because that triggers
  34. # name mangling when trying to create a slot for the field
  35. # (when slots=True)
  36. _hash_cache_field = "_attrs_cached_hash"
  37. _empty_metadata_singleton = metadata_proxy({})
  38. class _Nothing(object):
  39. """
  40. Sentinel class to indicate the lack of a value when ``None`` is ambiguous.
  41. ``_Nothing`` is a singleton. There is only ever one of it.
  42. """
  43. _singleton = None
  44. def __new__(cls):
  45. if _Nothing._singleton is None:
  46. _Nothing._singleton = super(_Nothing, cls).__new__(cls)
  47. return _Nothing._singleton
  48. def __repr__(self):
  49. return "NOTHING"
  50. NOTHING = _Nothing()
  51. """
  52. Sentinel to indicate the lack of a value when ``None`` is ambiguous.
  53. """
  54. def attrib(
  55. default=NOTHING,
  56. validator=None,
  57. repr=True,
  58. cmp=True,
  59. hash=None,
  60. init=True,
  61. convert=None,
  62. metadata=None,
  63. type=None,
  64. converter=None,
  65. factory=None,
  66. kw_only=False,
  67. ):
  68. """
  69. Create a new attribute on a class.
  70. .. warning::
  71. Does *not* do anything unless the class is also decorated with
  72. :func:`attr.s`!
  73. :param default: A value that is used if an ``attrs``-generated ``__init__``
  74. is used and no value is passed while instantiating or the attribute is
  75. excluded using ``init=False``.
  76. If the value is an instance of :class:`Factory`, its callable will be
  77. used to construct a new value (useful for mutable data types like lists
  78. or dicts).
  79. If a default is not set (or set manually to ``attr.NOTHING``), a value
  80. *must* be supplied when instantiating; otherwise a :exc:`TypeError`
  81. will be raised.
  82. The default can also be set using decorator notation as shown below.
  83. :type default: Any value.
  84. :param callable factory: Syntactic sugar for
  85. ``default=attr.Factory(callable)``.
  86. :param validator: :func:`callable` that is called by ``attrs``-generated
  87. ``__init__`` methods after the instance has been initialized. They
  88. receive the initialized instance, the :class:`Attribute`, and the
  89. passed value.
  90. The return value is *not* inspected so the validator has to throw an
  91. exception itself.
  92. If a ``list`` is passed, its items are treated as validators and must
  93. all pass.
  94. Validators can be globally disabled and re-enabled using
  95. :func:`get_run_validators`.
  96. The validator can also be set using decorator notation as shown below.
  97. :type validator: ``callable`` or a ``list`` of ``callable``\\ s.
  98. :param bool repr: Include this attribute in the generated ``__repr__``
  99. method.
  100. :param bool cmp: Include this attribute in the generated comparison methods
  101. (``__eq__`` et al).
  102. :param hash: Include this attribute in the generated ``__hash__``
  103. method. If ``None`` (default), mirror *cmp*'s value. This is the
  104. correct behavior according the Python spec. Setting this value to
  105. anything else than ``None`` is *discouraged*.
  106. :type hash: ``bool`` or ``None``
  107. :param bool init: Include this attribute in the generated ``__init__``
  108. method. It is possible to set this to ``False`` and set a default
  109. value. In that case this attributed is unconditionally initialized
  110. with the specified default value or factory.
  111. :param callable converter: :func:`callable` that is called by
  112. ``attrs``-generated ``__init__`` methods to converter attribute's value
  113. to the desired format. It is given the passed-in value, and the
  114. returned value will be used as the new value of the attribute. The
  115. value is converted before being passed to the validator, if any.
  116. :param metadata: An arbitrary mapping, to be used by third-party
  117. components. See :ref:`extending_metadata`.
  118. :param type: The type of the attribute. In Python 3.6 or greater, the
  119. preferred method to specify the type is using a variable annotation
  120. (see `PEP 526 <https://www.python.org/dev/peps/pep-0526/>`_).
  121. This argument is provided for backward compatibility.
  122. Regardless of the approach used, the type will be stored on
  123. ``Attribute.type``.
  124. Please note that ``attrs`` doesn't do anything with this metadata by
  125. itself. You can use it as part of your own code or for
  126. :doc:`static type checking <types>`.
  127. :param kw_only: Make this attribute keyword-only (Python 3+)
  128. in the generated ``__init__`` (if ``init`` is ``False``, this
  129. parameter is ignored).
  130. .. versionadded:: 15.2.0 *convert*
  131. .. versionadded:: 16.3.0 *metadata*
  132. .. versionchanged:: 17.1.0 *validator* can be a ``list`` now.
  133. .. versionchanged:: 17.1.0
  134. *hash* is ``None`` and therefore mirrors *cmp* by default.
  135. .. versionadded:: 17.3.0 *type*
  136. .. deprecated:: 17.4.0 *convert*
  137. .. versionadded:: 17.4.0 *converter* as a replacement for the deprecated
  138. *convert* to achieve consistency with other noun-based arguments.
  139. .. versionadded:: 18.1.0
  140. ``factory=f`` is syntactic sugar for ``default=attr.Factory(f)``.
  141. .. versionadded:: 18.2.0 *kw_only*
  142. """
  143. if hash is not None and hash is not True and hash is not False:
  144. raise TypeError(
  145. "Invalid value for hash. Must be True, False, or None."
  146. )
  147. if convert is not None:
  148. if converter is not None:
  149. raise RuntimeError(
  150. "Can't pass both `convert` and `converter`. "
  151. "Please use `converter` only."
  152. )
  153. warnings.warn(
  154. "The `convert` argument is deprecated in favor of `converter`. "
  155. "It will be removed after 2019/01.",
  156. DeprecationWarning,
  157. stacklevel=2,
  158. )
  159. converter = convert
  160. if factory is not None:
  161. if default is not NOTHING:
  162. raise ValueError(
  163. "The `default` and `factory` arguments are mutually "
  164. "exclusive."
  165. )
  166. if not callable(factory):
  167. raise ValueError("The `factory` argument must be a callable.")
  168. default = Factory(factory)
  169. if metadata is None:
  170. metadata = {}
  171. return _CountingAttr(
  172. default=default,
  173. validator=validator,
  174. repr=repr,
  175. cmp=cmp,
  176. hash=hash,
  177. init=init,
  178. converter=converter,
  179. metadata=metadata,
  180. type=type,
  181. kw_only=kw_only,
  182. )
  183. def _make_attr_tuple_class(cls_name, attr_names):
  184. """
  185. Create a tuple subclass to hold `Attribute`s for an `attrs` class.
  186. The subclass is a bare tuple with properties for names.
  187. class MyClassAttributes(tuple):
  188. __slots__ = ()
  189. x = property(itemgetter(0))
  190. """
  191. attr_class_name = "{}Attributes".format(cls_name)
  192. attr_class_template = [
  193. "class {}(tuple):".format(attr_class_name),
  194. " __slots__ = ()",
  195. ]
  196. if attr_names:
  197. for i, attr_name in enumerate(attr_names):
  198. attr_class_template.append(
  199. _tuple_property_pat.format(index=i, attr_name=attr_name)
  200. )
  201. else:
  202. attr_class_template.append(" pass")
  203. globs = {"_attrs_itemgetter": itemgetter, "_attrs_property": property}
  204. eval(compile("\n".join(attr_class_template), "", "exec"), globs)
  205. return globs[attr_class_name]
  206. # Tuple class for extracted attributes from a class definition.
  207. # `base_attrs` is a subset of `attrs`.
  208. _Attributes = _make_attr_tuple_class(
  209. "_Attributes",
  210. [
  211. # all attributes to build dunder methods for
  212. "attrs",
  213. # attributes that have been inherited
  214. "base_attrs",
  215. # map inherited attributes to their originating classes
  216. "base_attrs_map",
  217. ],
  218. )
  219. def _is_class_var(annot):
  220. """
  221. Check whether *annot* is a typing.ClassVar.
  222. The string comparison hack is used to avoid evaluating all string
  223. annotations which would put attrs-based classes at a performance
  224. disadvantage compared to plain old classes.
  225. """
  226. return str(annot).startswith(_classvar_prefixes)
  227. def _get_annotations(cls):
  228. """
  229. Get annotations for *cls*.
  230. """
  231. anns = getattr(cls, "__annotations__", None)
  232. if anns is None:
  233. return {}
  234. # Verify that the annotations aren't merely inherited.
  235. for base_cls in cls.__mro__[1:]:
  236. if anns is getattr(base_cls, "__annotations__", None):
  237. return {}
  238. return anns
  239. def _counter_getter(e):
  240. """
  241. Key function for sorting to avoid re-creating a lambda for every class.
  242. """
  243. return e[1].counter
  244. def _transform_attrs(cls, these, auto_attribs, kw_only):
  245. """
  246. Transform all `_CountingAttr`s on a class into `Attribute`s.
  247. If *these* is passed, use that and don't look for them on the class.
  248. Return an `_Attributes`.
  249. """
  250. cd = cls.__dict__
  251. anns = _get_annotations(cls)
  252. if these is not None:
  253. ca_list = [(name, ca) for name, ca in iteritems(these)]
  254. if not isinstance(these, ordered_dict):
  255. ca_list.sort(key=_counter_getter)
  256. elif auto_attribs is True:
  257. ca_names = {
  258. name
  259. for name, attr in cd.items()
  260. if isinstance(attr, _CountingAttr)
  261. }
  262. ca_list = []
  263. annot_names = set()
  264. for attr_name, type in anns.items():
  265. if _is_class_var(type):
  266. continue
  267. annot_names.add(attr_name)
  268. a = cd.get(attr_name, NOTHING)
  269. if not isinstance(a, _CountingAttr):
  270. if a is NOTHING:
  271. a = attrib()
  272. else:
  273. a = attrib(default=a)
  274. ca_list.append((attr_name, a))
  275. unannotated = ca_names - annot_names
  276. if len(unannotated) > 0:
  277. raise UnannotatedAttributeError(
  278. "The following `attr.ib`s lack a type annotation: "
  279. + ", ".join(
  280. sorted(unannotated, key=lambda n: cd.get(n).counter)
  281. )
  282. + "."
  283. )
  284. else:
  285. ca_list = sorted(
  286. (
  287. (name, attr)
  288. for name, attr in cd.items()
  289. if isinstance(attr, _CountingAttr)
  290. ),
  291. key=lambda e: e[1].counter,
  292. )
  293. own_attrs = [
  294. Attribute.from_counting_attr(
  295. name=attr_name, ca=ca, type=anns.get(attr_name)
  296. )
  297. for attr_name, ca in ca_list
  298. ]
  299. base_attrs = []
  300. base_attr_map = {} # A dictionary of base attrs to their classes.
  301. taken_attr_names = {a.name: a for a in own_attrs}
  302. # Traverse the MRO and collect attributes.
  303. for base_cls in cls.__mro__[1:-1]:
  304. sub_attrs = getattr(base_cls, "__attrs_attrs__", None)
  305. if sub_attrs is not None:
  306. for a in sub_attrs:
  307. prev_a = taken_attr_names.get(a.name)
  308. # Only add an attribute if it hasn't been defined before. This
  309. # allows for overwriting attribute definitions by subclassing.
  310. if prev_a is None:
  311. base_attrs.append(a)
  312. taken_attr_names[a.name] = a
  313. base_attr_map[a.name] = base_cls
  314. attr_names = [a.name for a in base_attrs + own_attrs]
  315. AttrsClass = _make_attr_tuple_class(cls.__name__, attr_names)
  316. if kw_only:
  317. own_attrs = [a._assoc(kw_only=True) for a in own_attrs]
  318. base_attrs = [a._assoc(kw_only=True) for a in base_attrs]
  319. attrs = AttrsClass(base_attrs + own_attrs)
  320. had_default = False
  321. was_kw_only = False
  322. for a in attrs:
  323. if (
  324. was_kw_only is False
  325. and had_default is True
  326. and a.default is NOTHING
  327. and a.init is True
  328. and a.kw_only is False
  329. ):
  330. raise ValueError(
  331. "No mandatory attributes allowed after an attribute with a "
  332. "default value or factory. Attribute in question: %r" % (a,)
  333. )
  334. elif (
  335. had_default is False
  336. and a.default is not NOTHING
  337. and a.init is not False
  338. and
  339. # Keyword-only attributes without defaults can be specified
  340. # after keyword-only attributes with defaults.
  341. a.kw_only is False
  342. ):
  343. had_default = True
  344. if was_kw_only is True and a.kw_only is False:
  345. raise ValueError(
  346. "Non keyword-only attributes are not allowed after a "
  347. "keyword-only attribute. Attribute in question: {a!r}".format(
  348. a=a
  349. )
  350. )
  351. if was_kw_only is False and a.init is True and a.kw_only is True:
  352. was_kw_only = True
  353. return _Attributes((attrs, base_attrs, base_attr_map))
  354. def _frozen_setattrs(self, name, value):
  355. """
  356. Attached to frozen classes as __setattr__.
  357. """
  358. raise FrozenInstanceError()
  359. def _frozen_delattrs(self, name):
  360. """
  361. Attached to frozen classes as __delattr__.
  362. """
  363. raise FrozenInstanceError()
  364. class _ClassBuilder(object):
  365. """
  366. Iteratively build *one* class.
  367. """
  368. __slots__ = (
  369. "_cls",
  370. "_cls_dict",
  371. "_attrs",
  372. "_base_names",
  373. "_attr_names",
  374. "_slots",
  375. "_frozen",
  376. "_weakref_slot",
  377. "_cache_hash",
  378. "_has_post_init",
  379. "_delete_attribs",
  380. "_base_attr_map",
  381. )
  382. def __init__(
  383. self,
  384. cls,
  385. these,
  386. slots,
  387. frozen,
  388. weakref_slot,
  389. auto_attribs,
  390. kw_only,
  391. cache_hash,
  392. ):
  393. attrs, base_attrs, base_map = _transform_attrs(
  394. cls, these, auto_attribs, kw_only
  395. )
  396. self._cls = cls
  397. self._cls_dict = dict(cls.__dict__) if slots else {}
  398. self._attrs = attrs
  399. self._base_names = set(a.name for a in base_attrs)
  400. self._base_attr_map = base_map
  401. self._attr_names = tuple(a.name for a in attrs)
  402. self._slots = slots
  403. self._frozen = frozen or _has_frozen_base_class(cls)
  404. self._weakref_slot = weakref_slot
  405. self._cache_hash = cache_hash
  406. self._has_post_init = bool(getattr(cls, "__attrs_post_init__", False))
  407. self._delete_attribs = not bool(these)
  408. self._cls_dict["__attrs_attrs__"] = self._attrs
  409. if frozen:
  410. self._cls_dict["__setattr__"] = _frozen_setattrs
  411. self._cls_dict["__delattr__"] = _frozen_delattrs
  412. def __repr__(self):
  413. return "<_ClassBuilder(cls={cls})>".format(cls=self._cls.__name__)
  414. def build_class(self):
  415. """
  416. Finalize class based on the accumulated configuration.
  417. Builder cannot be used after calling this method.
  418. """
  419. if self._slots is True:
  420. return self._create_slots_class()
  421. else:
  422. return self._patch_original_class()
  423. def _patch_original_class(self):
  424. """
  425. Apply accumulated methods and return the class.
  426. """
  427. cls = self._cls
  428. base_names = self._base_names
  429. # Clean class of attribute definitions (`attr.ib()`s).
  430. if self._delete_attribs:
  431. for name in self._attr_names:
  432. if (
  433. name not in base_names
  434. and getattr(cls, name, None) is not None
  435. ):
  436. try:
  437. delattr(cls, name)
  438. except AttributeError:
  439. # This can happen if a base class defines a class
  440. # variable and we want to set an attribute with the
  441. # same name by using only a type annotation.
  442. pass
  443. # Attach our dunder methods.
  444. for name, value in self._cls_dict.items():
  445. setattr(cls, name, value)
  446. return cls
  447. def _create_slots_class(self):
  448. """
  449. Build and return a new class with a `__slots__` attribute.
  450. """
  451. base_names = self._base_names
  452. cd = {
  453. k: v
  454. for k, v in iteritems(self._cls_dict)
  455. if k not in tuple(self._attr_names) + ("__dict__", "__weakref__")
  456. }
  457. weakref_inherited = False
  458. # Traverse the MRO to check for an existing __weakref__.
  459. for base_cls in self._cls.__mro__[1:-1]:
  460. if "__weakref__" in getattr(base_cls, "__dict__", ()):
  461. weakref_inherited = True
  462. break
  463. names = self._attr_names
  464. if (
  465. self._weakref_slot
  466. and "__weakref__" not in getattr(self._cls, "__slots__", ())
  467. and "__weakref__" not in names
  468. and not weakref_inherited
  469. ):
  470. names += ("__weakref__",)
  471. # We only add the names of attributes that aren't inherited.
  472. # Settings __slots__ to inherited attributes wastes memory.
  473. slot_names = [name for name in names if name not in base_names]
  474. if self._cache_hash:
  475. slot_names.append(_hash_cache_field)
  476. cd["__slots__"] = tuple(slot_names)
  477. qualname = getattr(self._cls, "__qualname__", None)
  478. if qualname is not None:
  479. cd["__qualname__"] = qualname
  480. # __weakref__ is not writable.
  481. state_attr_names = tuple(
  482. an for an in self._attr_names if an != "__weakref__"
  483. )
  484. def slots_getstate(self):
  485. """
  486. Automatically created by attrs.
  487. """
  488. return tuple(getattr(self, name) for name in state_attr_names)
  489. def slots_setstate(self, state):
  490. """
  491. Automatically created by attrs.
  492. """
  493. __bound_setattr = _obj_setattr.__get__(self, Attribute)
  494. for name, value in zip(state_attr_names, state):
  495. __bound_setattr(name, value)
  496. # slots and frozen require __getstate__/__setstate__ to work
  497. cd["__getstate__"] = slots_getstate
  498. cd["__setstate__"] = slots_setstate
  499. # Create new class based on old class and our methods.
  500. cls = type(self._cls)(self._cls.__name__, self._cls.__bases__, cd)
  501. # The following is a fix for
  502. # https://github.com/python-attrs/attrs/issues/102. On Python 3,
  503. # if a method mentions `__class__` or uses the no-arg super(), the
  504. # compiler will bake a reference to the class in the method itself
  505. # as `method.__closure__`. Since we replace the class with a
  506. # clone, we rewrite these references so it keeps working.
  507. for item in cls.__dict__.values():
  508. if isinstance(item, (classmethod, staticmethod)):
  509. # Class- and staticmethods hide their functions inside.
  510. # These might need to be rewritten as well.
  511. closure_cells = getattr(item.__func__, "__closure__", None)
  512. else:
  513. closure_cells = getattr(item, "__closure__", None)
  514. if not closure_cells: # Catch None or the empty list.
  515. continue
  516. for cell in closure_cells:
  517. if cell.cell_contents is self._cls:
  518. set_closure_cell(cell, cls)
  519. return cls
  520. def add_repr(self, ns):
  521. self._cls_dict["__repr__"] = self._add_method_dunders(
  522. _make_repr(self._attrs, ns=ns)
  523. )
  524. return self
  525. def add_str(self):
  526. repr = self._cls_dict.get("__repr__")
  527. if repr is None:
  528. raise ValueError(
  529. "__str__ can only be generated if a __repr__ exists."
  530. )
  531. def __str__(self):
  532. return self.__repr__()
  533. self._cls_dict["__str__"] = self._add_method_dunders(__str__)
  534. return self
  535. def make_unhashable(self):
  536. self._cls_dict["__hash__"] = None
  537. return self
  538. def add_hash(self):
  539. self._cls_dict["__hash__"] = self._add_method_dunders(
  540. _make_hash(
  541. self._attrs, frozen=self._frozen, cache_hash=self._cache_hash
  542. )
  543. )
  544. return self
  545. def add_init(self):
  546. self._cls_dict["__init__"] = self._add_method_dunders(
  547. _make_init(
  548. self._attrs,
  549. self._has_post_init,
  550. self._frozen,
  551. self._slots,
  552. self._cache_hash,
  553. self._base_attr_map,
  554. )
  555. )
  556. return self
  557. def add_cmp(self):
  558. cd = self._cls_dict
  559. cd["__eq__"], cd["__ne__"], cd["__lt__"], cd["__le__"], cd[
  560. "__gt__"
  561. ], cd["__ge__"] = (
  562. self._add_method_dunders(meth) for meth in _make_cmp(self._attrs)
  563. )
  564. return self
  565. def _add_method_dunders(self, method):
  566. """
  567. Add __module__ and __qualname__ to a *method* if possible.
  568. """
  569. try:
  570. method.__module__ = self._cls.__module__
  571. except AttributeError:
  572. pass
  573. try:
  574. method.__qualname__ = ".".join(
  575. (self._cls.__qualname__, method.__name__)
  576. )
  577. except AttributeError:
  578. pass
  579. return method
  580. def attrs(
  581. maybe_cls=None,
  582. these=None,
  583. repr_ns=None,
  584. repr=True,
  585. cmp=True,
  586. hash=None,
  587. init=True,
  588. slots=False,
  589. frozen=False,
  590. weakref_slot=True,
  591. str=False,
  592. auto_attribs=False,
  593. kw_only=False,
  594. cache_hash=False,
  595. ):
  596. r"""
  597. A class decorator that adds `dunder
  598. <https://wiki.python.org/moin/DunderAlias>`_\ -methods according to the
  599. specified attributes using :func:`attr.ib` or the *these* argument.
  600. :param these: A dictionary of name to :func:`attr.ib` mappings. This is
  601. useful to avoid the definition of your attributes within the class body
  602. because you can't (e.g. if you want to add ``__repr__`` methods to
  603. Django models) or don't want to.
  604. If *these* is not ``None``, ``attrs`` will *not* search the class body
  605. for attributes and will *not* remove any attributes from it.
  606. If *these* is an ordered dict (:class:`dict` on Python 3.6+,
  607. :class:`collections.OrderedDict` otherwise), the order is deduced from
  608. the order of the attributes inside *these*. Otherwise the order
  609. of the definition of the attributes is used.
  610. :type these: :class:`dict` of :class:`str` to :func:`attr.ib`
  611. :param str repr_ns: When using nested classes, there's no way in Python 2
  612. to automatically detect that. Therefore it's possible to set the
  613. namespace explicitly for a more meaningful ``repr`` output.
  614. :param bool repr: Create a ``__repr__`` method with a human readable
  615. representation of ``attrs`` attributes..
  616. :param bool str: Create a ``__str__`` method that is identical to
  617. ``__repr__``. This is usually not necessary except for
  618. :class:`Exception`\ s.
  619. :param bool cmp: Create ``__eq__``, ``__ne__``, ``__lt__``, ``__le__``,
  620. ``__gt__``, and ``__ge__`` methods that compare the class as if it were
  621. a tuple of its ``attrs`` attributes. But the attributes are *only*
  622. compared, if the types of both classes are *identical*!
  623. :param hash: If ``None`` (default), the ``__hash__`` method is generated
  624. according how *cmp* and *frozen* are set.
  625. 1. If *both* are True, ``attrs`` will generate a ``__hash__`` for you.
  626. 2. If *cmp* is True and *frozen* is False, ``__hash__`` will be set to
  627. None, marking it unhashable (which it is).
  628. 3. If *cmp* is False, ``__hash__`` will be left untouched meaning the
  629. ``__hash__`` method of the base class will be used (if base class is
  630. ``object``, this means it will fall back to id-based hashing.).
  631. Although not recommended, you can decide for yourself and force
  632. ``attrs`` to create one (e.g. if the class is immutable even though you
  633. didn't freeze it programmatically) by passing ``True`` or not. Both of
  634. these cases are rather special and should be used carefully.
  635. See the `Python documentation \
  636. <https://docs.python.org/3/reference/datamodel.html#object.__hash__>`_
  637. and the `GitHub issue that led to the default behavior \
  638. <https://github.com/python-attrs/attrs/issues/136>`_ for more details.
  639. :type hash: ``bool`` or ``None``
  640. :param bool init: Create a ``__init__`` method that initializes the
  641. ``attrs`` attributes. Leading underscores are stripped for the
  642. argument name. If a ``__attrs_post_init__`` method exists on the
  643. class, it will be called after the class is fully initialized.
  644. :param bool slots: Create a slots_-style class that's more
  645. memory-efficient. See :ref:`slots` for further ramifications.
  646. :param bool frozen: Make instances immutable after initialization. If
  647. someone attempts to modify a frozen instance,
  648. :exc:`attr.exceptions.FrozenInstanceError` is raised.
  649. Please note:
  650. 1. This is achieved by installing a custom ``__setattr__`` method
  651. on your class so you can't implement an own one.
  652. 2. True immutability is impossible in Python.
  653. 3. This *does* have a minor a runtime performance :ref:`impact
  654. <how-frozen>` when initializing new instances. In other words:
  655. ``__init__`` is slightly slower with ``frozen=True``.
  656. 4. If a class is frozen, you cannot modify ``self`` in
  657. ``__attrs_post_init__`` or a self-written ``__init__``. You can
  658. circumvent that limitation by using
  659. ``object.__setattr__(self, "attribute_name", value)``.
  660. .. _slots: https://docs.python.org/3/reference/datamodel.html#slots
  661. :param bool weakref_slot: Make instances weak-referenceable. This has no
  662. effect unless ``slots`` is also enabled.
  663. :param bool auto_attribs: If True, collect `PEP 526`_-annotated attributes
  664. (Python 3.6 and later only) from the class body.
  665. In this case, you **must** annotate every field. If ``attrs``
  666. encounters a field that is set to an :func:`attr.ib` but lacks a type
  667. annotation, an :exc:`attr.exceptions.UnannotatedAttributeError` is
  668. raised. Use ``field_name: typing.Any = attr.ib(...)`` if you don't
  669. want to set a type.
  670. If you assign a value to those attributes (e.g. ``x: int = 42``), that
  671. value becomes the default value like if it were passed using
  672. ``attr.ib(default=42)``. Passing an instance of :class:`Factory` also
  673. works as expected.
  674. Attributes annotated as :data:`typing.ClassVar` are **ignored**.
  675. .. _`PEP 526`: https://www.python.org/dev/peps/pep-0526/
  676. :param bool kw_only: Make all attributes keyword-only (Python 3+)
  677. in the generated ``__init__`` (if ``init`` is ``False``, this
  678. parameter is ignored).
  679. :param bool cache_hash: Ensure that the object's hash code is computed
  680. only once and stored on the object. If this is set to ``True``,
  681. hashing must be either explicitly or implicitly enabled for this
  682. class. If the hash code is cached, then no attributes of this
  683. class which participate in hash code computation may be mutated
  684. after object creation.
  685. .. versionadded:: 16.0.0 *slots*
  686. .. versionadded:: 16.1.0 *frozen*
  687. .. versionadded:: 16.3.0 *str*
  688. .. versionadded:: 16.3.0 Support for ``__attrs_post_init__``.
  689. .. versionchanged:: 17.1.0
  690. *hash* supports ``None`` as value which is also the default now.
  691. .. versionadded:: 17.3.0 *auto_attribs*
  692. .. versionchanged:: 18.1.0
  693. If *these* is passed, no attributes are deleted from the class body.
  694. .. versionchanged:: 18.1.0 If *these* is ordered, the order is retained.
  695. .. versionadded:: 18.2.0 *weakref_slot*
  696. .. deprecated:: 18.2.0
  697. ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__`` now raise a
  698. :class:`DeprecationWarning` if the classes compared are subclasses of
  699. each other. ``__eq`` and ``__ne__`` never tried to compared subclasses
  700. to each other.
  701. .. versionadded:: 18.2.0 *kw_only*
  702. .. versionadded:: 18.2.0 *cache_hash*
  703. """
  704. def wrap(cls):
  705. if getattr(cls, "__class__", None) is None:
  706. raise TypeError("attrs only works with new-style classes.")
  707. builder = _ClassBuilder(
  708. cls,
  709. these,
  710. slots,
  711. frozen,
  712. weakref_slot,
  713. auto_attribs,
  714. kw_only,
  715. cache_hash,
  716. )
  717. if repr is True:
  718. builder.add_repr(repr_ns)
  719. if str is True:
  720. builder.add_str()
  721. if cmp is True:
  722. builder.add_cmp()
  723. if hash is not True and hash is not False and hash is not None:
  724. # Can't use `hash in` because 1 == True for example.
  725. raise TypeError(
  726. "Invalid value for hash. Must be True, False, or None."
  727. )
  728. elif hash is False or (hash is None and cmp is False):
  729. if cache_hash:
  730. raise TypeError(
  731. "Invalid value for cache_hash. To use hash caching,"
  732. " hashing must be either explicitly or implicitly "
  733. "enabled."
  734. )
  735. elif hash is True or (hash is None and cmp is True and frozen is True):
  736. builder.add_hash()
  737. else:
  738. if cache_hash:
  739. raise TypeError(
  740. "Invalid value for cache_hash. To use hash caching,"
  741. " hashing must be either explicitly or implicitly "
  742. "enabled."
  743. )
  744. builder.make_unhashable()
  745. if init is True:
  746. builder.add_init()
  747. else:
  748. if cache_hash:
  749. raise TypeError(
  750. "Invalid value for cache_hash. To use hash caching,"
  751. " init must be True."
  752. )
  753. return builder.build_class()
  754. # maybe_cls's type depends on the usage of the decorator. It's a class
  755. # if it's used as `@attrs` but ``None`` if used as `@attrs()`.
  756. if maybe_cls is None:
  757. return wrap
  758. else:
  759. return wrap(maybe_cls)
  760. _attrs = attrs
  761. """
  762. Internal alias so we can use it in functions that take an argument called
  763. *attrs*.
  764. """
  765. if PY2:
  766. def _has_frozen_base_class(cls):
  767. """
  768. Check whether *cls* has a frozen ancestor by looking at its
  769. __setattr__.
  770. """
  771. return (
  772. getattr(cls.__setattr__, "__module__", None)
  773. == _frozen_setattrs.__module__
  774. and cls.__setattr__.__name__ == _frozen_setattrs.__name__
  775. )
  776. else:
  777. def _has_frozen_base_class(cls):
  778. """
  779. Check whether *cls* has a frozen ancestor by looking at its
  780. __setattr__.
  781. """
  782. return cls.__setattr__ == _frozen_setattrs
  783. def _attrs_to_tuple(obj, attrs):
  784. """
  785. Create a tuple of all values of *obj*'s *attrs*.
  786. """
  787. return tuple(getattr(obj, a.name) for a in attrs)
  788. def _make_hash(attrs, frozen, cache_hash):
  789. attrs = tuple(
  790. a
  791. for a in attrs
  792. if a.hash is True or (a.hash is None and a.cmp is True)
  793. )
  794. tab = " "
  795. # We cache the generated hash methods for the same kinds of attributes.
  796. sha1 = hashlib.sha1()
  797. sha1.update(repr(attrs).encode("utf-8"))
  798. unique_filename = "<attrs generated hash %s>" % (sha1.hexdigest(),)
  799. type_hash = hash(unique_filename)
  800. method_lines = ["def __hash__(self):"]
  801. def append_hash_computation_lines(prefix, indent):
  802. """
  803. Generate the code for actually computing the hash code.
  804. Below this will either be returned directly or used to compute
  805. a value which is then cached, depending on the value of cache_hash
  806. """
  807. method_lines.extend(
  808. [indent + prefix + "hash((", indent + " %d," % (type_hash,)]
  809. )
  810. for a in attrs:
  811. method_lines.append(indent + " self.%s," % a.name)
  812. method_lines.append(indent + " ))")
  813. if cache_hash:
  814. method_lines.append(tab + "if self.%s is None:" % _hash_cache_field)
  815. if frozen:
  816. append_hash_computation_lines(
  817. "object.__setattr__(self, '%s', " % _hash_cache_field, tab * 2
  818. )
  819. method_lines.append(tab * 2 + ")") # close __setattr__
  820. else:
  821. append_hash_computation_lines(
  822. "self.%s = " % _hash_cache_field, tab * 2
  823. )
  824. method_lines.append(tab + "return self.%s" % _hash_cache_field)
  825. else:
  826. append_hash_computation_lines("return ", tab)
  827. script = "\n".join(method_lines)
  828. globs = {}
  829. locs = {}
  830. bytecode = compile(script, unique_filename, "exec")
  831. eval(bytecode, globs, locs)
  832. # In order of debuggers like PDB being able to step through the code,
  833. # we add a fake linecache entry.
  834. linecache.cache[unique_filename] = (
  835. len(script),
  836. None,
  837. script.splitlines(True),
  838. unique_filename,
  839. )
  840. return locs["__hash__"]
  841. def _add_hash(cls, attrs):
  842. """
  843. Add a hash method to *cls*.
  844. """
  845. cls.__hash__ = _make_hash(attrs, frozen=False, cache_hash=False)
  846. return cls
  847. def __ne__(self, other):
  848. """
  849. Check equality and either forward a NotImplemented or return the result
  850. negated.
  851. """
  852. result = self.__eq__(other)
  853. if result is NotImplemented:
  854. return NotImplemented
  855. return not result
  856. WARNING_CMP_ISINSTANCE = (
  857. "Comparision of subclasses using __%s__ is deprecated and will be removed "
  858. "in 2019."
  859. )
  860. def _make_cmp(attrs):
  861. attrs = [a for a in attrs if a.cmp]
  862. # We cache the generated eq methods for the same kinds of attributes.
  863. sha1 = hashlib.sha1()
  864. sha1.update(repr(attrs).encode("utf-8"))
  865. unique_filename = "<attrs generated eq %s>" % (sha1.hexdigest(),)
  866. lines = [
  867. "def __eq__(self, other):",
  868. " if other.__class__ is not self.__class__:",
  869. " return NotImplemented",
  870. ]
  871. # We can't just do a big self.x = other.x and... clause due to
  872. # irregularities like nan == nan is false but (nan,) == (nan,) is true.
  873. if attrs:
  874. lines.append(" return (")
  875. others = [" ) == ("]
  876. for a in attrs:
  877. lines.append(" self.%s," % (a.name,))
  878. others.append(" other.%s," % (a.name,))
  879. lines += others + [" )"]
  880. else:
  881. lines.append(" return True")
  882. script = "\n".join(lines)
  883. globs = {}
  884. locs = {}
  885. bytecode = compile(script, unique_filename, "exec")
  886. eval(bytecode, globs, locs)
  887. # In order of debuggers like PDB being able to step through the code,
  888. # we add a fake linecache entry.
  889. linecache.cache[unique_filename] = (
  890. len(script),
  891. None,
  892. script.splitlines(True),
  893. unique_filename,
  894. )
  895. eq = locs["__eq__"]
  896. ne = __ne__
  897. def attrs_to_tuple(obj):
  898. """
  899. Save us some typing.
  900. """
  901. return _attrs_to_tuple(obj, attrs)
  902. def __lt__(self, other):
  903. """
  904. Automatically created by attrs.
  905. """
  906. if isinstance(other, self.__class__):
  907. if other.__class__ is not self.__class__:
  908. warnings.warn(
  909. WARNING_CMP_ISINSTANCE % ("lt",), DeprecationWarning
  910. )
  911. return attrs_to_tuple(self) < attrs_to_tuple(other)
  912. else:
  913. return NotImplemented
  914. def __le__(self, other):
  915. """
  916. Automatically created by attrs.
  917. """
  918. if isinstance(other, self.__class__):
  919. if other.__class__ is not self.__class__:
  920. warnings.warn(
  921. WARNING_CMP_ISINSTANCE % ("le",), DeprecationWarning
  922. )
  923. return attrs_to_tuple(self) <= attrs_to_tuple(other)
  924. else:
  925. return NotImplemented
  926. def __gt__(self, other):
  927. """
  928. Automatically created by attrs.
  929. """
  930. if isinstance(other, self.__class__):
  931. if other.__class__ is not self.__class__:
  932. warnings.warn(
  933. WARNING_CMP_ISINSTANCE % ("gt",), DeprecationWarning
  934. )
  935. return attrs_to_tuple(self) > attrs_to_tuple(other)
  936. else:
  937. return NotImplemented
  938. def __ge__(self, other):
  939. """
  940. Automatically created by attrs.
  941. """
  942. if isinstance(other, self.__class__):
  943. if other.__class__ is not self.__class__:
  944. warnings.warn(
  945. WARNING_CMP_ISINSTANCE % ("ge",), DeprecationWarning
  946. )
  947. return attrs_to_tuple(self) >= attrs_to_tuple(other)
  948. else:
  949. return NotImplemented
  950. return eq, ne, __lt__, __le__, __gt__, __ge__
  951. def _add_cmp(cls, attrs=None):
  952. """
  953. Add comparison methods to *cls*.
  954. """
  955. if attrs is None:
  956. attrs = cls.__attrs_attrs__
  957. cls.__eq__, cls.__ne__, cls.__lt__, cls.__le__, cls.__gt__, cls.__ge__ = _make_cmp( # noqa
  958. attrs
  959. )
  960. return cls
  961. _already_repring = threading.local()
  962. def _make_repr(attrs, ns):
  963. """
  964. Make a repr method for *attr_names* adding *ns* to the full name.
  965. """
  966. attr_names = tuple(a.name for a in attrs if a.repr)
  967. def __repr__(self):
  968. """
  969. Automatically created by attrs.
  970. """
  971. try:
  972. working_set = _already_repring.working_set
  973. except AttributeError:
  974. working_set = set()
  975. _already_repring.working_set = working_set
  976. if id(self) in working_set:
  977. return "..."
  978. real_cls = self.__class__
  979. if ns is None:
  980. qualname = getattr(real_cls, "__qualname__", None)
  981. if qualname is not None:
  982. class_name = qualname.rsplit(">.", 1)[-1]
  983. else:
  984. class_name = real_cls.__name__
  985. else:
  986. class_name = ns + "." + real_cls.__name__
  987. # Since 'self' remains on the stack (i.e.: strongly referenced) for the
  988. # duration of this call, it's safe to depend on id(...) stability, and
  989. # not need to track the instance and therefore worry about properties
  990. # like weakref- or hash-ability.
  991. working_set.add(id(self))
  992. try:
  993. result = [class_name, "("]
  994. first = True
  995. for name in attr_names:
  996. if first:
  997. first = False
  998. else:
  999. result.append(", ")
  1000. result.extend((name, "=", repr(getattr(self, name, NOTHING))))
  1001. return "".join(result) + ")"
  1002. finally:
  1003. working_set.remove(id(self))
  1004. return __repr__
  1005. def _add_repr(cls, ns=None, attrs=None):
  1006. """
  1007. Add a repr method to *cls*.
  1008. """
  1009. if attrs is None:
  1010. attrs = cls.__attrs_attrs__
  1011. cls.__repr__ = _make_repr(attrs, ns)
  1012. return cls
  1013. def _make_init(attrs, post_init, frozen, slots, cache_hash, base_attr_map):
  1014. attrs = [a for a in attrs if a.init or a.default is not NOTHING]
  1015. # We cache the generated init methods for the same kinds of attributes.
  1016. sha1 = hashlib.sha1()
  1017. sha1.update(repr(attrs).encode("utf-8"))
  1018. unique_filename = "<attrs generated init {0}>".format(sha1.hexdigest())
  1019. script, globs, annotations = _attrs_to_init_script(
  1020. attrs, frozen, slots, post_init, cache_hash, base_attr_map
  1021. )
  1022. locs = {}
  1023. bytecode = compile(script, unique_filename, "exec")
  1024. attr_dict = dict((a.name, a) for a in attrs)
  1025. globs.update({"NOTHING": NOTHING, "attr_dict": attr_dict})
  1026. if frozen is True:
  1027. # Save the lookup overhead in __init__ if we need to circumvent
  1028. # immutability.
  1029. globs["_cached_setattr"] = _obj_setattr
  1030. eval(bytecode, globs, locs)
  1031. # In order of debuggers like PDB being able to step through the code,
  1032. # we add a fake linecache entry.
  1033. linecache.cache[unique_filename] = (
  1034. len(script),
  1035. None,
  1036. script.splitlines(True),
  1037. unique_filename,
  1038. )
  1039. __init__ = locs["__init__"]
  1040. __init__.__annotations__ = annotations
  1041. return __init__
  1042. def _add_init(cls, frozen):
  1043. """
  1044. Add a __init__ method to *cls*. If *frozen* is True, make it immutable.
  1045. """
  1046. cls.__init__ = _make_init(
  1047. cls.__attrs_attrs__,
  1048. getattr(cls, "__attrs_post_init__", False),
  1049. frozen,
  1050. _is_slot_cls(cls),
  1051. cache_hash=False,
  1052. base_attr_map={},
  1053. )
  1054. return cls
  1055. def fields(cls):
  1056. """
  1057. Return the tuple of ``attrs`` attributes for a class.
  1058. The tuple also allows accessing the fields by their names (see below for
  1059. examples).
  1060. :param type cls: Class to introspect.
  1061. :raise TypeError: If *cls* is not a class.
  1062. :raise attr.exceptions.NotAnAttrsClassError: If *cls* is not an ``attrs``
  1063. class.
  1064. :rtype: tuple (with name accessors) of :class:`attr.Attribute`
  1065. .. versionchanged:: 16.2.0 Returned tuple allows accessing the fields
  1066. by name.
  1067. """
  1068. if not isclass(cls):
  1069. raise TypeError("Passed object must be a class.")
  1070. attrs = getattr(cls, "__attrs_attrs__", None)
  1071. if attrs is None:
  1072. raise NotAnAttrsClassError(
  1073. "{cls!r} is not an attrs-decorated class.".format(cls=cls)
  1074. )
  1075. return attrs
  1076. def fields_dict(cls):
  1077. """
  1078. Return an ordered dictionary of ``attrs`` attributes for a class, whose
  1079. keys are the attribute names.
  1080. :param type cls: Class to introspect.
  1081. :raise TypeError: If *cls* is not a class.
  1082. :raise attr.exceptions.NotAnAttrsClassError: If *cls* is not an ``attrs``
  1083. class.
  1084. :rtype: an ordered dict where keys are attribute names and values are
  1085. :class:`attr.Attribute`\\ s. This will be a :class:`dict` if it's
  1086. naturally ordered like on Python 3.6+ or an
  1087. :class:`~collections.OrderedDict` otherwise.
  1088. .. versionadded:: 18.1.0
  1089. """
  1090. if not isclass(cls):
  1091. raise TypeError("Passed object must be a class.")
  1092. attrs = getattr(cls, "__attrs_attrs__", None)
  1093. if attrs is None:
  1094. raise NotAnAttrsClassError(
  1095. "{cls!r} is not an attrs-decorated class.".format(cls=cls)
  1096. )
  1097. return ordered_dict(((a.name, a) for a in attrs))
  1098. def validate(inst):
  1099. """
  1100. Validate all attributes on *inst* that have a validator.
  1101. Leaves all exceptions through.
  1102. :param inst: Instance of a class with ``attrs`` attributes.
  1103. """
  1104. if _config._run_validators is False:
  1105. return
  1106. for a in fields(inst.__class__):
  1107. v = a.validator
  1108. if v is not None:
  1109. v(inst, a, getattr(inst, a.name))
  1110. def _is_slot_cls(cls):
  1111. return "__slots__" in cls.__dict__
  1112. def _is_slot_attr(a_name, base_attr_map):
  1113. """
  1114. Check if the attribute name comes from a slot class.
  1115. """
  1116. return a_name in base_attr_map and _is_slot_cls(base_attr_map[a_name])
  1117. def _attrs_to_init_script(
  1118. attrs, frozen, slots, post_init, cache_hash, base_attr_map
  1119. ):
  1120. """
  1121. Return a script of an initializer for *attrs* and a dict of globals.
  1122. The globals are expected by the generated script.
  1123. If *frozen* is True, we cannot set the attributes directly so we use
  1124. a cached ``object.__setattr__``.
  1125. """
  1126. lines = []
  1127. any_slot_ancestors = any(
  1128. _is_slot_attr(a.name, base_attr_map) for a in attrs
  1129. )
  1130. if frozen is True:
  1131. if slots is True:
  1132. lines.append(
  1133. # Circumvent the __setattr__ descriptor to save one lookup per
  1134. # assignment.
  1135. # Note _setattr will be used again below if cache_hash is True
  1136. "_setattr = _cached_setattr.__get__(self, self.__class__)"
  1137. )
  1138. def fmt_setter(attr_name, value_var):
  1139. return "_setattr('%(attr_name)s', %(value_var)s)" % {
  1140. "attr_name": attr_name,
  1141. "value_var": value_var,
  1142. }
  1143. def fmt_setter_with_converter(attr_name, value_var):
  1144. conv_name = _init_converter_pat.format(attr_name)
  1145. return "_setattr('%(attr_name)s', %(conv)s(%(value_var)s))" % {
  1146. "attr_name": attr_name,
  1147. "value_var": value_var,
  1148. "conv": conv_name,
  1149. }
  1150. else:
  1151. # Dict frozen classes assign directly to __dict__.
  1152. # But only if the attribute doesn't come from an ancestor slot
  1153. # class.
  1154. # Note _inst_dict will be used again below if cache_hash is True
  1155. lines.append("_inst_dict = self.__dict__")
  1156. if any_slot_ancestors:
  1157. lines.append(
  1158. # Circumvent the __setattr__ descriptor to save one lookup
  1159. # per assignment.
  1160. "_setattr = _cached_setattr.__get__(self, self.__class__)"
  1161. )
  1162. def fmt_setter(attr_name, value_var):
  1163. if _is_slot_attr(attr_name, base_attr_map):
  1164. res = "_setattr('%(attr_name)s', %(value_var)s)" % {
  1165. "attr_name": attr_name,
  1166. "value_var": value_var,
  1167. }
  1168. else:
  1169. res = "_inst_dict['%(attr_name)s'] = %(value_var)s" % {
  1170. "attr_name": attr_name,
  1171. "value_var": value_var,
  1172. }
  1173. return res
  1174. def fmt_setter_with_converter(attr_name, value_var):
  1175. conv_name = _init_converter_pat.format(attr_name)
  1176. if _is_slot_attr(attr_name, base_attr_map):
  1177. tmpl = "_setattr('%(attr_name)s', %(c)s(%(value_var)s))"
  1178. else:
  1179. tmpl = "_inst_dict['%(attr_name)s'] = %(c)s(%(value_var)s)"
  1180. return tmpl % {
  1181. "attr_name": attr_name,
  1182. "value_var": value_var,
  1183. "c": conv_name,
  1184. }
  1185. else:
  1186. # Not frozen.
  1187. def fmt_setter(attr_name, value):
  1188. return "self.%(attr_name)s = %(value)s" % {
  1189. "attr_name": attr_name,
  1190. "value": value,
  1191. }
  1192. def fmt_setter_with_converter(attr_name, value_var):
  1193. conv_name = _init_converter_pat.format(attr_name)
  1194. return "self.%(attr_name)s = %(conv)s(%(value_var)s)" % {
  1195. "attr_name": attr_name,
  1196. "value_var": value_var,
  1197. "conv": conv_name,
  1198. }
  1199. args = []
  1200. kw_only_args = []
  1201. attrs_to_validate = []
  1202. # This is a dictionary of names to validator and converter callables.
  1203. # Injecting this into __init__ globals lets us avoid lookups.
  1204. names_for_globals = {}
  1205. annotations = {"return": None}
  1206. for a in attrs:
  1207. if a.validator:
  1208. attrs_to_validate.append(a)
  1209. attr_name = a.name
  1210. arg_name = a.name.lstrip("_")
  1211. has_factory = isinstance(a.default, Factory)
  1212. if has_factory and a.default.takes_self:
  1213. maybe_self = "self"
  1214. else:
  1215. maybe_self = ""
  1216. if a.init is False:
  1217. if has_factory:
  1218. init_factory_name = _init_factory_pat.format(a.name)
  1219. if a.converter is not None:
  1220. lines.append(
  1221. fmt_setter_with_converter(
  1222. attr_name,
  1223. init_factory_name + "({0})".format(maybe_self),
  1224. )
  1225. )
  1226. conv_name = _init_converter_pat.format(a.name)
  1227. names_for_globals[conv_name] = a.converter
  1228. else:
  1229. lines.append(
  1230. fmt_setter(
  1231. attr_name,
  1232. init_factory_name + "({0})".format(maybe_self),
  1233. )
  1234. )
  1235. names_for_globals[init_factory_name] = a.default.factory
  1236. else:
  1237. if a.converter is not None:
  1238. lines.append(
  1239. fmt_setter_with_converter(
  1240. attr_name,
  1241. "attr_dict['{attr_name}'].default".format(
  1242. attr_name=attr_name
  1243. ),
  1244. )
  1245. )
  1246. conv_name = _init_converter_pat.format(a.name)
  1247. names_for_globals[conv_name] = a.converter
  1248. else:
  1249. lines.append(
  1250. fmt_setter(
  1251. attr_name,
  1252. "attr_dict['{attr_name}'].default".format(
  1253. attr_name=attr_name
  1254. ),
  1255. )
  1256. )
  1257. elif a.default is not NOTHING and not has_factory:
  1258. arg = "{arg_name}=attr_dict['{attr_name}'].default".format(
  1259. arg_name=arg_name, attr_name=attr_name
  1260. )
  1261. if a.kw_only:
  1262. kw_only_args.append(arg)
  1263. else:
  1264. args.append(arg)
  1265. if a.converter is not None:
  1266. lines.append(fmt_setter_with_converter(attr_name, arg_name))
  1267. names_for_globals[
  1268. _init_converter_pat.format(a.name)
  1269. ] = a.converter
  1270. else:
  1271. lines.append(fmt_setter(attr_name, arg_name))
  1272. elif has_factory:
  1273. arg = "{arg_name}=NOTHING".format(arg_name=arg_name)
  1274. if a.kw_only:
  1275. kw_only_args.append(arg)
  1276. else:
  1277. args.append(arg)
  1278. lines.append(
  1279. "if {arg_name} is not NOTHING:".format(arg_name=arg_name)
  1280. )
  1281. init_factory_name = _init_factory_pat.format(a.name)
  1282. if a.converter is not None:
  1283. lines.append(
  1284. " " + fmt_setter_with_converter(attr_name, arg_name)
  1285. )
  1286. lines.append("else:")
  1287. lines.append(
  1288. " "
  1289. + fmt_setter_with_converter(
  1290. attr_name,
  1291. init_factory_name + "({0})".format(maybe_self),
  1292. )
  1293. )
  1294. names_for_globals[
  1295. _init_converter_pat.format(a.name)
  1296. ] = a.converter
  1297. else:
  1298. lines.append(" " + fmt_setter(attr_name, arg_name))
  1299. lines.append("else:")
  1300. lines.append(
  1301. " "
  1302. + fmt_setter(
  1303. attr_name,
  1304. init_factory_name + "({0})".format(maybe_self),
  1305. )
  1306. )
  1307. names_for_globals[init_factory_name] = a.default.factory
  1308. else:
  1309. if a.kw_only:
  1310. kw_only_args.append(arg_name)
  1311. else:
  1312. args.append(arg_name)
  1313. if a.converter is not None:
  1314. lines.append(fmt_setter_with_converter(attr_name, arg_name))
  1315. names_for_globals[
  1316. _init_converter_pat.format(a.name)
  1317. ] = a.converter
  1318. else:
  1319. lines.append(fmt_setter(attr_name, arg_name))
  1320. if a.init is True and a.converter is None and a.type is not None:
  1321. annotations[arg_name] = a.type
  1322. if attrs_to_validate: # we can skip this if there are no validators.
  1323. names_for_globals["_config"] = _config
  1324. lines.append("if _config._run_validators is True:")
  1325. for a in attrs_to_validate:
  1326. val_name = "__attr_validator_{}".format(a.name)
  1327. attr_name = "__attr_{}".format(a.name)
  1328. lines.append(
  1329. " {}(self, {}, self.{})".format(val_name, attr_name, a.name)
  1330. )
  1331. names_for_globals[val_name] = a.validator
  1332. names_for_globals[attr_name] = a
  1333. if post_init:
  1334. lines.append("self.__attrs_post_init__()")
  1335. # because this is set only after __attrs_post_init is called, a crash
  1336. # will result if post-init tries to access the hash code. This seemed
  1337. # preferable to setting this beforehand, in which case alteration to
  1338. # field values during post-init combined with post-init accessing the
  1339. # hash code would result in silent bugs.
  1340. if cache_hash:
  1341. if frozen:
  1342. if slots:
  1343. # if frozen and slots, then _setattr defined above
  1344. init_hash_cache = "_setattr('%s', %s)"
  1345. else:
  1346. # if frozen and not slots, then _inst_dict defined above
  1347. init_hash_cache = "_inst_dict['%s'] = %s"
  1348. else:
  1349. init_hash_cache = "self.%s = %s"
  1350. lines.append(init_hash_cache % (_hash_cache_field, "None"))
  1351. args = ", ".join(args)
  1352. if kw_only_args:
  1353. if PY2:
  1354. raise PythonTooOldError(
  1355. "Keyword-only arguments only work on Python 3 and later."
  1356. )
  1357. args += "{leading_comma}*, {kw_only_args}".format(
  1358. leading_comma=", " if args else "",
  1359. kw_only_args=", ".join(kw_only_args),
  1360. )
  1361. return (
  1362. """\
  1363. def __init__(self, {args}):
  1364. {lines}
  1365. """.format(
  1366. args=args, lines="\n ".join(lines) if lines else "pass"
  1367. ),
  1368. names_for_globals,
  1369. annotations,
  1370. )
  1371. class Attribute(object):
  1372. """
  1373. *Read-only* representation of an attribute.
  1374. :attribute name: The name of the attribute.
  1375. Plus *all* arguments of :func:`attr.ib`.
  1376. For the version history of the fields, see :func:`attr.ib`.
  1377. """
  1378. __slots__ = (
  1379. "name",
  1380. "default",
  1381. "validator",
  1382. "repr",
  1383. "cmp",
  1384. "hash",
  1385. "init",
  1386. "metadata",
  1387. "type",
  1388. "converter",
  1389. "kw_only",
  1390. )
  1391. def __init__(
  1392. self,
  1393. name,
  1394. default,
  1395. validator,
  1396. repr,
  1397. cmp,
  1398. hash,
  1399. init,
  1400. convert=None,
  1401. metadata=None,
  1402. type=None,
  1403. converter=None,
  1404. kw_only=False,
  1405. ):
  1406. # Cache this descriptor here to speed things up later.
  1407. bound_setattr = _obj_setattr.__get__(self, Attribute)
  1408. # Despite the big red warning, people *do* instantiate `Attribute`
  1409. # themselves.
  1410. if convert is not None:
  1411. if converter is not None:
  1412. raise RuntimeError(
  1413. "Can't pass both `convert` and `converter`. "
  1414. "Please use `converter` only."
  1415. )
  1416. warnings.warn(
  1417. "The `convert` argument is deprecated in favor of `converter`."
  1418. " It will be removed after 2019/01.",
  1419. DeprecationWarning,
  1420. stacklevel=2,
  1421. )
  1422. converter = convert
  1423. bound_setattr("name", name)
  1424. bound_setattr("default", default)
  1425. bound_setattr("validator", validator)
  1426. bound_setattr("repr", repr)
  1427. bound_setattr("cmp", cmp)
  1428. bound_setattr("hash", hash)
  1429. bound_setattr("init", init)
  1430. bound_setattr("converter", converter)
  1431. bound_setattr(
  1432. "metadata",
  1433. (
  1434. metadata_proxy(metadata)
  1435. if metadata
  1436. else _empty_metadata_singleton
  1437. ),
  1438. )
  1439. bound_setattr("type", type)
  1440. bound_setattr("kw_only", kw_only)
  1441. def __setattr__(self, name, value):
  1442. raise FrozenInstanceError()
  1443. @property
  1444. def convert(self):
  1445. warnings.warn(
  1446. "The `convert` attribute is deprecated in favor of `converter`. "
  1447. "It will be removed after 2019/01.",
  1448. DeprecationWarning,
  1449. stacklevel=2,
  1450. )
  1451. return self.converter
  1452. @classmethod
  1453. def from_counting_attr(cls, name, ca, type=None):
  1454. # type holds the annotated value. deal with conflicts:
  1455. if type is None:
  1456. type = ca.type
  1457. elif ca.type is not None:
  1458. raise ValueError(
  1459. "Type annotation and type argument cannot both be present"
  1460. )
  1461. inst_dict = {
  1462. k: getattr(ca, k)
  1463. for k in Attribute.__slots__
  1464. if k
  1465. not in (
  1466. "name",
  1467. "validator",
  1468. "default",
  1469. "type",
  1470. "convert",
  1471. ) # exclude methods and deprecated alias
  1472. }
  1473. return cls(
  1474. name=name,
  1475. validator=ca._validator,
  1476. default=ca._default,
  1477. type=type,
  1478. **inst_dict
  1479. )
  1480. # Don't use attr.assoc since fields(Attribute) doesn't work
  1481. def _assoc(self, **changes):
  1482. """
  1483. Copy *self* and apply *changes*.
  1484. """
  1485. new = copy.copy(self)
  1486. new._setattrs(changes.items())
  1487. return new
  1488. # Don't use _add_pickle since fields(Attribute) doesn't work
  1489. def __getstate__(self):
  1490. """
  1491. Play nice with pickle.
  1492. """
  1493. return tuple(
  1494. getattr(self, name) if name != "metadata" else dict(self.metadata)
  1495. for name in self.__slots__
  1496. )
  1497. def __setstate__(self, state):
  1498. """
  1499. Play nice with pickle.
  1500. """
  1501. self._setattrs(zip(self.__slots__, state))
  1502. def _setattrs(self, name_values_pairs):
  1503. bound_setattr = _obj_setattr.__get__(self, Attribute)
  1504. for name, value in name_values_pairs:
  1505. if name != "metadata":
  1506. bound_setattr(name, value)
  1507. else:
  1508. bound_setattr(
  1509. name,
  1510. metadata_proxy(value)
  1511. if value
  1512. else _empty_metadata_singleton,
  1513. )
  1514. _a = [
  1515. Attribute(
  1516. name=name,
  1517. default=NOTHING,
  1518. validator=None,
  1519. repr=True,
  1520. cmp=True,
  1521. hash=(name != "metadata"),
  1522. init=True,
  1523. )
  1524. for name in Attribute.__slots__
  1525. if name != "convert" # XXX: remove once `convert` is gone
  1526. ]
  1527. Attribute = _add_hash(
  1528. _add_cmp(_add_repr(Attribute, attrs=_a), attrs=_a),
  1529. attrs=[a for a in _a if a.hash],
  1530. )
  1531. class _CountingAttr(object):
  1532. """
  1533. Intermediate representation of attributes that uses a counter to preserve
  1534. the order in which the attributes have been defined.
  1535. *Internal* data structure of the attrs library. Running into is most
  1536. likely the result of a bug like a forgotten `@attr.s` decorator.
  1537. """
  1538. __slots__ = (
  1539. "counter",
  1540. "_default",
  1541. "repr",
  1542. "cmp",
  1543. "hash",
  1544. "init",
  1545. "metadata",
  1546. "_validator",
  1547. "converter",
  1548. "type",
  1549. "kw_only",
  1550. )
  1551. __attrs_attrs__ = tuple(
  1552. Attribute(
  1553. name=name,
  1554. default=NOTHING,
  1555. validator=None,
  1556. repr=True,
  1557. cmp=True,
  1558. hash=True,
  1559. init=True,
  1560. kw_only=False,
  1561. )
  1562. for name in ("counter", "_default", "repr", "cmp", "hash", "init")
  1563. ) + (
  1564. Attribute(
  1565. name="metadata",
  1566. default=None,
  1567. validator=None,
  1568. repr=True,
  1569. cmp=True,
  1570. hash=False,
  1571. init=True,
  1572. kw_only=False,
  1573. ),
  1574. )
  1575. cls_counter = 0
  1576. def __init__(
  1577. self,
  1578. default,
  1579. validator,
  1580. repr,
  1581. cmp,
  1582. hash,
  1583. init,
  1584. converter,
  1585. metadata,
  1586. type,
  1587. kw_only,
  1588. ):
  1589. _CountingAttr.cls_counter += 1
  1590. self.counter = _CountingAttr.cls_counter
  1591. self._default = default
  1592. # If validator is a list/tuple, wrap it using helper validator.
  1593. if validator and isinstance(validator, (list, tuple)):
  1594. self._validator = and_(*validator)
  1595. else:
  1596. self._validator = validator
  1597. self.repr = repr
  1598. self.cmp = cmp
  1599. self.hash = hash
  1600. self.init = init
  1601. self.converter = converter
  1602. self.metadata = metadata
  1603. self.type = type
  1604. self.kw_only = kw_only
  1605. def validator(self, meth):
  1606. """
  1607. Decorator that adds *meth* to the list of validators.
  1608. Returns *meth* unchanged.
  1609. .. versionadded:: 17.1.0
  1610. """
  1611. if self._validator is None:
  1612. self._validator = meth
  1613. else:
  1614. self._validator = and_(self._validator, meth)
  1615. return meth
  1616. def default(self, meth):
  1617. """
  1618. Decorator that allows to set the default for an attribute.
  1619. Returns *meth* unchanged.
  1620. :raises DefaultAlreadySetError: If default has been set before.
  1621. .. versionadded:: 17.1.0
  1622. """
  1623. if self._default is not NOTHING:
  1624. raise DefaultAlreadySetError()
  1625. self._default = Factory(meth, takes_self=True)
  1626. return meth
  1627. _CountingAttr = _add_cmp(_add_repr(_CountingAttr))
  1628. @attrs(slots=True, init=False, hash=True)
  1629. class Factory(object):
  1630. """
  1631. Stores a factory callable.
  1632. If passed as the default value to :func:`attr.ib`, the factory is used to
  1633. generate a new value.
  1634. :param callable factory: A callable that takes either none or exactly one
  1635. mandatory positional argument depending on *takes_self*.
  1636. :param bool takes_self: Pass the partially initialized instance that is
  1637. being initialized as a positional argument.
  1638. .. versionadded:: 17.1.0 *takes_self*
  1639. """
  1640. factory = attrib()
  1641. takes_self = attrib()
  1642. def __init__(self, factory, takes_self=False):
  1643. """
  1644. `Factory` is part of the default machinery so if we want a default
  1645. value here, we have to implement it ourselves.
  1646. """
  1647. self.factory = factory
  1648. self.takes_self = takes_self
  1649. def make_class(name, attrs, bases=(object,), **attributes_arguments):
  1650. """
  1651. A quick way to create a new class called *name* with *attrs*.
  1652. :param name: The name for the new class.
  1653. :type name: str
  1654. :param attrs: A list of names or a dictionary of mappings of names to
  1655. attributes.
  1656. If *attrs* is a list or an ordered dict (:class:`dict` on Python 3.6+,
  1657. :class:`collections.OrderedDict` otherwise), the order is deduced from
  1658. the order of the names or attributes inside *attrs*. Otherwise the
  1659. order of the definition of the attributes is used.
  1660. :type attrs: :class:`list` or :class:`dict`
  1661. :param tuple bases: Classes that the new class will subclass.
  1662. :param attributes_arguments: Passed unmodified to :func:`attr.s`.
  1663. :return: A new class with *attrs*.
  1664. :rtype: type
  1665. .. versionadded:: 17.1.0 *bases*
  1666. .. versionchanged:: 18.1.0 If *attrs* is ordered, the order is retained.
  1667. """
  1668. if isinstance(attrs, dict):
  1669. cls_dict = attrs
  1670. elif isinstance(attrs, (list, tuple)):
  1671. cls_dict = dict((a, attrib()) for a in attrs)
  1672. else:
  1673. raise TypeError("attrs argument must be a dict or a list.")
  1674. post_init = cls_dict.pop("__attrs_post_init__", None)
  1675. type_ = type(
  1676. name,
  1677. bases,
  1678. {} if post_init is None else {"__attrs_post_init__": post_init},
  1679. )
  1680. # For pickling to work, the __module__ variable needs to be set to the
  1681. # frame where the class is created. Bypass this step in environments where
  1682. # sys._getframe is not defined (Jython for example) or sys._getframe is not
  1683. # defined for arguments greater than 0 (IronPython).
  1684. try:
  1685. type_.__module__ = sys._getframe(1).f_globals.get(
  1686. "__name__", "__main__"
  1687. )
  1688. except (AttributeError, ValueError):
  1689. pass
  1690. return _attrs(these=cls_dict, **attributes_arguments)(type_)
  1691. # These are required by within this module so we define them here and merely
  1692. # import into .validators.
  1693. @attrs(slots=True, hash=True)
  1694. class _AndValidator(object):
  1695. """
  1696. Compose many validators to a single one.
  1697. """
  1698. _validators = attrib()
  1699. def __call__(self, inst, attr, value):
  1700. for v in self._validators:
  1701. v(inst, attr, value)
  1702. def and_(*validators):
  1703. """
  1704. A validator that composes multiple validators into one.
  1705. When called on a value, it runs all wrapped validators.
  1706. :param validators: Arbitrary number of validators.
  1707. :type validators: callables
  1708. .. versionadded:: 17.1.0
  1709. """
  1710. vals = []
  1711. for validator in validators:
  1712. vals.extend(
  1713. validator._validators
  1714. if isinstance(validator, _AndValidator)
  1715. else [validator]
  1716. )
  1717. return _AndValidator(tuple(vals))