Skip to content

Commit beb3e96

Browse files
committed
Add design decisions section
1 parent f7b5924 commit beb3e96

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ destructure them.
129129
- `typing.MutableSet[T]`, `typing.Set[T]` (converts to a set).
130130
- `typing.FrozenSet[T]` (converts to a frozenset).
131131
- `typing.Dict[K, V]`, `typing.MutableMapping[K, V]`, `typing.Mapping[K, V]` (converts to a dict).
132+
- `typing.TypedDict`.
132133
- _attrs_ classes with simple attributes and the usual `__init__`.
133134

134135
- Simple attributes are attributes that can be assigned unstructured data,
@@ -142,6 +143,20 @@ destructure them.
142143
_cattrs_ comes with preconfigured converters for a number of serialization libraries, including json, msgpack, cbor2, bson, yaml and toml.
143144
For details, see the [cattr.preconf package](https://catt.rs/en/stable/preconf.html).
144145

146+
## Design Decisions
147+
148+
_cattrs_ is based on a few fundamental design decisions.
149+
150+
- Un/structuring rules are separate from the models.
151+
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.
152+
- Invent as little as possible; reuse existing ordinary Python instead.
153+
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).
154+
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_.
155+
- Refuse the temptation to guess.
156+
If there are two ways of solving a problem, _cattrs_ should refuse to guess and let the user configure it themselves.
157+
158+
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.
159+
145160
## Additional documentation and talks
146161

147162
- [On structured and unstructured data, or the case for cattrs](https://threeofwands.com/on-structured-and-unstructured-data-or-the-case-for-cattrs/)
@@ -151,8 +166,7 @@ For details, see the [cattr.preconf package](https://catt.rs/en/stable/preconf.h
151166

152167
## Credits
153168

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

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

0 commit comments

Comments
 (0)