Skip to main content

Pydantic Dict Encoders

Simple wrapper of pydantic for support custom dict encoders like json encoders

CI Coverage pypi downloads versions license

This wrapper created for solve serialization problem in ariadne resolvers, where needed return dict only with simple objects. After research some problems and pydantic features, it was decided to make this wrapper more like a crutch for solve my problem.

This should be resolved in Pydantic V2, but it's not production ready yet. And even there is a PR that was made some time ago. But for now, it seemed to me sufficient to use this solution and perhaps it will be useful to someone else.

Thanks to @samuelcolvin for create and being active on pydantic and starting to solve problems before some people think about them! :)

IMPORTANT! Remember: this is a crutch that you use at your own risk. It will most likely not be needed after some time, but if everything suits you - go for it ;)


Usage

This wrapper has 2 possibilities:

  1. Define custom dict encoders that work the same as pydantic json_encoders when calling model.dict()
  2. Encode each field value as if they were passed to pydantic json.

To use this just inherit your model from PydanticDictEncodersMixin:

class AnyModel(PydanticDictEncodersMixin, BaseModel):
    any_field: str | None = None

    class Config:
        dict_encoders = {}
        jsonify_dict_encode = False

WARNING! Please, remember about python MRO: BaseModel MUST BE after mixin.


Pros and cons

Pros:

  • Pretty simple and fast way to get the required behavior when exporting to a python dict

Cons:

  • In nested models that do not inherit from the mixin, serialization will break
  • Dirty decision

Metadata

Release files for pydantic-dict-encoders 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-dict-encoders 0.2
File Size Uploaded
pydantic_dict_encoders-0.2.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-dict-encoders 0.2
File Interpreter ABI Platform
pydantic_dict_encoders-0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / pydantic_dict_encoders-0.2.tar.gz

Download URL pydantic_dict_encoders-0.2.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
704f908ebeaca779189df9ae26ddf4ca6f144aa593b953a2ba0eba38afdfcb24
BLAKE2b-256 checksum
How to use checksums
d866e7fb3372eded89fbed12c46d8e01adbab586d85b626f7d80936434362bcd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.9.16 Darwin/22.2.0

Release files / pydantic_dict_encoders-0.2-py3-none-any.whl

Download URL pydantic_dict_encoders-0.2-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
684e6fe73ed4b75a50c68a43fe74519d3c1243d7ef1b734db4ae6ebd2e5a4c9f
BLAKE2b-256 checksum
How to use checksums
0dc8b15234e4886fce30e581bbcf2c91250c125b7652ec627a82bdfb7c9262ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.9.16 Darwin/22.2.0

Release history Release notifications | RSS feed

This release

0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page