---
title: "cattrs | pypx"
description: "Composable complex class support for attrs and dataclasses."
canonical_url: "https://pypx.app/packages/cattrs"
meta:
  "og:description": "Composable complex class support for attrs and dataclasses."
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_cattrs%2Cversion_26.2.1%2Csummary_Composable%2Bcomplex%2Bclass%2Bsupport%2Bfor%2Battrs%2Band%2Bdataclasses.%2Clicense_MIT%2BLicense%2Cp_Ii9wYWNrYWdlcy9jYXR0cnMi%2Cs_w47ISKpjFQDm1D4P.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "cattrs 26.2.1"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_cattrs%2Cversion_26.2.1%2Csummary_Composable%2Bcomplex%2Bclass%2Bsupport%2Bfor%2Battrs%2Band%2Bdataclasses.%2Clicense_MIT%2BLicense%2Cp_Ii9wYWNrYWdlcy9jYXR0cnMi%2Cs_w47ISKpjFQDm1D4P.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **cattrs**

v26.2.1

Composable complex class support for attrs and dataclasses.

73 KB Python 3.10+ py3-none-anyMIT License3 deps

[**Diff **](https://pypx.app/packages/cattrs/diff)

`$ uv add cattrs`

# *cattrs*: Flexible Object Serialization and Validation

*Because validation belongs to the edges.*

[![Documentation](https://img.shields.io/badge/Docs-Read%20The%20Docs-black)](https://catt.rs/) [![License: MIT](https://img.shields.io/badge/license-MIT-C06524)](https://github.com/hynek/stamina/blob/main/LICENSE) [![PyPI](https://img.shields.io/pypi/v/cattrs.svg)](https://pypi.python.org/pypi/cattrs) [![Supported Python Versions](https://img.shields.io/pypi/pyversions/cattrs.svg)](https://github.com/python-attrs/cattrs) [![Downloads](https://static.pepy.tech/badge/cattrs/month)](https://pepy.tech/project/cattrs) [![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/Tinche/22405310d6a663164d894a2beab4d44d/raw/covbadge.json)](https://github.com/python-attrs/cattrs/actions/workflows/main.yml)

---

**cattrs** is a Swiss Army knife for (un)structuring and validating data in Python. In practice, that means it converts **unstructured dictionaries** into **proper classes** and back, while **validating** their contents.

## Example

*cattrs* works best with [*attrs*](https://www.attrs.org/) classes, and [dataclasses](https://docs.python.org/3/library/dataclasses.html) where simple (un-)structuring works out of the box, even for nested data, without polluting your data model with serialization details:

```
>>> from attrs import define
>>> from cattrs import structure, unstructure
>>> @define
... class C:
...     a: int
...     b: list[str]
>>> instance = structure({'a': 1, 'b': ['x', 'y']}, C)
>>> instance
C(a=1, b=['x', 'y'])
>>> unstructure(instance)
{'a': 1, 'b': ['x', 'y']}
```

Have a look at [*Why *cattrs*?*](https://catt.rs/en/latest/why.html) for more examples!

## Features

### Recursive Unstructuring

- *attrs* classes and dataclasses are converted into dictionaries in a way similar to `attrs.asdict()`, or into tuples in a way similar to `attrs.astuple()`.
- Enumeration instances are converted to their values.
- Other types are let through without conversion. This includes types such as integers, dictionaries, lists and instances of non-*attrs* classes.
- Custom converters for any type can be registered using `register_unstructure_hook`.

### Recursive Structuring

Converts unstructured data into structured data, recursively, according to your specification given as a type. The following types are supported:

- `typing.Optional[T]` and its 3.10+ form, `T | None`.
- `list[T]`, `typing.List[T]`, `typing.MutableSequence[T]`, `typing.Sequence[T]` convert to lists.
- `tuple` and `typing.Tuple` (both variants, `tuple[T, ...]` and `tuple[X, Y, Z]`).
- `set[T]`, `typing.MutableSet[T]`, and `typing.Set[T]` convert to sets.
- `frozenset[T]`, and `typing.FrozenSet[T]` convert to frozensets.
- `dict[K, V]`, `typing.Dict[K, V]`, `typing.MutableMapping[K, V]`, and `typing.Mapping[K, V]` convert to dictionaries.
- [`typing.TypedDict`](https://docs.python.org/3/library/typing.html#typing.TypedDict), both ordinary and generic.
- [`typing.NewType`](https://docs.python.org/3/library/typing.html#newtype)
- [PEP 695 type aliases](https://docs.python.org/3/library/typing.html#type-aliases) on 3.12+
- *attrs* classes with simple attributes and the usual `__init__`\[^simple].
- All *attrs* classes and dataclasses with the usual `__init__`, if their complex attributes have type metadata.
- Unions of supported *attrs* classes, given that all of the classes have a unique field.
- Unions of anything, if you provide a disambiguation function for it.
- Custom converters for any type can be registered using `register_structure_hook`.

\[^simple]: Simple attributes are attributes that can be assigned unstructured data, like numbers, strings, and collections of unstructured data.

### Batteries Included

*cattrs* comes with pre-configured converters for a number of serialization libraries, including JSON (standard library, [*orjson*](https://pypi.org/project/orjson/), [UltraJSON](https://pypi.org/project/ujson/)), [*msgpack*](https://pypi.org/project/msgpack/), [*cbor2*](https://pypi.org/project/cbor2/), [*bson*](https://pypi.org/project/bson/), [PyYAML](https://pypi.org/project/PyYAML/), [*tomlkit*](https://pypi.org/project/tomlkit/) and [*msgspec*](https://pypi.org/project/msgspec/) (supports only JSON at this time).

For details, see the [cattrs.preconf package](https://catt.rs/en/stable/preconf.html).

## Design Decisions

*cattrs* is based on a few fundamental design decisions:

- Un/structuring rules are separate from the models. This allows models to have a one-to-many relationship with un/structuring rules, and to create un/structuring rules for models which you do not own and you cannot change. (*cattrs* can be configured to use un/structuring rules from models using the [`use_class_methods` strategy](https://catt.rs/en/latest/strategies.html#using-class-specific-structure-and-unstructure-methods).)
- Invent as little as possible; reuse existing ordinary Python instead. For example, *cattrs* did not have a custom exception type to group exceptions until the sanctioned Python [`exceptiongroups`](https://docs.python.org/3/library/exceptions.html#ExceptionGroup). 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*.
- Resist the temptation to guess. If there are two ways of solving a problem, *cattrs* should refuse to guess and let the user configure it themselves.

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.

## Credits

Major credits to Hynek Schlawack for creating [attrs](https://attrs.org) and its predecessor, [characteristic](https://github.com/hynek/characteristic).

*cattrs* is tested with [Hypothesis](http://hypothesis.readthedocs.io/en/latest/), by David R. MacIver.

*cattrs* is benchmarked using [perf](https://github.com/haypo/perf) and [pytest-benchmark](https://pytest-benchmark.readthedocs.io/en/latest/index.html).

This package was created with [Cookiecutter](https://github.com/audreyr/cookiecutter) and the [`audreyr/cookiecutter-pypackage`](https://github.com/audreyr/cookiecutter-pypackage) project template.

## **Details **

<dl>

<dt>Version</dt>
<dd>26.2.1</dd>

<dt>License</dt>
<dd>MIT License</dd>

<dt>Python</dt>
<dd>&gt;=3.10</dd>

<dt>Maintainer</dt>
<dd>Tin Tvrtkovic &lt;tinchester@gmail.com&gt;</dd></dl>

## **Links**

- [Bug Tracker](https://github.com/python-attrs/cattrs/issues)
- [Changelog](https://catt.rs/en/latest/history.html)
- [Documentation](https://catt.rs/en/stable/)
- [Homepage](https://catt.rs)
- [Repository](https://github.com/python-attrs/cattrs)
- [View on PyPI](https://pypi.org/project/cattrs/)

## **Release Cadence **

**3**

releases in the past year

avg 75 days between releases

## **Maintainers **

- Tin Tvrtkovic · [tinchester@gmail.com](mailto:tinchester@gmail.com)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/cattrs#webpage","@type":"WebPage","description":"Composable complex class support for attrs and dataclasses.","name":"cattrs","url":"https://pypx.app/packages/cattrs","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/cattrs"]}]},{"@id":"https://pypx.app/packages/cattrs#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Composable complex class support for attrs and dataclasses.","license":"MIT License","name":"cattrs","softwareVersion":"26.2.1"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
