Skip to main content

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

Image title

Installation

pip install marshmallow-generic

Dependencies

Python Version 3.10+ and marshmallow (duh)


© 2023 Daniel Fainberg

Licence: Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marshmallow_generic-4.0.0.tar.gz (70.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

marshmallow_generic-4.0.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file marshmallow_generic-4.0.0.tar.gz.

File metadata

  • Download URL: marshmallow_generic-4.0.0.tar.gz
  • Upload date:
  • Size: 70.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for marshmallow_generic-4.0.0.tar.gz
Algorithm Hash digest
SHA256 8b8a4a55d36a64960cead37b50bcc39667f4046808292aee23e6930d7c41002b
MD5 e0f8d21a3d8ca3c0776cdd617a56101f
BLAKE2b-256 23fe0df6aab9ddbe748824d99f62586fb0e829cc83be10afe7a42cf473e4b54c

See more details on using hashes here.

File details

Details for the file marshmallow_generic-4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_generic-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea3d6cf8d30846df9266de5a16008daa58732a1079b2cb65831f35ef8de21603
MD5 a78836c455c5d80285b5d93e0d15a89b
BLAKE2b-256 65bf64539cf5d1182b08f41f8387c9e3738002ed229dc42f60d5dfc7b412e780

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page