pydantic-strict-partial
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.
Metadata
Release files for pydantic-strict-partial 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydantic_strict_partial-1.0.0.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_strict_partial-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.6 kB
Release files / pydantic_strict_partial-1.0.0.tar.gz
| Download URL | pydantic_strict_partial-1.0.0.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e853132ced6db005866928c1e0a1e3571942683524c746b115a0e31a9afe6ce0
|
|
BLAKE2b-256 checksum How to use checksums |
04ae65fecd011debffffe0afee740ca91573e238c182f0944efd0536e4df6cb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / pydantic_strict_partial-1.0.0-py3-none-any.whl
| Download URL | pydantic_strict_partial-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
612a4f5e1b7c562414f58ec85b2e1fd4d578ac0f4cd82c6cfc2161696b685c33
|
|
BLAKE2b-256 checksum How to use checksums |
9dba5cac7cd96bdbc0a25a5de3a0170207b77e644f6432e7babb22b29e2df600
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|