Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/attr/setters.py: 38%
26 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
1# SPDX-License-Identifier: MIT
3"""
4Commonly used hooks for on_setattr.
5"""
8from . import _config
9from .exceptions import FrozenAttributeError
12def pipe(*setters):
13 """
14 Run all *setters* and return the return value of the last one.
16 .. versionadded:: 20.1.0
17 """
19 def wrapped_pipe(instance, attrib, new_value):
20 rv = new_value
22 for setter in setters:
23 rv = setter(instance, attrib, rv)
25 return rv
27 return wrapped_pipe
30def frozen(_, __, ___):
31 """
32 Prevent an attribute to be modified.
34 .. versionadded:: 20.1.0
35 """
36 raise FrozenAttributeError()
39def validate(instance, attrib, new_value):
40 """
41 Run *attrib*'s validator on *new_value* if it has one.
43 .. versionadded:: 20.1.0
44 """
45 if _config._run_validators is False:
46 return new_value
48 v = attrib.validator
49 if not v:
50 return new_value
52 v(instance, attrib, new_value)
54 return new_value
57def convert(instance, attrib, new_value):
58 """
59 Run *attrib*'s converter -- if it has one -- on *new_value* and return the
60 result.
62 .. versionadded:: 20.1.0
63 """
64 c = attrib.converter
65 if c:
66 return c(new_value)
68 return new_value
71# Sentinel for disabling class-wide *on_setattr* hooks for certain attributes.
72# autodata stopped working, so the docstring is inlined in the API docs.
73NO_OP = object()