Skip to content

Compare read-only mappings recursively in test operations - #257

Open
liugedragon wants to merge 1 commit into
stefankoegl:masterfrom
liugedragon:fix/readonly-mapping-tests
Open

liugedragon wants to merge 1 commit into
stefankoegl:masterfrom
liugedragon:fix/readonly-mapping-tests

Conversation

@liugedragon

Copy link
Copy Markdown

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 root test against {'a': 1}, while testing its /a member against 1 correctly fails.

Use Mapping for 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.0 equivalence, 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).
  • Standard-profile python property_tests.py: 26 tests, OK.
  • CI's fatal Flake8 checks: passed.
  • Additional 5,000-example stress run: the single process hit a Hypothesis MemoryError and replay error; the affected two properties each passed at the same budget in fresh processes.
  • git diff --check: passed.

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.

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_equal mapping comparison from MutableMapping to Mapping.
  • Add regression tests ensuring test fails 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 thread jsonpatch.py
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 thread tests.py
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

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.

2 participants