Skip to main content

Apply changes as patches to pydanic models.

Project description

pydantic-apply

Installation

Just use pip install pydantic-apply to install the library.

About

With pydantic-apply you can apply changes to your pydantic models by using the ApplyModelMixin it provides:

import pydantic

from pydantic_apply import ApplyModelMixin


class Something(ApplyModelMixin, pydantic.BaseModel):
    name: str
    age: int


obj = Something(name='John Doe', age=42)
obj.apply({
    "age": 43,
})
assert obj.age == 43

As the apply data you may pass any dictionary or other pydanic object as you wish. pydantic objects will be converted to dict's when being applied - but will only use fields that where explicitly set on the model instance. Also note that .apply() will ignore all fields not present in the model, like the model constructor would.

Nested models

pydantic-apply will also know how to apply changes to nested models. If those models are by themself subclasses of ApplyModelMixin it will call apply() on those fields as well. Otherwise the whole attribute will be replaced.

Apply changes when using validate_assignment

When your models have validate_assignment enabled it may become tricky to apply changes to the model. This is due to the fact that you only can assign fields once at a time. But with validate_assignment enabled this means each field assignment will trigger its own validation and this validation might fail as the model state is not completely changes and thus in a "broken" intermediate state.

pydantic-apply will take care of this issue and disable the validation for each assignment while applying the changes. It will also ensure the resulting object will still pass the validation, so you don't have to care about this case at all.

Contributing

If you want to contribute to this project, feel free to just fork the project, create a dev branch in your fork and then create a pull request (PR). If you are unsure about whether your changes really suit the project please create an issue first, to talk about this.

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

pydantic_apply-0.1.5.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

pydantic_apply-0.1.5-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_apply-0.1.5.tar.gz.

File metadata

  • Download URL: pydantic_apply-0.1.5.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for pydantic_apply-0.1.5.tar.gz
Algorithm Hash digest
SHA256 dfe11eb33ee3f618b427f55dac116c0cb6d7fdffb7a0eeef9b79ba0f029a9c5e
MD5 ac67f42072673dd43fd659f38271740d
BLAKE2b-256 d836e9e67f9e6b5d9551425d162c9aa4e82f7b6a080ce818b351cfc7b19bd320

See more details on using hashes here.

Provenance

File details

Details for the file pydantic_apply-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_apply-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f381ff85e0f38730cc1316715e271fb404f4d2e074295f7a2030b20b66ee6ee8
MD5 8d1bcf37238c4b0bc5a63f64f1144baf
BLAKE2b-256 4b97d898b4ce9d3d651adac5a7b8029007b17939061e7e435cda00b1d208f2ee

See more details on using hashes here.

Provenance

Supported by

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