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