MCPcopy
hub / github.com/python-attrs/attrs / astuple

Function astuple

src/attr/_funcs.py:232–351  ·  view source on GitHub ↗

Return the *attrs* attribute values of *inst* as a tuple. Optionally recurse into other *attrs*-decorated classes. Args: inst: Instance of an *attrs*-decorated class. recurse (bool): Recurse into classes that are also *attrs*-decorated. filter (~t

(
    inst,
    recurse=True,
    filter=None,
    tuple_factory=tuple,
    retain_collection_types=False,
)

Source from the content-addressed store, hash-verified

230
231
232def astuple(
233 inst,
234 recurse=True,
235 filter=None,
236 tuple_factory=tuple,
237 retain_collection_types=False,
238):
239 """
240 Return the *attrs* attribute values of *inst* as a tuple.
241
242 Optionally recurse into other *attrs*-decorated classes.
243
244 Args:
245 inst: Instance of an *attrs*-decorated class.
246
247 recurse (bool):
248 Recurse into classes that are also *attrs*-decorated.
249
250 filter (~typing.Callable):
251 A callable whose return code determines whether an attribute or
252 element is included (`True`) or dropped (`False`). Is called with
253 the `attrs.Attribute` as the first argument and the value as the
254 second argument.
255
256 tuple_factory (~typing.Callable):
257 A callable to produce tuples from. For example, to produce lists
258 instead of tuples.
259
260 retain_collection_types (bool):
261 Do not convert to `list` or `dict` when encountering an attribute
262 which type is `tuple`, `dict` or `set`. Only meaningful if
263 *recurse* is `True`.
264
265 Returns:
266 Return type of *tuple_factory*
267
268 Raises:
269 attrs.exceptions.NotAnAttrsClassError:
270 If *cls* is not an *attrs* class.
271
272 .. versionadded:: 16.2.0
273 """
274 attrs = fields(inst.__class__)
275 rv = []
276 retain = retain_collection_types # Very long. :/
277 for a in attrs:
278 v = getattr(inst, a.name)
279 if filter is not None and not filter(a, v):
280 continue
281 value_type = type(v)
282 if recurse is True:
283 if value_type in _ATOMIC_TYPES:
284 rv.append(v)
285 elif has(value_type):
286 rv.append(
287 astuple(
288 v,
289 recurse=True,

Callers 13

test_shallowMethod · 0.90
test_recurseMethod · 0.90
test_recurse_propertyMethod · 0.90
test_recurse_retainMethod · 0.90
test_filterMethod · 0.90
test_lists_tuplesMethod · 0.90
test_dictsMethod · 0.90
test_roundtripMethod · 0.90
test_sets_no_retainMethod · 0.90
test_non_atomic_typesMethod · 0.90

Calls 2

fieldsFunction · 0.85
hasFunction · 0.85

Tested by 12

test_shallowMethod · 0.72
test_recurseMethod · 0.72
test_recurse_propertyMethod · 0.72
test_recurse_retainMethod · 0.72
test_filterMethod · 0.72
test_lists_tuplesMethod · 0.72
test_dictsMethod · 0.72
test_roundtripMethod · 0.72
test_sets_no_retainMethod · 0.72
test_non_atomic_typesMethod · 0.72