Utility functions

Utility methods for marshmallow.

marshmallow.utils.callable_or_raise(obj)[source]

Check that an object is callable, else raise a TypeError.

marshmallow.utils.get_value(obj, key, default=<marshmallow.missing>)[source]

Helper for pulling a keyed value off various types of objects. Fields use this method by default to access attributes of the source object. For object x and attribute i, this method first tries to access x[i], and then falls back to x.i if an exception is raised.

Warning

If an object x does not raise an exception when x[i] does not exist, get_value will never check the value x.i. Consider overriding marshmallow.fields.Field.get_value in this case.

Parameters:

key (int | str)

marshmallow.utils.is_collection(obj)[source]

Return True if obj is a collection type, e.g list, tuple, queryset.

Return type:

TypeGuard[Iterable]

marshmallow.utils.is_generator(obj)[source]

Return True if obj is a generator

Return type:

TypeGuard[Generator]

marshmallow.utils.is_iterable_but_not_string(obj)[source]

Return True if obj is an iterable object that isn’t a string.

Return type:

TypeGuard[Iterable]

marshmallow.utils.is_sequence_but_not_string(obj)[source]

Return True if obj is a sequence that isn’t a string.

Return type:

TypeGuard[Sequence]

marshmallow.utils.pluck(dictlist, key)[source]

Extracts a list of dictionary values from a list of dictionaries.

>>> dlist = [{'id': 1, 'name': 'foo'}, {'id': 2, 'name': 'bar'}]
>>> pluck(dlist, 'id')
[1, 2]
Parameters:
marshmallow.utils.set_value(dct, key, value)[source]

Set a value in a dict. If key contains a ‘.’, it is assumed be a path (i.e. dot-delimited string) to the value’s location.

>>> d = {}
>>> set_value(d, 'foo.bar', 42)
>>> d
{'foo': {'bar': 42}}
Parameters:
marshmallow.utils.timedelta_to_microseconds(value)[source]

Compute the total microseconds of a timedelta.

https://github.com/python/cpython/blob/v3.13.1/Lib/_pydatetime.py#L805-L807

Parameters:

value (timedelta)

Return type:

int