marshmallow-generic
Generic schema with full typing support and minimal boilerplate
Documentation: daniil-berg.github.io/marshmallow-generic
Source Code: github.com/daniil-berg/marshmallow-generic
Extension for marshmallow to make deserialization to objects easier and improve type safety.
The main GenericSchema class extends marshmallow.Schema making it generic in terms of the class that data should be deserialized to, when calling load/loads.
With GenericSchema there is no need to explicitly write post_load hooks to initialize the object anymore. 🎉
If the "model" class is (for example) User, it just needs to be passed as the type argument, when subclassing GenericSchema. The output of the load/loads method will then be automatically inferred as either User or list[User] (depending on whether many is True or not) by any competent type checker. ✨
Usage Example
from marshmallow_generic import GenericSchema, fields
class User:
def __init__(self, name: str, email: str) -> None:
self.name = name
self.email = email
def __repr__(self) -> str:
return f"<User(name={self.name})>"
...
class UserSchema(GenericSchema[User]):
name = fields.Str()
email = fields.Email()
user_data = {"name": "Monty", "email": "monty@python.org"}
schema = UserSchema()
single_user = schema.load(user_data)
print(single_user) # <User(name='Monty')>
json_data = '''[
{"name": "Monty", "email": "monty@python.org"},
{"name": "Ronnie", "email": "ronnie@stones.com"}
]'''
multiple_users = schema.loads(json_data, many=True)
print(multiple_users) # [<User(name='Monty')>, <User(name='Ronnie')>]
Adding reveal_type(single_user) and reveal_type(multiple_users) at the bottom and running that code through mypy would yield the following output:
note: Revealed type is "User"
note: Revealed type is "list[User]"
With the regular marshmallow.Schema, the output of mypy would instead be this:
note: Revealed type is "Any"
note: Revealed type is "Any"
This also means your IDE will be able to infer the types and thus provide useful auto-suggestions for the loaded objects. 👨💻
Here is PyCharm with the example from above:
Installation
pip install marshmallow-generic
Dependencies
Python Version 3.10+ and marshmallow (duh)
© 2023 Daniel Fainberg
Licence: Apache-2.0
Metadata
Release files for marshmallow-generic 4.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| marshmallow_generic-4.0.0.tar.gz | 70.3 kB | Without provenance | Details for marshmallow_generic-4.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| marshmallow_generic-4.0.0-py3-none-any.whl | Python 3 | none | any | Details for marshmallow_generic-4.0.0-py3-none-any.whl |
Total release size: 83.5 kB
Release files / marshmallow_generic-4.0.0.tar.gz
| Download URL | marshmallow_generic-4.0.0.tar.gz |
|---|---|
| Size | 70.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8b8a4a55d36a64960cead37b50bcc39667f4046808292aee23e6930d7c41002b
|
|
BLAKE2b-256 checksum How to use checksums |
23fe0df6aab9ddbe748824d99f62586fb0e829cc83be10afe7a42cf473e4b54c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.19
|
Release files / marshmallow_generic-4.0.0-py3-none-any.whl
| Download URL | marshmallow_generic-4.0.0-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea3d6cf8d30846df9266de5a16008daa58732a1079b2cb65831f35ef8de21603
|
|
BLAKE2b-256 checksum How to use checksums |
65bf64539cf5d1182b08f41f8387c9e3738002ed229dc42f60d5dfc7b412e780
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.19
|
Security for marshmallow-generic 4.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.