{"id":7154,"library":"desert","title":"Deserialize to objects while staying DRY","description":"Desert is a Python library that generates serialization schemas for dataclasses and attrs classes, building upon the Marshmallow library. It aims to help developers write DRY (Don't Repeat Yourself) code by automatically deriving serialization/deserialization logic from class definitions. The library is actively maintained, with its current version being 2022.9.22.","status":"active","version":"2022.9.22","language":"en","source_language":"en","source_url":"https://github.com/python-desert/desert","tags":["serialization","dataclasses","attrs","marshmallow","DRY"],"install":[{"cmd":"pip install desert","lang":"bash","label":"Pip"},{"cmd":"poetry add desert","lang":"bash","label":"Poetry"}],"dependencies":[{"reason":"Core dependency for schema generation.","package":"marshmallow"},{"reason":"Optional dependency if using attrs classes instead of dataclasses.","package":"attrs","optional":true}],"imports":[{"symbol":"desert","correct":"import desert"},{"symbol":"dataclass","correct":"from dataclasses import dataclass"}],"quickstart":{"code":"from dataclasses import dataclass\nfrom typing import List\nimport desert\n\n@dataclass\nclass Person:\n    name: str\n    age: int\n\n@dataclass\nclass Car:\n    passengers: List[Person]\n\n# Load some simple data types.\ndata = {'passengers': [{'name': 'Alice', 'age': 21}, {'name': 'Bob', 'age': 22}]}\n\n# Create a schema for the Car class.\nschema = desert.schema(Car)\n\n# Load the data.\ncar = schema.load(data)\n\nassert car == Car(passengers=[Person(name='Alice', age=21), Person(name='Bob', age=22)])","lang":"python","description":"This quickstart demonstrates how to define dataclasses, create a Desert schema from them, and then load data into the defined objects."},"warnings":[{"fix":"Upgrade your Python environment to version 3.7 or newer.","message":"Python 3.6 support was dropped in version 2022.09.22 to allow updating internal dependencies.","severity":"breaking","affected_versions":">=2022.09.22"},{"fix":"Avoid using string annotations and forward references within function scopes for types processed by Desert. Ensure all types are fully resolved at definition time.","message":"Desert does not support string annotations and forward references inside of functions, which can lead to unexpected behavior or failures in schema generation.","severity":"gotcha","affected_versions":"<2022.09.22"},{"fix":"Review custom field implementations to ensure compatibility with `desert`'s type resolution. For `TYPE_CHECKING` blocks, ensure that `desert`-compatible type hints are available for runtime schema generation outside these blocks if they are critical.","message":"The library may encounter crashes or unexpected behavior when dealing with optional types that use custom fields or type hints encapsulated within `TYPE_CHECKING` blocks.","severity":"gotcha","affected_versions":"<2022.09.22"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Always use `Optional[Type]` (e.g., `Optional[str]`) and verify that any custom fields define their types in a way `desert` can resolve.","cause":"Incorrect usage of `typing.Optional` (e.g., `Optional` instead of `Optional[str]`) or conflicts arising from custom fields and `desert`'s type introspection.","error":"TypeError: An Optional type expects 1 subtype"},{"fix":"Ensure that type hints essential for schema generation are not hidden within `TYPE_CHECKING` blocks or use `from __future__ import annotations` and ensure type resolution works at runtime.","cause":"Desert failed to generate a valid schema for a dataclass, often due to unsupported type hints, especially those conditionally defined within `if typing.TYPE_CHECKING:` blocks.","error":"AttributeError: 'NoneType' object has no attribute 'load' (or similar errors during schema generation)"},{"fix":"When possible, explicitly create and reuse a single schema instance for a given dataclass to ensure consistency. Investigate how dataclass references are managed in complex scenarios.","cause":"Under certain conditions, `desert` might generate multiple distinct schema instances for what is conceptually the same dataclass, leading to inconsistent behavior.","error":"Different schema classes are generated per reference of the same data class."}]}