Skip to content

Commit

Permalink
Fix the grammar on ErrorTree's repr when it has 1 error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian committed Oct 5, 2022
1 parent 8fd12e2 commit 669bd47
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v4.16.1
=======

* Make ``ErrorTree`` have a more grammatically correct ``repr``.

v4.16.0
=======

Expand Down
4 changes: 3 additions & 1 deletion jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ def __len__(self):
return self.total_errors

def __repr__(self):
return f"<{self.__class__.__name__} ({len(self)} total errors)>"
total = len(self)
errors = "error" if total == 1 else "errors"
return f"<{self.__class__.__name__} ({total} total {errors})>"

@property
def total_errors(self):
Expand Down
16 changes: 15 additions & 1 deletion jsonschema/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,17 @@ def test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error(self):
tree = exceptions.ErrorTree([error])
self.assertIsInstance(tree["foo"], exceptions.ErrorTree)

def test_repr(self):
def test_repr_single(self):
error = exceptions.ValidationError(
"1",
validator="foo",
path=["bar", "bar2"],
instance="i1",
)
tree = exceptions.ErrorTree([error])
self.assertEqual(repr(tree), "<ErrorTree (1 total error)>")

def test_repr_multiple(self):
e1, e2 = (
exceptions.ValidationError(
"1",
Expand All @@ -412,6 +422,10 @@ def test_repr(self):
tree = exceptions.ErrorTree([e1, e2])
self.assertEqual(repr(tree), "<ErrorTree (2 total errors)>")

def test_repr_empty(self):
tree = exceptions.ErrorTree([])
self.assertEqual(repr(tree), "<ErrorTree (0 total errors)>")


class TestErrorInitReprStr(TestCase):
def make_error(self, **kwargs):
Expand Down

0 comments on commit 669bd47

Please sign in to comment.