From 378c96888e01c8c8d940408ab0ff760c73fc4a5f Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Sat, 8 Jul 2017 18:22:57 +0100 Subject: [PATCH] simplifying errors --- HISTORY.rst | 1 + pydantic/exceptions.py | 2 +- tests/test_complex.py | 28 ++++++++++++++-------------- tests/test_main.py | 4 ++-- tests/test_settings.py | 2 +- tests/test_types.py | 2 +- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e4ca48e..89559f6 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -7,6 +7,7 @@ v0.4.0 (2017-XX-XX) ................... * show length in string validation error * fix aliases in config during inheritance #55 +* simplify error display * use unicode ellipsis in ``truncate`` v0.3.0 (2017-06-21) diff --git a/pydantic/exceptions.py b/pydantic/exceptions.py index 78a08f8..659d0ee 100644 --- a/pydantic/exceptions.py +++ b/pydantic/exceptions.py @@ -61,7 +61,7 @@ class ValidationError(ValueError): def __init__(self, errors): self.errors_raw = errors e_count = len(errors) - self.message = f'{e_count} error{"" if e_count == 1 else "s"} validating input' + self.message = 'error validating input' if e_count == 1 else f'{e_count} errors validating input' super().__init__(self.message) def json(self, indent=2): diff --git a/tests/test_complex.py b/tests/test_complex.py index 03a0bb1..a0d5039 100644 --- a/tests/test_complex.py +++ b/tests/test_complex.py @@ -27,7 +27,7 @@ def test_str_bytes(): with pytest.raises(ValidationError) as exc_info: StrBytesModel(v=None) - assert exc_info.value.message == '1 error validating input' + assert exc_info.value.message == 'error validating input' assert """\ { "v": [ @@ -89,7 +89,7 @@ def test_union_int_str(): with pytest.raises(ValidationError) as exc_info: Model(v=None) - assert exc_info.value.message == '1 error validating input' + assert exc_info.value.message == 'error validating input' assert """\ { "v": [ @@ -129,7 +129,7 @@ def test_typed_list(): with pytest.raises(ValidationError) as exc_info: Model(v=[1, 'x', 'y']) - assert exc_info.value.message == '1 error validating input' + assert exc_info.value.message == 'error validating input' assert """\ { "v": [ @@ -150,7 +150,7 @@ def test_typed_list(): with pytest.raises(ValidationError) as exc_info: Model(v=1) - assert exc_info.value.message == '1 error validating input' + assert exc_info.value.message == 'error validating input' assert """\ { "v": { @@ -171,7 +171,7 @@ def test_typed_set(): with pytest.raises(ValidationError) as exc_info: Model(v=[1, 'x']) assert """\ -1 error validating input +error validating input v: invalid literal for int() with base 10: 'x' (error_type=ValueError track=int index=1)""" == str(exc_info.value) @@ -197,21 +197,21 @@ def test_typed_dict(value, result): ( 1, """\ -1 error validating input +error validating input v: 'int' object is not iterable (error_type=TypeError)""" ), ( {'a': 'b'}, """\ -1 error validating input +error validating input v: invalid literal for int() with base 10: 'b' (error_type=ValueError track=int index=a)""" ), ( [1, 2, 3], """\ -1 error validating input +error validating input v: cannot convert dictionary update sequence element #0 to a sequence (error_type=TypeError)""", ) @@ -229,7 +229,7 @@ def test_dict_key_error(): with pytest.raises(ValidationError) as exc_info: DictIntModel(v={'foo': 2, '3': '4'}) assert """\ -1 error validating input +error validating input v: invalid literal for int() with base 10: 'foo' (error_type=ValueError track=int index=key)""" == str(exc_info.value) @@ -284,9 +284,9 @@ def test_recursive_list_error(): MasterListModel(v=[{}]) assert """\ -1 error validating input +error validating input v: - 1 error validating input (error_type=ValidationError track=SubModel) + error validating input (error_type=ValidationError track=SubModel) name: field required (error_type=Missing)\ """ == str(exc_info.value) @@ -302,7 +302,7 @@ v: "track": null } }, - "error_msg": "1 error validating input", + "error_msg": "error validating input", "error_type": "ValidationError", "index": 0, "track": "SubModel" @@ -320,7 +320,7 @@ def test_list_unions(): with pytest.raises(ValidationError) as exc_info: Model(v=[1, 2, None]) assert """\ -1 error validating input +error validating input v: int() argument must be a string, a bytes-like object or a number, not 'NoneType' \ (error_type=TypeError track=int index=2) @@ -389,7 +389,7 @@ def test_alias_error(): with pytest.raises(ValidationError) as exc_info: Model(_a='foo') assert """\ -1 error validating input +error validating input _a: invalid literal for int() with base 10: 'foo' (error_type=ValueError track=int)""" == str(exc_info.value) diff --git a/tests/test_main.py b/tests/test_main.py index 5459432..128bb3f 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -26,7 +26,7 @@ def test_ultra_simple_missing(): with pytest.raises(ValidationError) as exc_info: UltraSimpleModel() assert """\ -1 error validating input +error validating input a: field required (error_type=Missing)""" == str(exc_info.value) @@ -299,7 +299,7 @@ def test_required(): with pytest.raises(ValidationError) as exc_info: Model() assert """\ -1 error validating input +error validating input a: field required (error_type=Missing)\ """ == str(exc_info.value) diff --git a/tests/test_settings.py b/tests/test_settings.py index 1856a93..324c0e5 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -23,7 +23,7 @@ def test_sub_env_missing(): with pytest.raises(ValidationError) as exc_info: SimpleSettings() assert """\ -1 error validating input +error validating input apple: None is not an allow value (error_type=TypeError track=str)\ """ == str(exc_info.value) diff --git a/tests/test_types.py b/tests/test_types.py index 76c73c8..8f78f3a 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -240,7 +240,7 @@ def test_enum_successful(): def test_enum_fails(): with pytest.raises(ValueError) as exc_info: CookingModel(tool=3) - assert exc_info.value.message == '1 error validating input' + assert exc_info.value.message == 'error validating input' assert """\ { "tool": {