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.
import dataclasses
import datetime
import decimal
import marshmallow_recipe as mr
import uuid
@dataclasses.dataclass(frozen=True)
class Transaction:
id: uuid.UUID
created_at: datetime.datetime
processed_at: datetime.datetime | None
amount: decimal.Decimal
transaction = Transaction(
id=uuid.uuid4(),
created_at=datetime.datetime.utcnow(),
processed_at=None,
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.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.11.tar.gz
(10.8 kB
view details)
Built Distribution
File details
Details for the file marshmallow-recipe-0.0.11.tar.gz
.
File metadata
- Download URL: marshmallow-recipe-0.0.11.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c39ef7957d9e043dad98e0d279f634e526a7cff96c9fe411127ff8fc3eaaf53b |
|
MD5 | f3f6cae2d658c69b8e6773af65f4df31 |
|
BLAKE2b-256 | eff1678b0f2b0e1057c52c94c36ce125318421bf588fe31996377727b9337042 |
File details
Details for the file marshmallow_recipe-0.0.11-py3-none-any.whl
.
File metadata
- Download URL: marshmallow_recipe-0.0.11-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc3c823efe33932b4e64d363220d7ccb2b58cf3ecb20524413b52c225776b6cc |
|
MD5 | 26adcbece1c8fcadb4b9c2f0320e1d4c |
|
BLAKE2b-256 | 5715f8c1c56955c062ea20619412c67d822a1a0e75c049cc5ab20efc0cc64857 |