All Projects → lovasoa → Marshmallow_dataclass

lovasoa / Marshmallow_dataclass

Licence: mit
Automatic generation of marshmallow schemas from dataclasses.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Marshmallow dataclass

Qxorm
QxOrm library - C++ Qt ORM (Object Relational Mapping) and ODM (Object Document Mapper) library - Official repository
Stars: ✭ 176 (-30.98%)
Mutual labels:  orm, serialization
Jsona
Data formatter that creates simplified objects from JSON or stored reduxObject, creates JSON from the same simplified objects (in according with JSON API specification)
Stars: ✭ 144 (-43.53%)
Mutual labels:  orm, serialization
Datafiles
A file-based ORM for Python dataclasses.
Stars: ✭ 113 (-55.69%)
Mutual labels:  orm, serialization
marshmallow-validators
Use 3rd-party validators (e.g. from WTForms and colander) with marshmallow
Stars: ✭ 24 (-90.59%)
Mutual labels:  serialization, marshmallow
Django Rest Marshmallow
Marshmallow schemas for Django REST framework
Stars: ✭ 198 (-22.35%)
Mutual labels:  marshmallow, serialization
Python Api Development Fundamentals
Develop a full-stack web application with Python and Flask
Stars: ✭ 44 (-82.75%)
Mutual labels:  marshmallow, serialization
Awesome Python Models
A curated list of awesome Python libraries, which implement models, schemas, serializers/deserializers, ODM's/ORM's, Active Records or similar patterns.
Stars: ✭ 124 (-51.37%)
Mutual labels:  orm, serialization
Dynamorm
Python object & relation mapping library for Amazon's DynamoDB service
Stars: ✭ 72 (-71.76%)
Mutual labels:  marshmallow, orm
Marshmallow Jsonapi
JSON API 1.0 (https://jsonapi.org/) formatting with marshmallow
Stars: ✭ 203 (-20.39%)
Mutual labels:  marshmallow, serialization
desert
Deserialize to objects while staying DRY
Stars: ✭ 136 (-46.67%)
Mutual labels:  serialization, marshmallow
yorm
Automatic object-YAML mapping for Python.
Stars: ✭ 23 (-90.98%)
Mutual labels:  orm
Simple-YAML
A Java API that provides an easy-to-use way to store data using the YAML format.
Stars: ✭ 68 (-73.33%)
Mutual labels:  serialization
AbacusUtil
Release the power in Java programming
Stars: ✭ 77 (-69.8%)
Mutual labels:  orm
full-stack-flask-couchdb
Full stack, modern web application generator. Using Flask, CouchDB as database, Docker, Swagger, automatic HTTPS and more.
Stars: ✭ 28 (-89.02%)
Mutual labels:  marshmallow
quick-crud
An easy CRUD operation based on Factory pattern with Mongoose.
Stars: ✭ 16 (-93.73%)
Mutual labels:  orm
sqb
Extensible, multi-dialect SQL query builder and Database connection framework for NodeJS
Stars: ✭ 14 (-94.51%)
Mutual labels:  serialization
corma
Convention-based Object-Relational Mapper
Stars: ✭ 28 (-89.02%)
Mutual labels:  orm
graphql-tutorial
Tutorial for GraphQL
Stars: ✭ 24 (-90.59%)
Mutual labels:  orm
simplify
simplify 包含了一系列自我驱动学习的子项目。
Stars: ✭ 67 (-73.73%)
Mutual labels:  serialization
model-orm-php
PHP ORM Model class which provides table column/property mapping, CRUD, and dynamic finders/counters on a database table using PDO
Stars: ✭ 18 (-92.94%)
Mutual labels:  orm

marshmallow-dataclass

Build Status PyPI version marshmallow 3 compatible

Automatic generation of marshmallow schemas from dataclasses.

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.

pip3 install marshmallow-dataclass

You may optionally install the following extras:

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 function to generate a marshmallow Schema class from a dataclass.

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 (such as float, str, bytes, datetime, ...), or other dataclasses.

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 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:

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.

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

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

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

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.

@marshmallow_dataclass.dataclass(base_schema=UppercaseSchema)
class Sample:
    my_text: str
    my_int: int

See marshmallow's documentation about extending Schema.

Custom NewType declarations

This library exports a NewType function to create types that generate customized marshmallow fields.

Keyword arguments to NewType are passed to the marshmallow field constructor.

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.

import marshmallow
from marshmallow_dataclass import NewType

Email = NewType("Email", str, field=marshmallow.fields.Email)

For convenience, some custom types are provided:

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

[mypy]
plugins = marshmallow_dataclass.mypy
# ...

Meta options

Meta options are set the same way as a marshmallow Schema.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].