Composable complex class support for attrs and dataclasses.
cattrs is an open source Python library for structuring and unstructuring data. cattrs works best with attrs classes, dataclasses and the usual Python collections, but other kinds of classes are supported by manually registering converters.
Python has a rich set of powerful, easy to use, built-in data types like dictionaries, lists and tuples. These data types are also the lingua franca of most data serialization libraries, for formats like json, msgpack, cbor, yaml or toml.
Data types like this, and mappings like dict
s in particular, represent
unstructured data. Your data is, in all likelihood, structured: not all
combinations of field names or values are valid inputs to your programs. In
Python, structured data is better represented with classes and enumerations.
attrs is an excellent library for declaratively describing the structure of
your data, and validating it.
When you're handed unstructured data (by your network, file system, database...), cattrs helps to convert this data into structured data. When you have to convert your structured data into data types other libraries can handle, cattrs turns your classes and enumerations into dictionaries, integers and strings.
Here's a simple taste. The list containing a float, an int and a string gets converted into a tuple of three ints.
>>> import cattrs
>>> cattrs.structure([1.0, 2, "3"], tuple[int, int, int])
(1, 2, 3)
cattrs works well with attrs classes out of the box.
>>> from attrs import frozen
>>> import cattrs
>>> @frozen # It works with non-frozen classes too.
... class C:
... a: int
... b: str
>>> instance = C(1, 'a')
>>> cattrs.unstructure(instance)
{'a': 1, 'b': 'a'}
>>> cattrs.structure({'a': 1, 'b': 'a'}, C)
C(a=1, b='a')
Here's a much more complex example, involving attrs
classes with type
metadata.
>>> from enum import unique, Enum
>>> from typing import Optional, Sequence, Union
>>> from cattrs import structure, unstructure
>>> from attrs import define, field
>>> @unique
... class CatBreed(Enum):
... SIAMESE = "siamese"
... MAINE_COON = "maine_coon"
... SACRED_BIRMAN = "birman"
>>> @define
... class Cat:
... breed: CatBreed
... names: Sequence[str]
>>> @define
... class DogMicrochip:
... chip_id = field() # Type annotations are optional, but recommended
... time_chipped: float = field()
>>> @define
... class Dog:
... cuteness: int
... chip: Optional[DogMicrochip] = None
>>> p = unstructure([Dog(cuteness=1, chip=DogMicrochip(chip_id=1, time_chipped=10.0)),
... Cat(breed=CatBreed.MAINE_COON, names=('Fluffly', 'Fluffer'))])
>>> print(p)
[{'cuteness': 1, 'chip': {'chip_id': 1, 'time_chipped': 10.0}}, {'breed': 'maine_coon', 'names': ('Fluffly', 'Fluffer')}]
>>> print(structure(p, list[Union[Dog, Cat]]))
[Dog(cuteness=1, chip=DogMicrochip(chip_id=1, time_chipped=10.0)), Cat(breed=<CatBreed.MAINE_COON: 'maine_coon'>, names=['Fluffly', 'Fluffer'])]
Consider unstructured data a low-level representation that needs to be converted
to structured data to be handled, and use structure
. When you're done,
unstructure
the data to its unstructured form and pass it along to another
library or module. Use attrs type metadata
to add type metadata to attributes, so cattrs will know how to structure and
destructure them.
Converts structured data into unstructured data, recursively:
attrs.asdict
, or into tuples in a way similar to attrs.astuple
.register_unstructure_hook
.Converts unstructured data into structured data, recursively, according to your specification given as a type. The following types are supported:
typing.Optional[T]
.
typing.List[T]
, typing.MutableSequence[T]
, typing.Sequence[T]
(converts to a list).
typing.Tuple
(both variants, Tuple[T, ...]
and Tuple[X, Y, Z]
).
typing.MutableSet[T]
, typing.Set[T]
(converts to a set).
typing.FrozenSet[T]
(converts to a frozenset).
typing.Dict[K, V]
, typing.MutableMapping[K, V]
, typing.Mapping[K, V]
(converts to a dict).
typing.TypedDict
.
attrs classes with simple attributes and the usual __init__
.
All attrs classes and dataclasses with the usual __init__
, if their complex attributes have type metadata.
typing.Union
s of supported attrs classes, given that all of the classes have a unique field.
typing.Union
s of anything, given that you provide a disambiguation function for it.
Custom converters for any type can be registered using register_structure_hook
.
cattrs comes with preconfigured converters for a number of serialization libraries, including json, msgpack, cbor2, bson, yaml and toml. For details, see the cattrs.preconf package.
cattrs is based on a few fundamental design decisions.
use_class_methods
strategy.)exceptiongroups
.
A side-effect of this design decision is that, in a lot of cases, when you're solving cattrs problems you're actually learning Python instead of learning cattrs.A foolish consistency is the hobgoblin of little minds so these decisions can and are sometimes broken, but they have proven to be a good foundation.
Major credits to Hynek Schlawack for creating attrs and its predecessor, characteristic.
cattrs is tested with Hypothesis, by David R. MacIver.
cattrs is benchmarked using perf and pytest-benchmark.
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage
project template.