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

Function asdict

src/attr/_funcs.py:28–151  ·  view source on GitHub ↗

Return the *attrs* attribute values of *inst* as a dict. 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 (~typing.Callabl

(
    inst,
    recurse=True,
    filter=None,
    dict_factory=dict,
    retain_collection_types=False,
    value_serializer=None,
)

Source from the content-addressed store, hash-verified

26
27
28def asdict(
29 inst,
30 recurse=True,
31 filter=None,
32 dict_factory=dict,
33 retain_collection_types=False,
34 value_serializer=None,
35):
36 """
37 Return the *attrs* attribute values of *inst* as a dict.
38
39 Optionally recurse into other *attrs*-decorated classes.
40
41 Args:
42 inst: Instance of an *attrs*-decorated class.
43
44 recurse (bool): Recurse into classes that are also *attrs*-decorated.
45
46 filter (~typing.Callable):
47 A callable whose return code determines whether an attribute or
48 element is included (`True`) or dropped (`False`). Is called with
49 the `attrs.Attribute` as the first argument and the value as the
50 second argument.
51
52 dict_factory (~typing.Callable):
53 A callable to produce dictionaries from. For example, to produce
54 ordered dictionaries instead of normal Python dictionaries, pass in
55 ``collections.OrderedDict``.
56
57 retain_collection_types (bool):
58 Do not convert to `list` when encountering an attribute whose type
59 is `tuple` or `set`. Only meaningful if *recurse* is `True`.
60
61 value_serializer (typing.Callable | None):
62 A hook that is called for every attribute or dict key/value. It
63 receives the current instance, field and value and must return the
64 (updated) value. The hook is run *after* the optional *filter* has
65 been applied.
66
67 Returns:
68 Return type of *dict_factory*.
69
70 Raises:
71 attrs.exceptions.NotAnAttrsClassError:
72 If *cls* is not an *attrs* class.
73
74 .. versionadded:: 16.0.0 *dict_factory*
75 .. versionadded:: 16.1.0 *retain_collection_types*
76 .. versionadded:: 20.3.0 *value_serializer*
77 .. versionadded:: 21.3.0
78 If a dict has a collection for a key, it is serialized as a tuple.
79 """
80 attrs = fields(inst.__class__)
81 rv = dict_factory()
82 for a in attrs:
83 v = getattr(inst, a.name)
84 if filter is not None and not filter(a, v):
85 continue

Callers 14

test_shallowMethod · 0.90
test_recurseMethod · 0.90
test_non_atomic_typesMethod · 0.90
test_nested_listsMethod · 0.90
test_nested_dictsMethod · 0.90
test_recurse_propertyMethod · 0.90
test_filterMethod · 0.90
test_lists_tuplesMethod · 0.90
test_sets_no_retainMethod · 0.90
test_dictsMethod · 0.90
test_roundtripMethod · 0.90

Calls 3

fieldsFunction · 0.85
hasFunction · 0.85
_asdict_anythingFunction · 0.85

Tested by 13

test_shallowMethod · 0.72
test_recurseMethod · 0.72
test_non_atomic_typesMethod · 0.72
test_nested_listsMethod · 0.72
test_nested_dictsMethod · 0.72
test_recurse_propertyMethod · 0.72
test_filterMethod · 0.72
test_lists_tuplesMethod · 0.72
test_sets_no_retainMethod · 0.72
test_dictsMethod · 0.72
test_roundtripMethod · 0.72