diff options
author | Julian Berman <Julian@GrayVines.com> | 2022-10-04 17:29:13 +0100 |
---|---|---|
committer | Julian Berman <Julian@GrayVines.com> | 2022-10-05 17:29:14 +0100 |
commit | 669bd47422089d0405f42c3d32e7abf9a85310bf (patch) | |
tree | dd76fe67cf72a3b2502baf2335e68f75725025dd /jsonschema | |
parent | 8fd12e29d8ad6e44ab48e0cd54c87ffe015b6c2a (diff) | |
download | jsonschema-669bd47422089d0405f42c3d32e7abf9a85310bf.tar.gz |
Fix the grammar on ErrorTree's repr when it has 1 error.
Diffstat (limited to 'jsonschema')
-rw-r--r-- | jsonschema/exceptions.py | 4 | ||||
-rw-r--r-- | jsonschema/tests/test_exceptions.py | 16 |
2 files changed, 18 insertions, 2 deletions
diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index bb66950..87db3df 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -297,7 +297,9 @@ class ErrorTree: 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 d7b9deb..00ff300 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -396,7 +396,17 @@ class TestErrorTree(TestCase): 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", @@ -412,6 +422,10 @@ class TestErrorTree(TestCase): 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): |