What You Can Structure and How#

The philosophy of cattrs structuring is simple: give it an instance of Python built-in types and collections, and a type describing the data you want out. cattrs will convert the input data into the type you want, or throw an exception.

All structuring conversions are composable, where applicable. This is demonstrated further in the examples.

Primitive Values#

typing.Any#

Use typing.Any to avoid applying any conversions to the object you’re structuring; it will simply be passed through.

>>> cattrs.structure(1, Any)
1
>>> d = {1: 1}
>>> cattrs.structure(d, Any) is d
True

int, float, str, bytes#

Use any of these primitive types to convert the object to the type.

>>> cattrs.structure(1, str)
'1'
>>> cattrs.structure("1", float)
1.0

In case the conversion isn’t possible, the expected exceptions will be propagated out. The particular exceptions are the same as if you’d tried to do the conversion yourself, directly.

>>> cattrs.structure("not-an-int", int)
Traceback (most recent call last):
...
ValueError: invalid literal for int() with base 10: 'not-an-int'

Enums#

Enums will be structured by their values. This works even for complex values, like tuples.

>>> @unique
... class CatBreed(Enum):
...    SIAMESE = "siamese"
...    MAINE_COON = "maine_coon"
...    SACRED_BIRMAN = "birman"

>>> cattrs.structure("siamese", CatBreed)
<CatBreed.SIAMESE: 'siamese'>

Again, in case of errors, the expected exceptions will fly out.

>>> cattrs.structure("alsatian", CatBreed)
Traceback (most recent call last):
...
ValueError: 'alsatian' is not a valid CatBreed

pathlib.Path#

pathlib.Path objects are structured using their string value.

>>> from pathlib import Path

>>> cattrs.structure("/root", Path)
PosixPath('/root')

In case the conversion isn’t possible, the resulting exception is propagated out.

New in version 23.1.0.

Collections and Other Generics#

Optionals#

Optional primitives and collections are supported out of the box.

>>> cattrs.structure(None, int)
Traceback (most recent call last):
...
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
>>> cattrs.structure(None, Optional[int])
>>> # None was returned.

Bare Optional s (non-parameterized, just Optional, as opposed to Optional[str]) aren’t supported, use Optional[Any] instead.

The Python 3.10 more readable syntax, str | None instead of Optional[str], is also supported.

This generic type is composable with all other converters.

>>> cattrs.structure(1, Optional[float])
1.0

Lists#

Lists can be produced from any iterable object. Types converting to lists are:

  • Sequence[T]

  • MutableSequence[T]

  • List[T]

  • list[T]

In all cases, a new list will be returned, so this operation can be used to copy an iterable into a list. A bare type, for example Sequence instead of Sequence[int], is equivalent to Sequence[Any].

>>> cattrs.structure((1, 2, 3), MutableSequence[int])
[1, 2, 3]

These generic types are composable with all other converters.

>>> cattrs.structure((1, None, 3), list[Optional[str]])
['1', None, '3']

Deques#

Deques can be produced from any iterable object. Types converting to deques are:

  • Deque[T]

  • deque[T]

In all cases, a new unbounded deque (maxlen=None) will be returned, so this operation can be used to copy an iterable into a deque. If you want to convert into bounded deque, registering a custom structuring hook is a good approach.

>>> from collections import deque
>>> cattrs.structure((1, 2, 3), deque[int])
deque([1, 2, 3])

These generic types are composable with all other converters.

>>> cattrs.structure((1, None, 3), deque[Optional[str]])
deque(['1', None, '3'])

New in version 23.1.0.

Sets and Frozensets#

Sets and frozensets can be produced from any iterable object. Types converting to sets are:

  • Set[T]

  • MutableSet[T]

  • set[T]

Types converting to frozensets are:

  • FrozenSet[T]

  • frozenset[T]

In all cases, a new set or frozenset will be returned, so this operation can be used to copy an iterable into a set. A bare type, for example MutableSet instead of MutableSet[int], is equivalent to MutableSet[Any].

>>> cattrs.structure([1, 2, 3, 4], Set)
{1, 2, 3, 4}

These generic types are composable with all other converters.

>>> cattrs.structure([[1, 2], [3, 4]], set[frozenset[str]])
{frozenset({'2', '1'}), frozenset({'4', '3'})}

Dictionaries#

Dicts can be produced from other mapping objects. To be more precise, the object being converted must expose an items() method producing an iterable key-value tuples, and be able to be passed to the dict constructor as an argument. Types converting to dictionaries are:

  • Dict[K, V]

  • MutableMapping[K, V]

  • Mapping[K, V]

  • dict[K, V]

In all cases, a new dict will be returned, so this operation can be used to copy a mapping into a dict. Any type parameters set to typing.Any will be passed through unconverted. If both type parameters are absent, they will be treated as Any too.

>>> from collections import OrderedDict
>>> cattrs.structure(OrderedDict([(1, 2), (3, 4)]), Dict)
{1: 2, 3: 4}

These generic types are composable with all other converters. Note both keys and values can be converted.

>>> cattrs.structure({1: None, 2: 2.0}, dict[str, Optional[int]])
{'1': None, '2': 2}

Typed Dicts#

TypedDicts can be produced from mapping objects, usually dictionaries.

>>> from typing import TypedDict

>>> class MyTypedDict(TypedDict):
...    a: int

>>> cattrs.structure({"a": "1"}, MyTypedDict)
{'a': 1}

Both total and non-total TypedDicts are supported, and inheritance between any combination works (except on 3.8 when typing.TypedDict is used, see below). Generic TypedDicts work on Python 3.11 and later, since that is the first Python version that supports them in general.

typing.Required and typing.NotRequired are supported.

On Python 3.8, using typing_extensions.TypedDict is recommended since typing.TypedDict doesn’t support all necessary features, so certain combinations of subclassing, totality and typing.Required won’t work.

Similar to attrs classes, structuring can be customized using cattrs.gen.typeddicts.make_dict_structure_fn().

>>> from typing import TypedDict
>>> from cattrs import Converter
>>> from cattrs.gen import override
>>> from cattrs.gen.typeddicts import make_dict_structure_fn

>>> class MyTypedDict(TypedDict):
...     a: int
...     b: int

>>> c = Converter()
>>> c.register_structure_hook(
...     MyTypedDict,
...     make_dict_structure_fn(
...         MyTypedDict,
...         c,
...         a=override(rename="a-with-dash")
...     )
... )

>>> c.structure({"a-with-dash": 1, "b": 2}, MyTypedDict)
{'b': 2, 'a': 1}

New in version 23.1.0.

Homogeneous and Heterogeneous Tuples#

Homogeneous and heterogeneous tuples can be produced from iterable objects. Heterogeneous tuples require an iterable with the number of elements matching the number of type parameters exactly. Use:

  • Tuple[A, B, C, D]

  • tuple[A, B, C, D]

Homogeneous tuples use:

  • Tuple[T, ...]

  • tuple[T, ...]

In all cases a tuple will be returned. Any type parameters set to typing.Any will be passed through unconverted.

>>> cattrs.structure([1, 2, 3], tuple[int, str, float])
(1, '2', 3.0)

The tuple conversion is composable with all other converters.

>>> cattrs.structure([{1: 1}, {2: 2}], tuple[dict[str, float], ...])
({'1': 1.0}, {'2': 2.0})

Unions#

Unions of NoneType and a single other type are supported (also known as Optional s). All other unions require a disambiguation function.

Automatic Disambiguation#

In the case of a union consisting exclusively of attrs classes, cattrs will attempt to generate a disambiguation function automatically; this will succeed only if each class has a unique field. Given the following classes:

>>> @define
... class A:
...     a = field()
...     x = field()

>>> @define
... class B:
...     a = field()
...     y = field()

>>> @define
... class C:
...     a = field()
...     z = field()

cattrs can deduce only instances of A will contain x, only instances of B will contain y, etc. A disambiguation function using this information will then be generated and cached. This will happen automatically, the first time an appropriate union is structured.

Manual Disambiguation#

To support arbitrary unions, register a custom structuring hook for the union (see Registering custom structuring hooks).

Another option is to use a custom tagged union strategy (see Strategies - Tagged Unions).

typing.Final#

PEP 591 Final attribute types (Final[int]) are supported and structured appropriately.

New in version 23.1.0.

typing.Annotated#

PEP 593 annotations (typing.Annotated[type, ...]) are supported and are matched using the first type present in the annotated type.

typing.NewType#

NewTypes are supported and are structured according to the rules for their underlying type. Their hooks can also be overriden using Converter.register_structure_hook().

>>> from typing import NewType
>>> from datetime import datetime

>>> IsoDate = NewType("IsoDate", datetime)

>>> converter = cattrs.Converter()
>>> converter.register_structure_hook(IsoDate, lambda v, _: datetime.fromisoformat(v))

>>> converter.structure("2022-01-01", IsoDate)
datetime.datetime(2022, 1, 1, 0, 0)

New in version 22.2.0.

Note

NewTypes are not supported by the legacy BaseConverter.

attrs Classes and Dataclasses#

Simple attrs Classes and Dataclasses#

attrs classes and dataclasses using primitives, collections of primitives and their own converters work out of the box. Given a mapping d and class A, cattrs will simply instantiate A with d unpacked.

>>> @define
... class A:
...     a: int
...     b: int

>>> cattrs.structure({'a': 1, 'b': '2'}, A)
A(a=1, b=2)

Classes like these deconstructed into tuples can be structured using structure_attrs_fromtuple() (fromtuple as in the opposite of attr.astuple and converter.unstructure_attrs_astuple).

>>> @define
... class A:
...     a: str
...     b: int

>>> cattrs.structure_attrs_fromtuple(['string', '2'], A)
A(a='string', b=2)

Loading from tuples can be made the default by creating a new Converter with unstruct_strat=cattr.UnstructureStrategy.AS_TUPLE.

>>> converter = cattrs.Converter(unstruct_strat=cattr.UnstructureStrategy.AS_TUPLE)
>>> @define
... class A:
...     a: str
...     b: int

>>> converter.structure(['string', '2'], A)
A(a='string', b=2)

Structuring from tuples can also be made the default for specific classes only; see registering custom structure hooks below.

Using Attribute Types and Converters#

By default, structure() will use hooks registered using register_structure_hook(), to convert values to the attribute type, and fallback to invoking any converters registered on attributes with attrib.

>>> from ipaddress import IPv4Address, ip_address
>>> converter = cattrs.Converter()

# Note: register_structure_hook has not been called, so this will fallback to 'ip_address'
>>> @define
... class A:
...     a: IPv4Address = field(converter=ip_address)

>>> converter.structure({'a': '127.0.0.1'}, A)
A(a=IPv4Address('127.0.0.1'))

Priority is still given to hooks registered with register_structure_hook(), but this priority can be inverted by setting prefer_attrib_converters to True.

>>> converter = cattrs.Converter(prefer_attrib_converters=True)

>>> converter.register_structure_hook(int, lambda v, t: int(v))

>>> @define
... class A:
...     a: int = field(converter=lambda v: int(v) + 5)

>>> converter.structure({'a': '10'}, A)
A(a=15)

Complex attrs Classes and Dataclasses#

Complex attrs classes and dataclasses are classes with type information available for some or all attributes. These classes support almost arbitrary nesting.

Type information is supported by attrs directly, and can be set using type annotations when using Python 3.6+, or by passing the appropriate type to attr.ib.

>>> @define
... class A:
...     a: int

>>> attr.fields(A).a
Attribute(name='a', default=NOTHING, validator=None, repr=True, eq=True, eq_key=None, order=True, order_key=None, hash=None, init=True, metadata=mappingproxy({}), type=<class 'int'>, converter=None, kw_only=False, inherited=False, on_setattr=None, alias='a')

Type information, when provided, can be used for all attribute types, not only attributes holding attrs classes and dataclasses.

>>> @define
... class A:
...     a: int = 0

>>> @define
... class B:
...     b: A

>>> cattrs.structure({'b': {'a': '1'}}, B)
B(b=A(a=1))

Finally, if an attrs or dataclass class uses inheritance and as such has one or several subclasses, it can be structured automatically to its exact subtype by using the include subclasses strategy.

Registering Custom Structuring Hooks#

cattrs doesn’t know how to structure non-attrs classes by default, so it has to be taught. This can be done by registering structuring hooks on a converter instance (including the global converter).

Here’s an example involving a simple, classic (i.e. non-attrs) Python class.

>>> class C:
...     def __init__(self, a):
...         self.a = a
...     def __repr__(self):
...         return f'C(a={self.a})'

>>> cattrs.structure({'a': 1}, C)
Traceback (most recent call last):
...
StructureHandlerNotFoundError: Unsupported type: <class '__main__.C'>. Register a structure hook for it.

>>> cattrs.register_structure_hook(C, lambda d, t: C(**d))
>>> cattrs.structure({'a': 1}, C)
C(a=1)

The structuring hooks are callables that take two arguments: the object to convert to the desired class and the type to convert to. (The type may seem redundant but is useful when dealing with generic types.)

When using cattrs.register_structure_hook(), the hook will be registered on the global converter. If you want to avoid changing the global converter, create an instance of cattrs.Converter and register the hook on that.

In some situations, it is not possible to decide on the converter using typing mechanisms alone (such as with attrs classes). In these situations, cattrs provides a register_unstructure_hook_func() hook instead, which accepts a predicate function to determine whether that type can be handled instead.

The function-based hooks are evaluated after the class-based hooks. In the case where both a class-based hook and a function-based hook are present, the class-based hook will be used.

>>> class D:
...     custom = True
...     def __init__(self, a):
...         self.a = a
...     def __repr__(self):
...         return f'D(a={self.a})'
...     @classmethod
...     def deserialize(cls, data):
...         return cls(data["a"])

>>> cattrs.register_structure_hook_func(
...     lambda cls: getattr(cls, "custom", False), lambda d, t: t.deserialize(d)
... )

>>> cattrs.structure({'a': 2}, D)
D(a=2)

Structuring Hook Factories#

Hook factories operate one level higher than structuring hooks; structuring hooks are functions registered to a class or predicate, and hook factories are functions (registered via a predicate) that produce structuring hooks.

Structuring hooks factories are registered using Converter.register_structure_hook_factory().

Here’s a small example showing how to use factory hooks to apply the forbid_extra_keys to all attrs classes:

>>> from attrs import define, has
>>> from cattrs.gen import make_dict_structure_fn

>>> c = cattrs.Converter()
>>> c.register_structure_hook_factory(
...     has,
...     lambda cl: make_dict_structure_fn(
...         cl, c, _cattrs_forbid_extra_keys=True, _cattrs_detailed_validation=False
...     )
... )

>>> @define
... class E:
...    an_int: int

>>> c.structure({"an_int": 1, "else": 2}, E)
Traceback (most recent call last):
...
cattrs.errors.ForbiddenExtraKeysError: Extra fields in constructor for E: else

A complex use case for hook factories is described over at Using Factory Hooks.