Desert generates serialization schemas for dataclasses and attrs classes. Writing code that’s DRY (“don’t repeat yourself”) helps avoid bugs and improve readability. Desert helps you write code that’s DRY.
Installation
pip install desert
or with Poetry
poetry add desert
Usage
A simple example models two Person objects in a Car.
from dataclasses import dataclass
# Or using attrs
# from attr import dataclass
from typing import List
import desert
@dataclass
class Person:
name: str
age: int
@dataclass
class Car:
passengers: List[Person]
# Load some simple data types.
data = {'passengers': [{'name': 'Alice', 'age': 21}, {'name': 'Bob', 'age': 22}]}
# Create a schema for the Car class.
schema = desert.schema(Car)
# Load the data.
car = schema.load(data)
assert car == Car(passengers=[Person(name='Alice', age=21), Person(name='Bob', age=22)])
Documentation
Limitations
String annotations and forward references inside of functions are not supported.
Acknowledgements
This package began as an extension of marshmallow-dataclass to add support for attrs.
2022.09.22 (2022-09-22)
Backward-incompatible Changes
Update all project dependencies and fix all deprecated warnings. Python 3.6 support was dropped to allow updating deprecated dependencies. #161
Changes
It is now possible to use type-variant generics in your dataclasses, such as Sequence or MutableSequence instead of List, Mapping instead of Dict, etc.
This allows you to hide implementation details from users of your dataclasses. If a field in your dataclass works just as fine with a tuple as a list, you no longer need to force your users to pass in a list just to satisfy type checkers.
For example, by using Mapping or MutableMapping, users can pass OrderedDict to a Dict attribute without MyPy complaining.
@dataclass class OldWay: str_list: List[str] num_map: Dict[str, float] # MyPy will reject this even though Marshmallow works just fine. If you use # type-variant generics, MyPy will accept this code. instance = OldClass([], collections.ChainMap(MY_DEFAULTS)) @dataclass class NewWay: str_list: List[str] # Type-invariants still work num_map: MutableMapping[str, float] # Now generics do too
2020.11.18 (2020-11-18)
Changes
Schemas no longer copy non-field dataclass attributes. Thanks to @sveinse for report and test. #79
2020.01.06 (2020-01-06)
Changes
Additional metadata are supported in ib() and fields(). Thanks to @sveinse for reporting and testing. #28
2020.01.05 (2020-01-05)
Changes
Add support for attrs factories that take self (attr.Factory(..., takes_self=True)). #27
2020.01.04 (2020-01-04)
Changes
Add support for Tuple[int, …] per https://docs.python.org/3/library/typing.html#typing.Tuple #16 Thanks to @sveinse for reporting and testing.
Use module imports internally. #18
Desert version only needs to be updated in _version.py #19
Fix doctests. #20
2020.01.03
Changes
Optional fields allow None. #11. Thanks to @sveinse for reporting and testing.
2019.12.18
Changes
Improve error message for unknown generics. #10
2019.12.10
Changes
Add UnknownType exception with better error message for types that should be generic. #8
2019.12.09
Changes
Marshmallow schema Meta arguments are accepted, allowing exclusion of unknown fields and other options. #3
2019.11.06 (2019-11-06)
Changes
Add twine and wheel development dependencies. #2
2019.11.06 (2019-11-06)
Changes
Switch to calver
Backward-incompatible Changes
Non-optional fields without a default or factory now have required=True so raise marshmallow.exceptions.ValidationError when missing. #1
0.1.0 (2019-06-22)
Changes
First release on PyPI.
—
Metadata
Release files for desert 2022.9.22
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| desert-2022.9.22.tar.gz | 66.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| desert-2022.9.22-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 77.6 kB
Release files / desert-2022.9.22.tar.gz
| Download URL | desert-2022.9.22.tar.gz |
|---|---|
| Size | 66.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0f45e098915e16452a269a1da0bcbbf31df0af2173899f1c097fb2e3b0265d89
|
|
BLAKE2b-256 checksum How to use checksums |
e1b3919b5483c66ddae7f0fbbdc04cac9930e163aef9c86641a77585b5d7fda0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / desert-2022.9.22-py3-none-any.whl
| Download URL | desert-2022.9.22-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cad7b6f1936448d26bde403882ec6855786b4d24d38d0ed4400e505ac8c5591f
|
|
BLAKE2b-256 checksum How to use checksums |
d748060b6678ede661cfaf4ffaed176b4bae51c88bb915200d56f5bc9d721d9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|