Generic schema with full typing support and minimal boilerplate
Project description
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 "<User(name={self.name!r})>".format(self=self)
...
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 "builtins.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.9+
and marshmallow
(duh)
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
Built Distribution
File details
Details for the file marshmallow-generic-1.0.1.tar.gz
.
File metadata
- Download URL: marshmallow-generic-1.0.1.tar.gz
- Upload date:
- Size: 68.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03b29e9133da33fd55a4c10305deced9502aed5c7c8248770e6b9fd511703231 |
|
MD5 | 6e2410ab9930f0e67d243b078ad523bc |
|
BLAKE2b-256 | c7d3c79a6f8e3026ef3916804512ee6db077265ce5e6ca78f8f613561899e942 |
File details
Details for the file marshmallow_generic-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: marshmallow_generic-1.0.1-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11decfb6ebe03bb194dcd14283b6cee4a3a652a2d2f9b8884a15d4fe726eb8ac |
|
MD5 | 0d279dd440d95b767a352cf3114fd17d |
|
BLAKE2b-256 | b42d2f143df64cb8b258d48af16f0184c75a4d9836f6220b43fa61f47df29444 |