Fix ErrorTree.__getitem__ mutating the tree on error-free access - #1551
Closed
agu2347 wants to merge 1 commit into
Closed
Fix ErrorTree.__getitem__ mutating the tree on error-free access#1551agu2347 wants to merge 1 commit into
agu2347 wants to merge 1 commit into
Conversation
`ErrorTree._contents` is a `defaultdict(ErrorTree)`. `__getitem__` accessed `self._contents[index]` directly, and merely *reading* a missing key on a defaultdict inserts it. So calling `tree[index]` for an index that had no validation errors of its own silently added that index to `_contents`, which is exactly what `__contains__` and `__iter__` check -- causing `index in tree` and `list(tree)` to change after a read-only lookup, contradicting the documented "Check whether instance[index] has any errors" contract. Populate `_contents` directly during tree construction in `__init__` (where auto-vivification is genuinely wanted, since we're recording real error paths), and make `__getitem__` return a fresh, unstored `ErrorTree()` for indices with no recorded errors instead of touching `_contents`. `total_errors`/`__len__`/`__repr__`, which also iterate `_contents`, are fixed as a consequence. Fixes python-jsonschema#1328
Documentation build overview
|
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.
Summary
ErrorTree.__contains__/__iter__change their results after a read-onlyErrorTree.__getitem__access on an index that had no errors, contradicting the documented contract ("Check whetherinstance[index]has any errors").Root cause
ErrorTree._contentsis adefaultdict(ErrorTree).__getitem__didreturn self._contents[index]directly -- and on adefaultdict, merely reading a missing key inserts it. So a read-only lookup for an error-free (but otherwise valid) index permanently adds that index to_contents, which is exactly what__contains__/__iter__/total_errorscheck.Fix
__init__(tree construction) is the only place that should auto-vivify entries, since it's recording real error paths -- so it now populates_contentsdirectly (container._contents[element]) instead of going through the public__getitem__.__getitem__no longer touches_contentsfor a miss: it validates the index againstself._instance(still propagatingLookupErrorfor genuinely invalid indices, per the existing docstring/tests) and returns a fresh, unstoredErrorTree()for an index with no recorded errors, so_contents-- and therefore__contains__/__iter__/total_errors/__len__/__repr__, which all read_contents-- stay accurate.Testing
test_getitem_of_an_error_free_index_does_not_mutate_the_treetojsonschema/tests/test_exceptions.py, reproducing the exact scenario from the issue and assertinglist(tree)/in/total_errorsare unchanged before and after accessing the error-free index.jsonschema/tests/: 8281 passed, 232 skipped (unrelated/environment-gated), 0 failed.jsonschema/exceptions.py(keeping the new test) reproduces the exact reported bug:AssertionError: Lists differ: [0, 1] != [0].ruff checkclean on both changed files.Fixes #1328