marshmallow-dataclass


Namemarshmallow-dataclass JSON
Version 8.6.1 PyPI version JSON
download
home_pagehttps://github.com/lovasoa/marshmallow_dataclass
SummaryPython library to convert dataclasses into marshmallow schemas.
upload_time2024-03-20 16:32:16
maintainerNone
docs_urlNone
authorOphir LOJKINE
requires_python>=3.6
licenseMIT
keywords marshmallow dataclass serialization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # marshmallow-dataclass

[![Test Workflow Status (master branch)](https://img.shields.io/github/actions/workflow/status/lovasoa/marshmallow_dataclass/python-package.yml?branch=master&label=tests)](https://github.com/lovasoa/marshmallow_dataclass/actions/workflows/python-package.yml)
[![PyPI version](https://badge.fury.io/py/marshmallow-dataclass.svg)](https://badge.fury.io/py/marshmallow-dataclass)
[![marshmallow 3 compatible](https://badgen.net/badge/marshmallow/3)](https://marshmallow.readthedocs.io/en/latest/upgrading.html)
[![download stats](https://img.shields.io/pypi/dm/marshmallow-dataclass.svg)](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
```

You may optionally install the following extras:

- `enum` : enum support for marshmallow versions <3.18 [marshmallow-enum](https://github.com/justanr/marshmallow_enum).
- `union` : for translating python [`Union` types](https://docs.python.org/3/library/typing.html#typing.Union) to union fields.

```shell
pip3 install "marshmallow-dataclass[enum,union]"
```

### 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 NewType declarations

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)
```

For convenience, some custom types are provided:

```python
from marshmallow_dataclass.typing import Email, Url
```

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).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/lovasoa/marshmallow_dataclass",
    "name": "marshmallow-dataclass",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "marshmallow, dataclass, serialization",
    "author": "Ophir LOJKINE",
    "author_email": "pere.jobs@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4e/2c/0a835792269c01cca3ad424b69a6f946c908334d6f155723d5faa5b22288/marshmallow_dataclass-8.6.1.tar.gz",
    "platform": null,
    "description": "# marshmallow-dataclass\n\n[![Test Workflow Status (master branch)](https://img.shields.io/github/actions/workflow/status/lovasoa/marshmallow_dataclass/python-package.yml?branch=master&label=tests)](https://github.com/lovasoa/marshmallow_dataclass/actions/workflows/python-package.yml)\n[![PyPI version](https://badge.fury.io/py/marshmallow-dataclass.svg)](https://badge.fury.io/py/marshmallow-dataclass)\n[![marshmallow 3 compatible](https://badgen.net/badge/marshmallow/3)](https://marshmallow.readthedocs.io/en/latest/upgrading.html)\n[![download stats](https://img.shields.io/pypi/dm/marshmallow-dataclass.svg)](https://pypistats.org/packages/marshmallow-dataclass)\n\nAutomatic generation of [marshmallow](https://marshmallow.readthedocs.io/) schemas from dataclasses.\n\n```python\nfrom dataclasses import dataclass, field\nfrom typing import List, Optional\n\nimport marshmallow_dataclass\nimport marshmallow.validate\n\n\n@dataclass\nclass Building:\n    # field metadata is used to instantiate the marshmallow field\n    height: float = field(metadata={\"validate\": marshmallow.validate.Range(min=0)})\n    name: str = field(default=\"anonymous\")\n\n\n@dataclass\nclass City:\n    name: Optional[str]\n    buildings: List[Building] = field(default_factory=list)\n\n\ncity_schema = marshmallow_dataclass.class_schema(City)()\n\ncity = city_schema.load(\n    {\"name\": \"Paris\", \"buildings\": [{\"name\": \"Eiffel Tower\", \"height\": 324}]}\n)\n# => City(name='Paris', buildings=[Building(height=324.0, name='Eiffel Tower')])\n\ncity_dict = city_schema.dump(city)\n# => {'name': 'Paris', 'buildings': [{'name': 'Eiffel Tower', 'height': 324.0}]}\n```\n\n## Why\n\nUsing 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.\nAs of Python 3.6, types can be defined for class members, which allows libraries to generate schemas automatically.\n\nTherefore, you can document your APIs in a way that allows you to statically check that the code matches the documentation.\n\n## Installation\n\nThis package [is hosted on PyPI](https://pypi.org/project/marshmallow-dataclass/).\n\n```shell\npip3 install marshmallow-dataclass\n```\n\nYou may optionally install the following extras:\n\n- `enum` : enum support for marshmallow versions <3.18 [marshmallow-enum](https://github.com/justanr/marshmallow_enum).\n- `union` : for translating python [`Union` types](https://docs.python.org/3/library/typing.html#typing.Union) to union fields.\n\n```shell\npip3 install \"marshmallow-dataclass[enum,union]\"\n```\n\n### marshmallow 2 support\n\n`marshmallow-dataclass` no longer supports marshmallow 2.\nInstall `marshmallow_dataclass<6.0` if you need marshmallow 2 compatibility.\n\n## Usage\n\nUse the [`class_schema`](https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html#marshmallow_dataclass.class_schema)\nfunction to generate a marshmallow [Schema](https://marshmallow.readthedocs.io/en/latest/api_reference.html#marshmallow.Schema)\nclass from a [`dataclass`](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass).\n\n```python\nfrom dataclasses import dataclass\nfrom datetime import date\n\nimport marshmallow_dataclass\n\n\n@dataclass\nclass Person:\n    name: str\n    birth: date\n\n\nPersonSchema = marshmallow_dataclass.class_schema(Person)\n```\n\nThe type of your fields must be either basic \n[types supported by marshmallow](https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.Schema.TYPE_MAPPING)\n(such as `float`, `str`, `bytes`, `datetime`, ...), `Union`, or other dataclasses.\n\n### Union (de)serialization coercion \n\nTypically the Union type; `Union[X, Y]` means\u2014from a set theory perspective\u2014either `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). \n\nFor example, \n\n```python\nfrom typing import Union\n\nfrom dataclasses import dataclass\n\n\n@dataclass\nclass Person:\n    name: str\n    age: Union[int, float]\n\n\nPersonSchema = marshmallow_dataclass.class_schema(Person)\nPersonSchema().load({\"name\": \"jane\", \"age\": 50.0})\n# => Person(name=\"jane\", age=50)\n```\n\nwill 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).\n\n### Customizing generated fields\n\nTo pass arguments to the generated marshmallow fields (e.g., `validate`, `load_only`, `dump_only`, etc.),\npass them to the `metadata` argument of the\n[`field`](https://docs.python.org/3/library/dataclasses.html#dataclasses.field) function.\n\nNote that starting with version 4, marshmallow will disallow passing arbitrary arguments, so any\nadditional metadata should itself be put in its own `metadata` dict:\n\n```python\nfrom dataclasses import dataclass, field\nimport marshmallow_dataclass\nimport marshmallow.validate\n\n\n@dataclass\nclass Person:\n    name: str = field(\n        metadata=dict(\n            load_only=True, metadata=dict(description=\"The person's first name\")\n        )\n    )\n    height: float = field(metadata=dict(validate=marshmallow.validate.Range(min=0)))\n\n\nPersonSchema = marshmallow_dataclass.class_schema(Person)\n```\n\n### `@dataclass` shortcut\n\n`marshmallow_dataclass` provides a `@dataclass` decorator that behaves like the standard library's \n`@dataclasses.dataclass` and adds a `Schema` attribute with the generated marshmallow\n[Schema](https://marshmallow.readthedocs.io/en/2.x-line/api_reference.html#marshmallow.Schema).\n\n```python\n# Use marshmallow_dataclass's @dataclass shortcut\nfrom marshmallow_dataclass import dataclass\n\n\n@dataclass\nclass Point:\n    x: float\n    y: float\n\n\nPoint.Schema().dump(Point(4, 2))\n# => {'x': 4, 'y': 2}\n```\n\nNote: Since the `.Schema` property is added dynamically, it can confuse type checkers.\nTo avoid that, you can declare `Schema` as a [`ClassVar`](https://docs.python.org/3/library/typing.html#typing.ClassVar).\n\n```python\nfrom typing import ClassVar, Type\n\nfrom marshmallow_dataclass import dataclass\nfrom marshmallow import Schema\n\n\n@dataclass\nclass Point:\n    x: float\n    y: float\n    Schema: ClassVar[Type[Schema]] = Schema\n```\n\n### Customizing the base Schema\n\nIt is also possible to derive all schemas from your own \nbase Schema class\n(see [marshmallow's documentation about extending `Schema`](https://marshmallow.readthedocs.io/en/stable/extending.html)).\nThis allows you to implement custom (de)serialization\nbehavior, for instance specifying a custom mapping between your classes and marshmallow fields,\nor renaming fields on serialization.\n\n#### Custom mapping between classes and fields\n\n```python\nclass BaseSchema(marshmallow.Schema):\n    TYPE_MAPPING = {CustomType: CustomField, List: CustomListField}\n\n\nclass Sample:\n    my_custom: CustomType\n    my_custom_list: List[int]\n\n\nSampleSchema = marshmallow_dataclass.class_schema(Sample, base_schema=BaseSchema)\n# SampleSchema now serializes my_custom using the CustomField marshmallow field\n# and serializes my_custom_list using the CustomListField marshmallow field\n```\n\n#### Renaming fields on serialization\n\n```python\nimport marshmallow\nimport marshmallow_dataclass\n\n\nclass UppercaseSchema(marshmallow.Schema):\n    \"\"\"A Schema that marshals data with uppercased keys.\"\"\"\n\n    def on_bind_field(self, field_name, field_obj):\n        field_obj.data_key = (field_obj.data_key or field_name).upper()\n\n\nclass Sample:\n    my_text: str\n    my_int: int\n\n\nSampleSchema = marshmallow_dataclass.class_schema(Sample, base_schema=UppercaseSchema)\n\nSampleSchema().dump(Sample(my_text=\"warm words\", my_int=1))\n# -> {\"MY_TEXT\": \"warm words\", \"MY_INT\": 1}\n```\n\nYou can also pass `base_schema` to `marshmallow_dataclass.dataclass`.\n\n```python\n@marshmallow_dataclass.dataclass(base_schema=UppercaseSchema)\nclass Sample:\n    my_text: str\n    my_int: int\n```\n\nSee [marshmallow's documentation about extending `Schema`](https://marshmallow.readthedocs.io/en/stable/extending.html).\n\n### Custom NewType declarations\n\nThis 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).\n\nKeyword arguments to `NewType` are passed to the marshmallow field constructor.\n\n```python\nimport marshmallow.validate\nfrom marshmallow_dataclass import NewType\n\nIPv4 = NewType(\n    \"IPv4\", str, validate=marshmallow.validate.Regexp(r\"^([0-9]{1,3}\\\\.){3}[0-9]{1,3}$\")\n)\n```\n\nYou can also pass a marshmallow field to `NewType`.\n\n```python\nimport marshmallow\nfrom marshmallow_dataclass import NewType\n\nEmail = NewType(\"Email\", str, field=marshmallow.fields.Email)\n```\n\nFor convenience, some custom types are provided:\n\n```python\nfrom marshmallow_dataclass.typing import Email, Url\n```\n\nNote: if you are using `mypy`, you will notice that `mypy` throws an error if a variable defined with\n`NewType` is used in a type annotation. To resolve this, add the `marshmallow_dataclass.mypy` plugin\nto your `mypy` configuration, e.g.:\n\n```ini\n[mypy]\nplugins = marshmallow_dataclass.mypy\n# ...\n```\n\n### `Meta` options\n\n[`Meta` options](https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.Schema.Meta) are set the same way as a marshmallow `Schema`.\n\n```python\nfrom marshmallow_dataclass import dataclass\n\n\n@dataclass\nclass Point:\n    x: float\n    y: float\n\n    class Meta:\n        ordered = True\n```\n\n## Documentation\n\nThe project documentation is hosted on GitHub Pages: https://lovasoa.github.io/marshmallow_dataclass/\n\n## Contributing\n\nTo install this project and make changes to it locally, follow the instructions in [`CONTRIBUTING.md`](./CONTRIBUTING.md).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python library to convert dataclasses into marshmallow schemas.",
    "version": "8.6.1",
    "project_urls": {
        "Homepage": "https://github.com/lovasoa/marshmallow_dataclass"
    },
    "split_keywords": [
        "marshmallow",
        " dataclass",
        " serialization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5b961cc5b8d8d0b22f38ed4a95f34ba5c66b95949033b03d630657dcad31f492",
                "md5": "7913f2060a075b2972766b9f55ac0050",
                "sha256": "d941137bcb480729a14810e5f10d9bb79a498fb5b57c875aad6be37e97b98a5f"
            },
            "downloads": -1,
            "filename": "marshmallow_dataclass-8.6.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7913f2060a075b2972766b9f55ac0050",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 18685,
            "upload_time": "2024-03-20T16:32:13",
            "upload_time_iso_8601": "2024-03-20T16:32:13.613269Z",
            "url": "https://files.pythonhosted.org/packages/5b/96/1cc5b8d8d0b22f38ed4a95f34ba5c66b95949033b03d630657dcad31f492/marshmallow_dataclass-8.6.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4e2c0a835792269c01cca3ad424b69a6f946c908334d6f155723d5faa5b22288",
                "md5": "54e6bff80de2e3f263c8ecbea1602d8f",
                "sha256": "dca312c841f73f8f665b4434d23b3204e8cfbf50b8cbb57bb76f41a6ee8184c8"
            },
            "downloads": -1,
            "filename": "marshmallow_dataclass-8.6.1.tar.gz",
            "has_sig": false,
            "md5_digest": "54e6bff80de2e3f263c8ecbea1602d8f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 31519,
            "upload_time": "2024-03-20T16:32:16",
            "upload_time_iso_8601": "2024-03-20T16:32:16.143540Z",
            "url": "https://files.pythonhosted.org/packages/4e/2c/0a835792269c01cca3ad424b69a6f946c908334d6f155723d5faa5b22288/marshmallow_dataclass-8.6.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-20 16:32:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lovasoa",
    "github_project": "marshmallow_dataclass",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "marshmallow-dataclass"
}
        
Elapsed time: 0.26460s