Skip to main content

Makes partial Pydantic models without making fields nullable.

Project description

pydantic-strict-partial

PyPI version PyPI Supported Python Versions CI badge

About

Create partial models based on the original Pydantic models.

This makes all the fields optional. This doesn't make them nullable and doesn't disable validation. The only thing it does is provide default values for those fields (None by default), so you can use model.model_dump(exclude_unset=True) command to receive specified values only.

The most common use case is a PATCH request on FastAPI endpoints where you want to allow partial updates.

Installation

pydantic-strict-partial compatible with Python 3.10+ and Pydantic 2.1+.

Using pip

pip install pydantic-strict-partial

Using uv

uv add pydantic-strict-partial

Usage

from typing import Annotated

from annotated_types import Ge
from pydantic import BaseModel

from pydantic_strict_partial import create_partial_model


class UserSchema(BaseModel):
    name: str
    nickname: str | None
    age: Annotated[int, Ge(18)]


UserPartialUpdateSchema = create_partial_model(UserSchema)

assert UserPartialUpdateSchema(age=20).model_dump(exclude_unset=True) == {
    'age': 20
}

UserPartialUpdateSchema(name=None)  # raises ValidationError
UserPartialUpdateSchema(age=17)  # raises ValidationError

There is also possible to specify a limited list of fields to be partial:

UserPartialUpdateSchema = create_partial_model(UserSchema, 'name', 'nickname')

Or to make all fields partial except for the specified ones:

UserPartialCreateSchema = create_partial_model(UserSchema, required_fields=['age'])

Development

uv sync --group dev
uv tool install tox --with tox-uv
tox run

Use tox run -e lint or tox run -e py314-pydantic2.x if you only want a single environment.

Known limitations

MyPy: "is not valid as a type" error

You may be faced with Variable "UserPartialUpdateSchema" is not valid as a type error. There is no good solution for that. But the next approach can be used as a workaround:

class UserPartialUpdateSchema(create_partial_model(UserSchema)):  # type: ignore[misc]
    pass

Alternatives

pydantic-partial - it makes all fields nullable and disables all validators, which is not suitable for payload validation on PATCH endpoints.

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_strict_partial-1.0.0.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_strict_partial-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_strict_partial-1.0.0.tar.gz.

File metadata

  • Download URL: pydantic_strict_partial-1.0.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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_strict_partial-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e853132ced6db005866928c1e0a1e3571942683524c746b115a0e31a9afe6ce0
MD5 ed197cfdac74cc3af55a8502e7a98247
BLAKE2b-256 04ae65fecd011debffffe0afee740ca91573e238c182f0944efd0536e4df6cb7

See more details on using hashes here.

File details

Details for the file pydantic_strict_partial-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_strict_partial-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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_strict_partial-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 612a4f5e1b7c562414f58ec85b2e1fd4d578ac0f4cd82c6cfc2161696b685c33
MD5 ddff05403405403512b07b34c11d3816
BLAKE2b-256 9dba5cac7cd96bdbc0a25a5de3a0170207b77e644f6432e7babb22b29e2df600

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