Skip to main content

⚡ patchloom

Zero-dependency RFC 6902 JSON Patch, RFC 7396 Merge Patch, reversible structural diff & three-way merge engine for Python 3.12+.

PyPI Version Python Versions License: MIT CI Typing: Typed Dependencies: Zero


Why patchloom?

Event-sourced backends, configuration control planes, optimistic HTTP PATCH APIs, audit logs, and collaborative state engines all share the same core primitives: exact structural diffing, transactional patching, deterministic undo/rollback, and three-way merging.

Existing Python libraries typically cover only one slice of the problem, rely on naive index-by-index list comparison, conflate Python booleans with integers (True == 1), or mutate target documents half-way through a failed patch.

patchloom unifies all four standards in a compact, strictly typed, zero-dependency Python 3.12+ package:

  • RFC 6901 JSON Pointer: Immutable pointer arithmetic (Pointer("/users") / 0 / "role"), URI fragment decoding, strict ~0/~1 unescaping, and * / ** glob matching.
  • RFC 6902 JSON Patch: All 6 operations (add, remove, replace, move, copy, test) with 100% transactional atomicity (failed patches never leave partial mutations) and strict JSON type equality (True != 1, False != 0).
  • LCS & Keyed Structural Diff: Longest Common Subsequence array alignment, identity-keyed record diffing (array_keys="id"), automatic move & copy compression, wildcard path exclusions, float epsilon tolerance, and dataclass / Enum normalization.
  • Reversible Deltas (ReversiblePatch): Generates paired forward and inverse RFC 6902 patches guarded by test preconditions for safe state rollback and optimistic concurrency checks.
  • RFC 7396 JSON Merge Patch: Minimal merge-patch generation and spec-compliant application.
  • Three-Way Structural Merge (merge3): Merges concurrent changes (base, ours, theirs) across nested objects and keyed arrays with fine-grained conflict classification and pluggable resolution policies.

Architecture

flowchart LR
    Src["Source State (v1)"] --> Diff["LCS + Keyed Diff Engine"]
    Dst["Target State (v2)"] --> Diff
    Diff --> Fwd["Forward Patch (RFC 6902)"]
    Diff --> Rev["Reverse Patch (test + inverse ops)"]
    Fwd --> Apply["Transactional Patch Executor"]
    Apply --> Out["Committed State (v2)"]
    Rev --> Apply

    Base["Base Document"] --> M3["3-Way Structural Merger"]
    Ours["Ours Branch"] --> M3
    Theirs["Theirs Branch"] --> M3
    M3 --> Policy["ConflictPolicy / Custom Resolver"]
    Policy --> Merged["Merged Result + Conflict Report"]

Installation

pip install patchloom

Requires Python 3.12 or newer. Zero third-party runtime dependencies.


Quick Start

1. Structural Diff & Atomic RFC 6902 Patching

diff() computes minimal patches using Longest Common Subsequence (LCS) alignment on arrays and automatically coalesces renamed keys into move operations:

from patchloom import apply_patch, diff

before = {
    "service": "billing-api",
    "legacy_timeout": 30,
    "regions": ["eu-central-1", "us-east-1"],
}
after = {
    "service": "billing-api",
    "request_timeout": 30,
    "regions": ["eu-central-1", "ap-south-1", "us-east-1"],
}

patch = diff(before, after)
print(patch.to_json(indent=2))
# [
#   { "op": "move", "path": "/request_timeout", "from": "/legacy_timeout" },
#   { "op": "add", "path": "/regions/1", "value": "ap-south-1" }
# ]

assert apply_patch(before, patch) == after

2. Reversible Patches & Optimistic Concurrency Rollback

diff_reversible() emits both forward and reverse RFC 6902 patches. When include_tests=True (the default), rollback operations assert current field values via RFC 6902 test guards before reverting—preventing silent data loss if another writer modified the state in between:

from patchloom import PatchTestFailedError, diff_reversible

v1 = {"plan": "starter", "seats": 5, "features": ["export"]}
v2 = {"plan": "enterprise", "seats": 25, "features": ["export", "sso"]}

delta = diff_reversible(v1, v2)

current = delta.apply(v1)
assert current == v2

restored = delta.rollback(current)
assert restored == v1

# Concurrent modification is caught atomically before any rollback mutation occurs:
tampered = {**current, "seats": 99}
try:
    delta.rollback(tampered)
except PatchTestFailedError as err:
    print(err)  # Test failed at '/seats': expected 25, got 99.

3. Keyed Array Diffing & Wildcard Ignore Patterns

Align collections of domain entities by primary key (array_keys) while ignoring volatile metadata like timestamps or secrets (* and ** globs):

from patchloom import diff

prev = {
    "updated_at": "2026-11-17T08:00:00Z",
    "workers": [
        {"id": "w-1", "concurrency": 4, "heartbeat": 101},
        {"id": "w-2", "concurrency": 8, "heartbeat": 102},
    ],
}
curr = {
    "updated_at": "2026-11-18T10:00:00Z",
    "workers": [
        {"id": "w-0", "concurrency": 2, "heartbeat": 200},
        {"id": "w-1", "concurrency": 16, "heartbeat": 201},
        {"id": "w-2", "concurrency": 8, "heartbeat": 202},
    ],
}

patch = diff(
    prev,
    curr,
    array_keys={"/workers": "id"},
    ignore=["/updated_at", "/workers/*/heartbeat"],
)

print(patch.to_list())
# [
#   {'op': 'add', 'path': '/workers/0', 'value': {'id': 'w-0', 'concurrency': 2, 'heartbeat': 200}},
#   {'op': 'replace', 'path': '/workers/1/concurrency', 'value': 16}
# ]

4. Three-Way Structural Merge (merge3)

Merge concurrent edits relative to a common ancestor with built-in policies (RAISE, OURS, THEIRS, BASE) or a custom conflict resolver function:

from patchloom import ConflictPolicy, MergeConflict, merge3

base = {"replicas": 2, "limits": {"cpu": "500m", "mem": "512Mi"}}
ours = {"replicas": 4, "limits": {"cpu": "1000m", "mem": "512Mi"}}
theirs = {"replicas": 6, "limits": {"cpu": "500m", "mem": "1Gi"}}

# Custom resolver: pick the higher replica count on conflict
def resolve_higher_replicas(conflict: MergeConflict):
    if str(conflict.path) == "/replicas":
        return max(conflict.ours, conflict.theirs)
    return conflict.ours

result = merge3(base, ours, theirs, policy=resolve_higher_replicas)
assert result.value == {"replicas": 6, "limits": {"cpu": "1000m", "mem": "1Gi"}}
assert len(result.conflicts) == 1

5. RFC 7396 JSON Merge Patch & RFC 6901 JSON Pointer

from patchloom import Pointer, apply_merge_patch, create_merge_patch

doc = {"title": "v1", "author": {"name": "Hamidreza", "temp": True}}
target = {"title": "v2", "author": {"name": "Hamidreza"}}

mp = create_merge_patch(doc, target)
# {'author': {'temp': None}, 'title': 'v2'}
assert apply_merge_patch(doc, mp) == target

ptr = Pointer.root() / "author" / "name"
assert ptr.resolve(doc) == "Hamidreza"

Command-Line Interface

patchloom ships with a built-in CLI for shell pipelines, CI checks, and Gitops workflows:

# Generate an RFC 6902 JSON Patch
patchloom diff old.json new.json --array-key id --ignore "**/updated_at" > changes.patch.json

# Generate paired forward + reverse patches
patchloom diff --reversible old.json new.json > delta.json

# Apply a patch atomically (exits non-zero if any 'test' precondition fails)
patchloom apply old.json changes.patch.json > applied.json

# Three-way merge with conflict policy
patchloom merge3 base.json ours.json theirs.json --policy ours --array-key id

# Extract a value via RFC 6901 JSON Pointer
patchloom get config.json "/database/pool/max_connections"

Standards Implemented

Specification Scope Module
RFC 6901 JavaScript Object Notation (JSON) Pointer patchloom.pointer
RFC 6902 JavaScript Object Notation (JSON) Patch patchloom.patch / patchloom.diff
RFC 7396 JSON Merge Patch patchloom.merge_patch
3-Way Merge Recursive Object & Keyed-Collection Structural Merge patchloom.merge3

Development & Testing

git clone https://github.com/mrhamidrezam/patchloom.git
cd patchloom
pytest

License

Released under the MIT License.

Metadata

Release files for patchloom 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for patchloom 0.1.0
File Size Uploaded Provenance Actions
patchloom-0.1.0.tar.gz 33.5 kB With provenance Details for patchloom-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for patchloom 0.1.0
File Interpreter ABI Platform Actions
patchloom-0.1.0-py3-none-any.whl Python 3 none any Details for patchloom-0.1.0-py3-none-any.whl

Total release size: 61.2 kB

Release files / patchloom-0.1.0.tar.gz

Download URL patchloom-0.1.0.tar.gz
Size 33.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d7ee7208c7743c3ab8b44d95888a8395f5783528b7093bde18dbb930ac136553
BLAKE2b-256 checksum
How to use checksums
26ee4e0e95039f747b9aebbd7661ae2455287861cf529c947b05d989e0b05656
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / patchloom-0.1.0-py3-none-any.whl

Download URL patchloom-0.1.0-py3-none-any.whl
Size 27.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e41f9c9c1460a244913291d312c26df2753010114cf6e23da15d1b8caf0186a
BLAKE2b-256 checksum
How to use checksums
649a3ad2fd7a6dbcf3bcc4cdd6852a14d6916ac0af9a804667159b95d10fcc8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Security for patchloom 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for patchloom-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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