Skip to main content

pydantic-apply

Installation

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

Note: pydantic-apply is compatible with pydantic version 2.x on Python 3.10, 3.11, 3.12, 3.13 and 3.14. This is also ensured running all tests on all those versions using tox.

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.model_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.

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.9.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

pydantic_apply-0.9.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_apply-0.9.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pydantic_apply-0.9.1.tar.gz
Algorithm Hash digest
SHA256 c613c0dd2f6152fe6c93a79d8096583570aed01268eed69eb312975aafc6d849
MD5 f7178be37e46e17ce96385ae336d21cd
BLAKE2b-256 6579d5ff2cdbdf66804095e7bb4ba1551f1748afedf6adfd59d61f14316a77b1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_apply-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pydantic_apply-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c99a5ef6f43f2c05ed37d16b1e411526ccae75729e95b0f3e2e63ad6560c16ca
MD5 1c26902531e755dcefb8d1c9853b21d7
BLAKE2b-256 b5bde7288b6f9cf9e1eda2e776c7a9c67f168baf96b39ccb88eddfa773122b10

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.9.1 This release

2 files

0.8.1

2 files

0.8.0

2 files

0.7.1

2 files

0.7.0

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.8

2 files

0.5.7

2 files

0.5.5

2 files

0.5.4

2 files

0.5.2

2 files

0.5.0

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 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