Skip to content

Serialize and compare patches whose paths are pointer objects - #272

Open
stefankoegl wants to merge 1 commit into
masterfrom
claude/pointer-object-paths
Open

stefankoegl wants to merge 1 commit into
masterfrom
claude/pointer-object-paths

Conversation

@stefankoegl

Copy link
Copy Markdown
Owner

The path and from of an operation can be given as pointer objects instead of strings. Such a patch could be applied, but:

>>> patch = JsonPatch([{'op': 'add', 'path': JsonPointer('/a'), 'value': 1}])
>>> patch.to_string()
TypeError: Object of type JsonPointer is not JSON serializable
>>> patch == JsonPatch([{'op': 'add', 'path': '/a', 'value': 1}])
False

Changes

  • JsonPatch replaces pointer objects by their paths in patch.patch, so to_string(), str(), iteration, == and hash() all see strings. This is done in copies: the operation dicts the patch was created from keep their pointer objects.
  • PatchOperation.__eq__ and __hash__ compare by the paths too, so an operation created directly with a pointer object equals one created with the string.

What stays the same

A PatchOperation still shares the dict it was created from, also when that dict holds pointer objects, because its setters (key, from_key, …) write back into it and an existing test relies on that. My first attempt copied the dict there and broke that test, so the normalisation happens only in JsonPatch and in the comparisons.

Visible change

patch.patch now contains path strings where it used to contain the pointer objects that were passed in.

Tests

Seven new tests: serialisation, equality and hashing of patches and of single operations, read-only mapping inputs, a pointer class with a different path syntax, the setters, and that the caller's operations are not modified.

python tests.py and property_tests.py pass with coverage at 100%; flake8 is clean (Python 3.13).


Generated by Claude Code

The 'path' and 'from' of an operation can be given as pointer objects
instead of strings. A JsonPatch created from such operations could be
applied, but to_string() raised a TypeError, as its operations still
contained the pointer objects, and it was not equal to the same patch
given the paths as strings.

JsonPatch now replaces pointer objects by their paths in its operations,
in copies, so the operations it was created from are unchanged. Patch
operations compare and hash by the paths as well. Their setters keep
updating the operation they were created from.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PewzpjVeke4LTQ7Q88nyu6
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — claude/pointer-object-paths into master

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.

3 participants