pydantic-deep-patch
⚡️ Deep diffing, RFC 6902 JSON-Patch, RFC 7396 Merge-Patch, and type-safe audit logging for Pydantic v2 models.
💡 Why pydantic-deep-patch?
Handling partial API updates (PATCH /items/{id}) and tracking audit logs across nested Pydantic v2 models is usually painful and repetitive.
pydantic-deep-patch provides:
- 🔍 RFC 6902 JSON-Patch: Generate standard JSON Patch operations (
add,remove,replace) between any two Pydantic model instances. - 🛠 Safe Model Application (
apply_patch): Apply patches with automatic Pydantic v2 validation. - 📋 Human-Readable Audit Trails (
diff_audit): Generate human-readable change logs for database audits or UI activity feeds. - 🔄 RFC 7396 Merge-Patch (
merge_patch): Support standard JSON Merge-Patch dictionaries.
📦 Installation
pip install pydantic-deep-patch
🚀 Quick Usage
from pydantic import BaseModel
from pydantic_deep_patch import diff, apply_patch, diff_audit, merge_patch
class Profile(BaseModel):
name: str
age: int
class User(BaseModel):
id: str
profile: Profile
# 1. Compute RFC 6902 Diffs
u1 = User(id="1", profile=Profile(name="Alice", age=25))
u2 = User(id="1", profile=Profile(name="Alice", age=26))
patch_ops = diff(u1, u2)
# Output: [PatchOp(op='replace', path='/profile/age', value=26)]
# 2. Apply patch to update a model
updated_user = apply_patch(u1, patch_ops)
# 3. Generate Audit Log
audit_log = diff_audit(u1, u2)
# Output: [AuditEntry(field_name='age', action='modified', description="Field 'age' changed from 25 to 26")]
# 4. JSON Merge Patch (RFC 7396)
updated_merge = merge_patch(u1, {"profile": {"name": "Alice Smith"}})
📄 License
MIT © Raj Dubey
Release files for pydantic-deep-patch 0.1.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_deep_patch-0.1.0.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_deep_patch-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.6 kB
Release files / pydantic_deep_patch-0.1.0.tar.gz
| Download URL | pydantic_deep_patch-0.1.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b1983e9352d6bb0f774c6eb4efbcce38d6db13be96ddc12ecf679d2fcdfbe39
|
|
BLAKE2b-256 checksum How to use checksums |
adb331a28a276fc95e7cfcb4e53718dee4d5d4da7356757bd30d571c32049387
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.4
|
Release files / pydantic_deep_patch-0.1.0-py3-none-any.whl
| Download URL | pydantic_deep_patch-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70b46fc66bf1e7493be4eb4099ab8d26475a3b0ac9996bfaa4ecf1479e45fd1f
|
|
BLAKE2b-256 checksum How to use checksums |
dc8124449ce9ae982ad358048afaa9c5f1458afd8c04b18329aa02b79fda38de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.4
|