fix: prevent ErrorTree index access from mutating iteration#1469
Closed
BlocksecPHD wants to merge 3 commits intopython-jsonschema:mainfrom
Closed
fix: prevent ErrorTree index access from mutating iteration#1469BlocksecPHD wants to merge 3 commits intopython-jsonschema:mainfrom
BlocksecPHD wants to merge 3 commits intopython-jsonschema:mainfrom
Conversation
Issue python-jsonschema#1328: Accessing an index in ErrorTree that has no error incorrectly adds that index to the tree's iteration. This violates the documented behavior that __iter__ iterates over 'indices in the instance with errors'. The root cause was that _contents is a defaultdict, and __getitem__ used defaultdict.__getitem__ which auto-creates entries for missing keys. Fix by: 1. In __init__, explicitly create child trees and add to _contents instead of relying on __getitem__ mutation 2. In __getitem__, check if index exists in _contents first, and return an empty ErrorTree without mutation if not Added tests to verify the fix.
for more information, see https://pre-commit.ci
Documentation build overview
Show files changed (1 files in total): 📝 1 modified | ➕ 0 added | ➖ 0 deleted
|
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
Fixes #1328
The Bug: Accessing an index in that has no error incorrectly adds that index to the tree's iteration. This violates the documented behavior that should iterate over "indices in the instance with errors".
Root Cause: is a , and used which auto-creates entries for missing keys.
Fix:
Before the fix:
After the fix:
Tests
Added 3 new tests to verify the fix:
test_index_access_does_not_mutate_tree- basic mutation fix testtest_nested_index_access_does_not_mutate_tree- nested path mutation testtest_index_access_on_empty_tree_returns_empty_tree- empty tree edge caseAll 7291 existing tests continue to pass.