Package: desert
Desert generates serialization schemas fordataclassesandattrsclasses. Writing
code that’s DRY (“don’t repeat yourself”) helps avoid bugs and improve readability. Desert
helps you write code that’s DRY.Installationpip install desertor withPoetrypoetry add desertUsageA simple example models twoPersonobjects in aCar.fromdataclassesimportdataclass# Or using attrs# from attr import dataclassfromtypingimportListimportdesert@dataclassclassPerson:name:strage:int@dataclassclassCar: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)assertcar==Car(passengers=[Person(name='Alice',age=21),Person(name='Bob',age=22)])Documentationhttps://desert.readthedocs.io/LimitationsString annotations and forward references inside of functions are not supported.AcknowledgementsThis package began as an extension ofmarshmallow-dataclassto add support forattrs.2022.09.22 (2022-09-22)Backward-incompatible ChangesUpdate all project dependencies and fix all deprecated warnings. Python 3.6
support was dropped to allow updating deprecated dependencies.#161ChangesIt is now possible to usetype-variant genericsin your dataclasses, such asSequenceorMutableSequenceinstead ofList,Mappinginstead ofDict, 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 alistjust to satisfy type checkers.For example, by usingMappingorMutableMapping, users can passOrderedDictto
aDictattribute without MyPy complaining.@dataclassclassOldWay: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))@dataclassclassNewWay:str_list:List[str]# Type-invariants still worknum_map:MutableMapping[str,float]# Now generics do too#1402020.11.18 (2020-11-18)ChangesSchemas no longer copy non-field dataclass attributes. Thanks to @sveinse for report and test.#792020.01.06 (2020-01-06)ChangesAdditional metadata are supported in ib() and fields(). Thanks to @sveinse for reporting and testing.#282020.01.05 (2020-01-05)ChangesAdd support for attrs factories that takeself(attr.Factory(...,takes_self=True)).#272020.01.04 (2020-01-04)ChangesAdd support for Tuple[int, …] perhttps://docs.python.org/3/library/typing.html#typing.Tuple#16Thanks to @sveinse for reporting and testing.Use module imports internally.#18Desert version only needs to be updated in _version.py#19Fix doctests.#202020.01.03ChangesOptionalfields allowNone.#11. Thanks to @sveinse for reporting and testing.2019.12.18ChangesImprove error message for unknown generics.#102019.12.10ChangesAddUnknownTypeexception with better error message for types that should be generic.#82019.12.09ChangesMarshmallow schemaMetaarguments are accepted, allowing exclusion of unknown fields and other options.#32019.11.06 (2019-11-06)ChangesAdd twine and wheel development dependencies.#22019.11.06 (2019-11-06)ChangesSwitch to calverBackward-incompatible ChangesNon-optional fields without a default or factory now haverequired=Trueso raisemarshmallow.exceptions.ValidationErrorwhen missing.#10.1.0 (2019-06-22)ChangesFirst release on PyPI.—