Skip to content

Raise JsonPatchPathNotFound for operations on missing locations - #250

Open
stefankoegl wants to merge 1 commit into
masterfrom
claude/amazing-ramanujan-ge4irs
Open

stefankoegl wants to merge 1 commit into
masterfrom
claude/amazing-ramanujan-ge4irs

Conversation

@stefankoegl

Copy link
Copy Markdown
Owner

Refs #123

What

Adds JsonPatchPathNotFound, a subclass of JsonPatchConflict. It is raised when:

  • a remove targets a missing object member or array element
  • a replace targets a missing member, or an index past the end of a list
  • a move or copy has a from that doesn't exist

Other conflicts still raise plain JsonPatchConflict: removing the whole document, inserting outside a list, moving a value into its own child, or changing an immutable container. Messages are unchanged, and the failing operation is still attached as .op.

Why

Issue #123 asks for a way to treat removing an already-absent key as success. RFC 6902 requires that case to fail, so the default stays as it is. The new exception lets callers tell a missing location apart from other conflicts.

Docs

A new tutorial section, "Ignoring Missing Locations", explains that catching the exception around apply() stops the rest of the patch. It recommends a JsonPatch subclass whose operations map uses a RemoveOperation that ignores JsonPatchPathNotFound, and notes that this doesn't conform to RFC 6902. The tutorial examples run as doctests.

Compatibility notes

  • Existing except JsonPatchConflict handlers keep working.
  • The jsonpatch command prints the traceback, so its error output now shows jsonpatch.JsonPatchPathNotFound instead of jsonpatch.JsonPatchConflict in these cases. The command-line test checks for the new name.
  • A missing parent (e.g. removing /missing/key) still raises JsonPointerException, so the documented recipe doesn't skip it. This PR doesn't change that.

Tests

New PathNotFoundTests cover each case above, check that other conflicts don't use the new exception, and run the tutorial recipe. All 265 tests pass.


Generated by Claude Code

Add JsonPatchPathNotFound, a subclass of JsonPatchConflict, raised when
a remove or replace targets a non-existent member or array element, or a
move or copy reads from one. Existing handlers for JsonPatchConflict keep
working, while callers can now tell a missing location apart from other
conflicts.

Document the recommended way to get an idempotent remove: a JsonPatch
subclass whose operations map uses a RemoveOperation that ignores
JsonPatchPathNotFound.

Refs #123

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PewzpjYHG4P6Q7ZgUAgTzt
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — claude/amazing-ramanujan-ge4irs into master

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the missing-path exception and opt-in idempotent removal changes.

No open issues 🎉

What changed in this PR

Adds JsonPatchPathNotFound so callers can distinguish missing locations from other patch conflicts, supporting issue #123’s request for opt-in idempotent removal while preserving the default failure behavior.

Changes:

  • Raises the new JsonPatchConflict subclass for missing remove/replace targets and move/copy sources.
  • Documents a custom remove operation that skips missing locations and explains its limitations.
  • Adds exception and customization tests and updates the expected command-line error name.
File Description
tests.py Tests missing-location errors, other conflicts, and the idempotent removal recipe.
jsonpatch.py Defines and raises JsonPatchPathNotFound for applicable missing locations.
doc/​tutorial.rst Explains the exception and documents opt-in idempotent removal.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants