Repository navigation
Compare read-only mappings recursively in test operations - #257
Open
liugedragon wants to merge 1 commit into
Open
liugedragon wants to merge 1 commit into
liugedragon wants to merge 1 commit into
Conversation
Copilot stopped reviewing on behalf of
stefankoegl due to an error
November 9, 2026 05:44
Contributor
There was a problem hiding this comment.
Note
Copilot was unable to run its full agentic suite in this review.
Copilot review overview
2 open issues
What changed in this PR
This PR updates JSON Patch test operation equality semantics so read-only mapping types (e.g., MappingProxyType) are compared structurally instead of via Python’s default equality, preventing True/1 and False/0 from being treated as equal.
Changes:
- Broaden
_json_equalmapping comparison fromMutableMappingtoMapping. - Add regression tests ensuring
testfails for bool-vs-int mismatches on read-only mappings and does not mutate input documents on success/failure.
| File | Description |
|---|---|
jsonpatch.py |
Fixes _json_equal to structurally compare read-only mappings, preserving RFC6902 bool/number distinction. |
tests.py |
Adds regression coverage for MappingProxyType and nested read-only mapping scenarios (success and failure). |
Review effort: Lite
Comment on lines
1459
to
1465
| if isinstance(first, bool) or isinstance(second, bool): | ||
| return isinstance(first, bool) and isinstance(second, bool) and \ | ||
| first == second | ||
| if isinstance(first, MutableMapping) and \ | ||
| isinstance(second, MutableMapping): | ||
| if isinstance(first, Mapping) and isinstance(second, Mapping): | ||
| return len(first) == len(second) and all( | ||
| key in second and _json_equal(first[key], second[key]) | ||
| for key in first) |
Comment on lines
+449
to
+466
| def test_test_read_only_mapping_preserves_boolean_types(self): | ||
| for mapping_type in (dict, MappingProxyType): | ||
| for nested in (False, True): | ||
| for actual, expected in ((True, 1), (False, 0), | ||
| (1, True), (0, False)): | ||
| with self.subTest(mapping_type=mapping_type, nested=nested, | ||
| actual=actual, expected=expected): | ||
| mapping = mapping_type({'a': actual}) | ||
| doc = {'nested': mapping} if nested else mapping | ||
| path = '/nested' if nested else '' | ||
| original = json.dumps(doc, default=dict) | ||
| patch = [{'op': 'test', 'path': path, | ||
| 'value': {'a': expected}}] | ||
|
|
||
| with self.assertRaises(jsonpatch.JsonPatchTestFailed): | ||
| jsonpatch.apply_patch(doc, patch, in_place=True) | ||
|
|
||
| self.assertEqual(json.dumps(doc, default=dict), original) |
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.

Testing a whole read-only mapping currently falls back to Python equality rather than recursively applying JSON equality. For example,
MappingProxyType({'a': True})passes a roottestagainst{'a': 1}, while testing its/amember against1correctly fails.Use
Mappingfor the read-only object comparison in_json_equal. This applies the existing RFC 6902 type rules to mapping implementations already accepted by the module, including the types documented in its stubs. Mutation requirements and the sequence branch are unchanged.Three test methods cover 39 subcases: native/read-only and root/nested objects, Boolean/number rejection, numeric
1/1.0equivalence, successful return identity, and unchanged input content. Eleven read-only Boolean/number subcases fail before the fix and pass after it.Validation on Windows / Python 3.13.4:
python tests.py: 317 tests run, OK (1 skipped).python property_tests.py: 26 tests, OK.git diff --check: passed.