1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
# marshmallow-dataclass
[](https://github.com/lovasoa/marshmallow_dataclass/actions/workflows/python-package.yml)
[](https://badge.fury.io/py/marshmallow-dataclass)
[](https://marshmallow.readthedocs.io/en/latest/upgrading.html)
[](https://pypistats.org/packages/marshmallow-dataclass)
Automatic generation of [marshmallow](https://marshmallow.readthedocs.io/) schemas from dataclasses.
```python
from dataclasses import dataclass, field
from typing import List, Optional
import marshmallow_dataclass
import marshmallow.validate
@dataclass
class Building:
# field metadata is used to instantiate the marshmallow field
height: float = field(metadata={"validate": marshmallow.validate.Range(min=0)})
name: str = field(default="anonymous")
@dataclass
class City:
name: Optional[str]
buildings: List[Building] = field(default_factory=list)
city_schema = marshmallow_dataclass.class_schema(City)()
city = city_schema.load(
{"name": "Paris", "buildings": [{"name": "Eiffel Tower", "height": 324}]}
)
# => City(name='Paris', buildings=[Building(height=324.0, name='Eiffel Tower')])
city_dict = city_schema.dump(city)
# => {'name': 'Paris', 'buildings': [{'name': 'Eiffel Tower', 'height': 324.0}]}
```
## Why
Using schemas in Python often means having both a class to represent your data and a class to represent its schema, which results in duplicated code that could fall out of sync.
As of Python 3.6, types can be defined for class members, which allows libraries to generate schemas automatically.
Therefore, you can document your APIs in a way that allows you to statically check that the code matches the documentation.
## Installation
This package [is hosted on PyPI](https://pypi.org/project/marshmallow-dataclass/).
```shell
pip3 install marshmallow-dataclass
```
```shell
pip3 install "marshmallow-dataclass"
```
### marshmallow 2 support
`marshmallow-dataclass` no longer supports marshmallow 2.
Install `marshmallow_dataclass<6.0` if you need marshmallow 2 compatibility.
## Usage
Use the [`class_schema`](https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html#marshmallow_dataclass.class_schema)
function to generate a marshmallow [Schema](https://marshmallow.readthedocs.io/en/latest/api_reference.html#marshmallow.Schema)
class from a [`dataclass`](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass).
```python
from dataclasses import dataclass
from datetime import date
import marshmallow_dataclass
@dataclass
class Person:
name: str
birth: date
PersonSchema = marshmallow_dataclass.class_schema(Person)
```
The type of your fields must be either basic
[types supported by marshmallow](https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.Schema.TYPE_MAPPING)
(such as `float`, `str`, `bytes`, `datetime`, ...), `Union`, or other dataclasses.
### Union (de)serialization coercion
Typically the Union type; `Union[X, Y]` means—from a set theory perspective—either `X` or `Y`, i.e., an unordered set, howevever the order of the sub-types defines the precedence when attempting to ether deserialize or serialize the value per [here](https://github.com/lovasoa/marshmallow_dataclass/blob/master/marshmallow_dataclass/union_field.py).
For example,
```python
from typing import Union
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: Union[int, float]
PersonSchema = marshmallow_dataclass.class_schema(Person)
PersonSchema().load({"name": "jane", "age": 50.0})
# => Person(name="jane", age=50)
```
will first (sucessfully) try to coerce `50.0` to an `int`. If coercion is not desired the `Any` type can be used with the caveat that values will not be type checked without additional [validation](https://marshmallow.readthedocs.io/en/stable/marshmallow.validate.html).
### Customizing generated fields
To pass arguments to the generated marshmallow fields (e.g., `validate`, `load_only`, `dump_only`, etc.),
pass them to the `metadata` argument of the
[`field`](https://docs.python.org/3/library/dataclasses.html#dataclasses.field) function.
Note that starting with version 4, marshmallow will disallow passing arbitrary arguments, so any
additional metadata should itself be put in its own `metadata` dict:
```python
from dataclasses import dataclass, field
import marshmallow_dataclass
import marshmallow.validate
@dataclass
class Person:
name: str = field(
metadata=dict(
load_only=True, metadata=dict(description="The person's first name")
)
)
height: float = field(metadata=dict(validate=marshmallow.validate.Range(min=0)))
PersonSchema = marshmallow_dataclass.class_schema(Person)
```
### `@dataclass` shortcut
`marshmallow_dataclass` provides a `@dataclass` decorator that behaves like the standard library's
`@dataclasses.dataclass` and adds a `Schema` attribute with the generated marshmallow
[Schema](https://marshmallow.readthedocs.io/en/2.x-line/api_reference.html#marshmallow.Schema).
```python
# Use marshmallow_dataclass's @dataclass shortcut
from marshmallow_dataclass import dataclass
@dataclass
class Point:
x: float
y: float
Point.Schema().dump(Point(4, 2))
# => {'x': 4, 'y': 2}
```
Note: Since the `.Schema` property is added dynamically, it can confuse type checkers.
To avoid that, you can declare `Schema` as a [`ClassVar`](https://docs.python.org/3/library/typing.html#typing.ClassVar).
```python
from typing import ClassVar, Type
from marshmallow_dataclass import dataclass
from marshmallow import Schema
@dataclass
class Point:
x: float
y: float
Schema: ClassVar[Type[Schema]] = Schema
```
### Customizing the base Schema
It is also possible to derive all schemas from your own
base Schema class
(see [marshmallow's documentation about extending `Schema`](https://marshmallow.readthedocs.io/en/stable/extending.html)).
This allows you to implement custom (de)serialization
behavior, for instance specifying a custom mapping between your classes and marshmallow fields,
or renaming fields on serialization.
#### Custom mapping between classes and fields
```python
class BaseSchema(marshmallow.Schema):
TYPE_MAPPING = {CustomType: CustomField, List: CustomListField}
class Sample:
my_custom: CustomType
my_custom_list: List[int]
SampleSchema = marshmallow_dataclass.class_schema(Sample, base_schema=BaseSchema)
# SampleSchema now serializes my_custom using the CustomField marshmallow field
# and serializes my_custom_list using the CustomListField marshmallow field
```
#### Renaming fields on serialization
```python
import marshmallow
import marshmallow_dataclass
class UppercaseSchema(marshmallow.Schema):
"""A Schema that marshals data with uppercased keys."""
def on_bind_field(self, field_name, field_obj):
field_obj.data_key = (field_obj.data_key or field_name).upper()
class Sample:
my_text: str
my_int: int
SampleSchema = marshmallow_dataclass.class_schema(Sample, base_schema=UppercaseSchema)
SampleSchema().dump(Sample(my_text="warm words", my_int=1))
# -> {"MY_TEXT": "warm words", "MY_INT": 1}
```
You can also pass `base_schema` to `marshmallow_dataclass.dataclass`.
```python
@marshmallow_dataclass.dataclass(base_schema=UppercaseSchema)
class Sample:
my_text: str
my_int: int
```
See [marshmallow's documentation about extending `Schema`](https://marshmallow.readthedocs.io/en/stable/extending.html).
### Custom type aliases
This library allows you to specify [customized marshmallow fields](https://marshmallow.readthedocs.io/en/stable/custom_fields.html#creating-a-field-class) using python's Annoted type [PEP-593](https://peps.python.org/pep-0593/).
```python
from typing import Annotated
import marshmallow.fields as mf
import marshmallow.validate as mv
IPv4 = Annotated[str, mf.String(validate=mv.Regexp(r"^([0-9]{1,3}\\.){3}[0-9]{1,3}$"))]
```
You can also pass a marshmallow field class.
```python
from typing import Annotated
import marshmallow
from marshmallow_dataclass import NewType
Email = Annotated[str, marshmallow.fields.Email]
```
For convenience, some custom types are provided:
```python
from marshmallow_dataclass.typing import Email, Url
```
When using Python 3.8, you must import `Annotated` from the typing_extensions package
```python
# Version agnostic import code:
if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated
```
### Custom NewType declarations [__deprecated__]
> NewType is deprecated in favor or type aliases using Annotated, as described above.
This library exports a `NewType` function to create types that generate [customized marshmallow fields](https://marshmallow.readthedocs.io/en/stable/custom_fields.html#creating-a-field-class).
Keyword arguments to `NewType` are passed to the marshmallow field constructor.
```python
import marshmallow.validate
from marshmallow_dataclass import NewType
IPv4 = NewType(
"IPv4", str, validate=marshmallow.validate.Regexp(r"^([0-9]{1,3}\\.){3}[0-9]{1,3}$")
)
```
You can also pass a marshmallow field to `NewType`.
```python
import marshmallow
from marshmallow_dataclass import NewType
Email = NewType("Email", str, field=marshmallow.fields.Email)
```
Note: if you are using `mypy`, you will notice that `mypy` throws an error if a variable defined with
`NewType` is used in a type annotation. To resolve this, add the `marshmallow_dataclass.mypy` plugin
to your `mypy` configuration, e.g.:
```ini
[mypy]
plugins = marshmallow_dataclass.mypy
# ...
```
### `Meta` options
[`Meta` options](https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.Schema.Meta) are set the same way as a marshmallow `Schema`.
```python
from marshmallow_dataclass import dataclass
@dataclass
class Point:
x: float
y: float
class Meta:
ordered = True
```
## Documentation
The project documentation is hosted on GitHub Pages: https://lovasoa.github.io/marshmallow_dataclass/
## Contributing
To install this project and make changes to it locally, follow the instructions in [`CONTRIBUTING.md`](./CONTRIBUTING.md).
|