Makes partial Pydantic models without making fields nullable.
Project description
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 poetry
poetry 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'])
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_strict_partial-0.6.2.tar.gz.
File metadata
- Download URL: pydantic_strict_partial-0.6.2.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.8 Linux/6.8.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5827fccfa30c63f8a7c7303079f245bbb7af7eed6eb53a0854264072007083c
|
|
| MD5 |
6c9ac0a0fe0b8c357ccabcb54f0811a1
|
|
| BLAKE2b-256 |
bda67d5a0116e7d517f57242b0dd90272ce154713c4a5772d069059eccbf715f
|
File details
Details for the file pydantic_strict_partial-0.6.2-py3-none-any.whl.
File metadata
- Download URL: pydantic_strict_partial-0.6.2-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.8 Linux/6.8.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acb1f020eec6d892dc104184a0cc3d471622d7025216e2eb9f4b9e5ce78acebc
|
|
| MD5 |
8f90764310e744afd564f3a0fa49f562
|
|
| BLAKE2b-256 |
e5a75b40a0f327193c4cfebbc2b26df7b1b02304fca40da23825ca231a11efe9
|