diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a64a200d8..a6a18e6a4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +v4.16.1 +======= + +* Make ``ErrorTree`` have a more grammatically correct ``repr``. + v4.16.0 ======= diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index bb669507e..87db3df3a 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -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): diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py index d7b9deb74..00ff30091 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -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), "") + + def test_repr_multiple(self): e1, e2 = ( exceptions.ValidationError( "1", @@ -412,6 +422,10 @@ def test_repr(self): tree = exceptions.ErrorTree([e1, e2]) self.assertEqual(repr(tree), "") + def test_repr_empty(self): + tree = exceptions.ErrorTree([]) + self.assertEqual(repr(tree), "") + class TestErrorInitReprStr(TestCase): def make_error(self, **kwargs):