Repository navigation
Raise JsonPatchPathNotFound for operations on missing locations - #250
Open
stefankoegl wants to merge 1 commit into
Open
stefankoegl wants to merge 1 commit into
stefankoegl wants to merge 1 commit into
Conversation
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
Contributor
There was a problem hiding this comment.
🟢 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
JsonPatchConflictsubclass 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #123
What
Adds
JsonPatchPathNotFound, a subclass ofJsonPatchConflict. It is raised when:removetargets a missing object member or array elementreplacetargets a missing member, or an index past the end of a listmoveorcopyhas afromthat doesn't existOther 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 aJsonPatchsubclass whoseoperationsmap uses aRemoveOperationthat ignoresJsonPatchPathNotFound, and notes that this doesn't conform to RFC 6902. The tutorial examples run as doctests.Compatibility notes
except JsonPatchConflicthandlers keep working.jsonpatchcommand prints the traceback, so its error output now showsjsonpatch.JsonPatchPathNotFoundinstead ofjsonpatch.JsonPatchConflictin these cases. The command-line test checks for the new name./missing/key) still raisesJsonPointerException, so the documented recipe doesn't skip it. This PR doesn't change that.Tests
New
PathNotFoundTestscover 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