fix(exceptions): count duplicate-validator errors in ErrorTree#1476
Closed
Bojun-Vvibe wants to merge 1 commit intopython-jsonschema:mainfrom
Closed
fix(exceptions): count duplicate-validator errors in ErrorTree#1476Bojun-Vvibe wants to merge 1 commit intopython-jsonschema:mainfrom
Bojun-Vvibe wants to merge 1 commit intopython-jsonschema:mainfrom
Conversation
ErrorTree.total_errors used len(self.errors), but self.errors is a dict keyed by validator keyword, so multiple errors at the same path sharing a validator (e.g. several 'type' failures) were collapsed and undercounted. Track every error in an internal list and use its length for total_errors.
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.
Closes #442
Repo
python-jsonschema/jsonschema
Issue
#442
Root cause
ErrorTree.errorsis adictkeyed byerror.validator, so when multipleValidationErrors at the same path share the same validator keyword (e.g.several "type" failures), later ones overwrite earlier ones. Because
total_errorswas computed aslen(self.errors) + child_errors, thosecollapsed errors were silently undercounted.
Fix
Keep the per-validator
errorsdict for backward compatibility, but alsoappend every incoming error to a new internal
_all_errorslist and uselen(self._all_errors)intotal_errors. Surgical change injsonschema/exceptions.py(ErrorTree.__init__andtotal_errors).Regression test
jsonschema/tests/test_exceptions.py::TestErrorTree::test_total_errors_counts_duplicate_validatorsasserts that an
ErrorTreebuilt from threeValidationErrors sharingvalidator="type"reportstotal_errors == 3(was 1 before the fix).Risk
low
Verification
Ran
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest jsonschema/tests/test_exceptions.py -q— 60 passed, 0 failed.