Bake marshmallow schemas based on dataclasses
Project description
marshmallow-recipe
The main goal of this opinionated library is to simplify migration from marshmallow2 to marshmallow3. Also, it helps with:
- Stop writing marshmallow schemas completely: it generates them from dataclass.
- Using different naming cases(camel and capital camel cases are supported).
- Utilizing best practises on fields configuration.
Supported types:
str
,int
,float
,bool
,datetime.datetime
,datetime.date
,datetime.time
,decimal.Decimal
,uuid.UUID
Optional[T]
,T | None
Annotated
list
,dict
(with typed keys and values),tuple
(only when all elements of the same type),set
,frozenset
Mapping
(with typed keys and values),Set
,Sequence
Example: class Annotated: pass
import dataclasses
import datetime
import decimal
import marshmallow_recipe as mr
import uuid
from typing import Annotated
@dataclasses.dataclass(frozen=True)
class Transaction:
id: uuid.UUID
created_at: datetime.datetime
processed_at: datetime.datetime | None
amount: decimal.Decimal = dataclasses.field(metadata=mr.decimal_metadata(places=4))
transaction_amount: Annotated[decimal.Decimal, mr.decimal_metadata(places=4)]
transaction = Transaction(
id=uuid.uuid4(),
created_at=datetime.datetime.utcnow(),
processed_at=None,
amount=decimal.Decimal(42),
transaction_amount=decimal.Decimal(42),
)
# dumps the transaction to a dict
raw = mr.dump(transaction)
# loads a transaction from the dict
mr.load(Transaction, raw)
# provides a generated marshmallow schema for dataclass
mr.schema(Transaction)
Update API example:
import decimal
import dataclasses
import marshmallow_recipe as mr
@dataclasses.dataclass(frozen=True)
@mr.options(none_value_handling=mr.NoneValueHandling.INCLUDE)
class CompanyUpdateData:
name: str = mr.MISSING
annual_turnover: decimal.Decimal | None = mr.MISSING
company_update_data = CompanyUpdateData(name="updated name")
dumped = mr.dump(company_update_data)
assert dumped == {"name": "updated name"} # Note: no "annual_turnover" here
loaded = mr.load(CompanyUpdateData, {"name": "updated name"})
assert loaded.name == "updated name"
assert loaded.annual_turnover is mr.MISSING
loaded = mr.load(CompanyUpdateData, {"annual_turnover": None})
assert loaded.name is mr.MISSING
assert loaded.annual_turnover is None
v0.0.42(2024-11-09)
v0.0.41(2024-11-01)
v0.0.40(2024-10-28)
v0.0.39(2024-07-25)
v0.0.38(2024-01-05)
v0.0.38a2(2023-12-15)
v0.0.38a1(2023-12-11)
v0.0.37(2023-12-11)
v0.0.36(2023-12-08)
v0.0.34(2023-12-07)
- Support datetime.time: #137
v0.0.33(2023-09-30)
v0.0.32(2023-09-28)
v0.0.31(2023-09-26)
- Allow to strip whitespaces on load/dump for str fields
- Allow to provide post_load delegate for str fields
v0.0.30(2023-09-25)
- Improve validation support: expose ValidationError to raise it from validators, allow to pass a sequence of validators
- Add meta shortcuts to metadata methods
v0.0.29(2023-09-23)
v0.0.28(2023-09-22)
v0.0.27(2023-09-22)
v0.0.26(2023-09-18)
v0.0.25(2023-09-11)
- Support set, set[T], frozenset, frozenset[T], tuple, tuple[T, ...], collections.abc.Set[T], collections.abc.Sequence[T], collections.abc.Mapping[K, V]
v0.0.24(2023-09-08)
- Set module for auto-generated schemas
- Calling bake_schema should reuse already generated schemas
- NamingCase should not be propagated through serialisation hierarchy
- Removal of DEFAULT_CASE
v0.0.23(2023-09-05)
v0.0.22(2023-06-27)
v0.0.22a2(2023-06-27)
v0.0.22a1(2023-06-24)
v0.0.21(2023-06-24)
v0.0.21a1(2023-06-08)
v0.0.20(2023-04-25)
v0.0.19(2023-04-24)
v0.0.18(2023-01-05)
v0.0.17(2022-12-20)
v0.0.16(2022-12-02)
v0.0.15(2022-12-01)
v0.0.14(2022-11-14)
v0.0.13(2022-10-14)
v0.0.12(2022-08-23)
v0.0.11(2022-06-23)
v0.0.10(2022-06-15)
v0.0.9(2022-04-26)
v0.0.8(2022-03-26)
v0.0.7(2022-03-20)
v0.0.6(2022-03-20)
v0.0.5(2022-03-01)
- Validate before dump for marshmallow3
- Move many to schema
- Fix default naming case
- Fix unhashable type: 'set' error
v0.0.4(2022-02-21)
- Support decimal field
- Support metadata
- Support int field
- Support float field
- Support uuid field
- Support list and dict
- Support marshmallow3
- Support datetime and date
- Unify datetime.tzinfo behaviour
v0.0.3 (2022-02-14)
v0.0.2 (2022-02-13)
v0.0.1 (2022-02-13)
- A first version
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
marshmallow-recipe-0.0.42.tar.gz
(17.8 kB
view details)
Built Distribution
File details
Details for the file marshmallow-recipe-0.0.42.tar.gz
.
File metadata
- Download URL: marshmallow-recipe-0.0.42.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0b94b52c3bc0c1ded0c44996006aedf33166074082c21d8da1b5d6894679628 |
|
MD5 | 6391c9ecf4ac13fe4c24a25d4bca800e |
|
BLAKE2b-256 | 388a9a37eba966897853bece532a3f81d8d842d756fbab15bb473f9964712f5b |
File details
Details for the file marshmallow_recipe-0.0.42-py3-none-any.whl
.
File metadata
- Download URL: marshmallow_recipe-0.0.42-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d21e6c69bac1442e028c5220c60eb92236985d5ef90344acb21127a37c8c757 |
|
MD5 | 741cf423cd7d000da873b953d0ade144 |
|
BLAKE2b-256 | 40d9639b33350f949a145154658f7194ee25f7d2006743fd9898ff5f5b8dacfc |