Skip to content

Commit 669bd47

Browse files
committed
Fix the grammar on ErrorTree's repr when it has 1 error.
1 parent 8fd12e2 commit 669bd47

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v4.16.1
2+
=======
3+
4+
* Make ``ErrorTree`` have a more grammatically correct ``repr``.
5+
16
v4.16.0
27
=======
38

jsonschema/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ def __len__(self):
297297
return self.total_errors
298298

299299
def __repr__(self):
300-
return f"<{self.__class__.__name__} ({len(self)} total errors)>"
300+
total = len(self)
301+
errors = "error" if total == 1 else "errors"
302+
return f"<{self.__class__.__name__} ({total} total {errors})>"
301303

302304
@property
303305
def total_errors(self):

jsonschema/tests/test_exceptions.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,17 @@ def test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error(self):
396396
tree = exceptions.ErrorTree([error])
397397
self.assertIsInstance(tree["foo"], exceptions.ErrorTree)
398398

399-
def test_repr(self):
399+
def test_repr_single(self):
400+
error = exceptions.ValidationError(
401+
"1",
402+
validator="foo",
403+
path=["bar", "bar2"],
404+
instance="i1",
405+
)
406+
tree = exceptions.ErrorTree([error])
407+
self.assertEqual(repr(tree), "<ErrorTree (1 total error)>")
408+
409+
def test_repr_multiple(self):
400410
e1, e2 = (
401411
exceptions.ValidationError(
402412
"1",
@@ -412,6 +422,10 @@ def test_repr(self):
412422
tree = exceptions.ErrorTree([e1, e2])
413423
self.assertEqual(repr(tree), "<ErrorTree (2 total errors)>")
414424

425+
def test_repr_empty(self):
426+
tree = exceptions.ErrorTree([])
427+
self.assertEqual(repr(tree), "<ErrorTree (0 total errors)>")
428+
415429

416430
class TestErrorInitReprStr(TestCase):
417431
def make_error(self, **kwargs):

0 commit comments

Comments
 (0)