Skip to content

Support whole document operations on scalar documents (#213) - #219

Merged
stefankoegl merged 3 commits into
masterfrom
claude/tender-noether-ps9xr3
Oct 10, 2026
Merged

stefankoegl merged 3 commits into
masterfrom
claude/tender-noether-ps9xr3

Conversation

@stefankoegl

@stefankoegl stefankoegl commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Fixes #213.

A JSON document can be a scalar (null, a number, a string or a boolean), but operations on the whole document (path "") raised a bare TypeError for one. That is not part of the documented exceptions, so except jsonpatch.JsonPatchException didn't catch it.

Changes

  • add to "" now replaces the document whatever its type, as replace already did. copy to "" goes through the same code and is fixed with it.
  • remove of "" now raises JsonPatchConflict("can't remove the whole document") for every type of document. Before, scalars raised a TypeError, and strings and arrays raised JsonPointerException("invalid array index 'None'"). Objects already raised JsonPatchConflict, but with the misleading message "can't remove a non-existent object 'None'".
  • replace: removes the TypeError("invalid document type ...") branches, which could no longer be reached.

move from "" is not changed here. #214 already handles it on master, and it works for scalar documents too: moving "" onto itself does nothing, and moving it anywhere else raises JsonPatchConflict. The new tests cover that case as well.

Behaviour change: remove of "" on an array (or a string) now raises JsonPatchConflict rather than JsonPointerException. Both are documented exceptions, but code that catches only JsonPointerException for this case would no longer catch it.

Tests

🤖 Generated with Claude Code

https://claude.ai/code/session_01964EYaTCKUya9tcPjhytDq

claude added 3 commits October 9, 2026 22:07
A JSON document can be a scalar, but operations on the whole document
(path "") raised a bare TypeError for one, which is not part of the
documented exceptions:

- 'add' (and 'copy', which goes through it) to "" now replaces the
  document whatever its type, as 'replace' already did.
- 'remove' of "" now raises JsonPatchConflict for every type of document,
  instead of a TypeError for scalars and a misleading "non-existent
  object 'None'" / "invalid array index 'None'" for objects and arrays.
- 'move' from "" resolves the whole document as 'copy' does, so moving
  it onto itself is a no-op and moving it elsewhere fails with
  JsonPatchConflict. This also removes the TypeError for array documents
  from #214; its check for moves into a location's own children is
  still missing.

The TypeError branches in 'replace' could no longer be reached and are
removed. test_whole_document_as_target now holds;
test_raises_only_documented_exceptions still fails, only because of #215.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01964EYaTCKUya9tcPjhytDq
master now rejects moves into a location's own children (#214), which
also covers 'move' from "". Resolved by keeping master's MoveOperation:
it returns early when 'from' is "", so the whole-document branch this
branch had added there is dropped as unreachable. The comment on the
'move' from "" example keeps master's reference to #214.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01964EYaTCKUya9tcPjhytDq
master fixes #215, which drops the 'copy' from '/-' example from
test_raises_only_documented_exceptions. Resolved by taking that removal
and keeping this branch's comment on the scalar example. With #213,
#214 and #215 all fixed, the test now holds, so its expectedFailure
marker goes too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01964EYaTCKUya9tcPjhytDq
@stefankoegl
stefankoegl merged commit 077b4f4 into master Oct 10, 2026
5 checks passed
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.

Operations on the whole document raise TypeError when the document is a scalar

2 participants