Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,15 @@ class ErrorTree:

def __init__(self, errors: Iterable[ValidationError] = ()):
self.errors: MutableMapping[str, ValidationError] = {}
self._all_errors: list[ValidationError] = []
self._contents: Mapping[str, ErrorTree] = defaultdict(self.__class__)

for error in errors:
container = self
for element in error.path:
container = container[element]
container.errors[error.validator] = error
container._all_errors.append(error)

container._instance = error.instance

Expand Down Expand Up @@ -390,7 +392,7 @@ def total_errors(self):
The total number of errors in the entire tree, including children.
"""
child_errors = sum(len(tree) for _, tree in self._contents.items())
return len(self.errors) + child_errors
return len(self._all_errors) + child_errors


def by_relevance(weak=WEAK_MATCHES, strong=STRONG_MATCHES):
Expand Down
11 changes: 11 additions & 0 deletions jsonschema/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,17 @@ def test_it_knows_how_many_total_errors_it_contains(self):
tree = exceptions.ErrorTree(errors)
self.assertEqual(tree.total_errors, 8)

def test_total_errors_counts_duplicate_validators(self):
# Regression test for #442: multiple errors at the same path
# sharing the same validator keyword should all be counted.
errors = [
exceptions.ValidationError("first", validator="type"),
exceptions.ValidationError("second", validator="type"),
exceptions.ValidationError("third", validator="type"),
]
tree = exceptions.ErrorTree(errors)
self.assertEqual(tree.total_errors, 3)

def test_it_contains_an_item_if_the_item_had_an_error(self):
errors = [exceptions.ValidationError("a message", path=["bar"])]
tree = exceptions.ErrorTree(errors)
Expand Down
Loading