Skip to content

Commit b562814

Browse files
author
Sylvain MARIE
committed
Error messages improvements: removed the brackets in Wrong value: [...] for the Failure details. Fixed #32
Also fixed a few tests where `Failure()` did not have a first argument with the value.
1 parent f9b37a8 commit b562814

8 files changed

Lines changed: 34 additions & 34 deletions

File tree

docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ results in
7373

7474
```bash
7575
ValidationError[ValueError]: Error validating [surface=-1]. \
76-
TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1].
76+
TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1.
7777
```
7878

7979
Note that the resulting exception object contains much of the available information (`var_name`, `var_value`, `validation_outcome`) as fields.
@@ -201,7 +201,7 @@ yields for `surf = -1`
201201
```bash
202202
InvalidSurface[ValueError]: Surface should be a positive integer. \
203203
Error validating [surface=-1]. \
204-
TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1].
204+
TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1.
205205
```
206206

207207
### (c) message templating
@@ -221,7 +221,7 @@ yields
221221
```bash
222222
InvalidSurface[ValueError]: Surface should be > 0, found -1. \
223223
Error validating [surface=-1]. \
224-
TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1].
224+
TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1.
225225
```
226226

227227
Note: as shown in that last example, the value being validated is already sent to the help message string to format under the `'var_value'` key, so you do not need to pass it.

valid8/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def get_details(self):
311311
if len(strval) > self.__max_str_length_displayed__:
312312
return '(Actual value is too big to be printed in this message)'
313313
else:
314-
return 'Wrong value: [{}]'.format(self.wrong_value)
314+
return 'Wrong value: %s' % repr(self.wrong_value)
315315

316316

317317
class WrappingFailure(Failure):

valid8/entry_points.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ def get_details(self):
301301
''.format(what=self.get_what_txt(), val=self.validator.get_main_function_name(),
302302
result=self.validation_outcome)
303303

304-
# return 'Wrong value: [{}]'.format(self.var_value)
305304
return contents
306305

307306
def get_variable_str(self):

valid8/tests/readme/test_readme_index.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_readme_index_usage_quick():
1515
validate('surface', surf, instance_of=int, min_value=0)
1616
e = exc_info.value
1717
assert str(e) == "Error validating [surface=-1]. " \
18-
"TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1]."
18+
"TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1."
1919

2020

2121
def test_readme_usage_validator():
@@ -56,7 +56,7 @@ def test_readme_usage_validator():
5656
v.alid = surf > 0
5757
e = exc_info.value
5858
assert str(e) == "Error validating [surface=1j]. " \
59-
"HasWrongType: Value should be an instance of %s. Wrong value: [1j]." % repr(int)
59+
"HasWrongType: Value should be an instance of %s. Wrong value: 1j." % repr(int)
6060

6161
from valid8 import assert_instance_of
6262
with pytest.raises(ValidationError) as exc_info:
@@ -66,7 +66,7 @@ def test_readme_usage_validator():
6666
e = exc_info.value
6767
assert str(e) == "Error validating [surface=1j]. " \
6868
"Validation function [assert_instance_of(surf, int) ; v.alid = surf > 0] raised " \
69-
"HasWrongType: Value should be an instance of %s. Wrong value: [1j]." % repr(int)
69+
"HasWrongType: Value should be an instance of %s. Wrong value: 1j." % repr(int)
7070

7171

7272
def test_readme_usage_customization():
@@ -79,7 +79,7 @@ def test_readme_usage_customization():
7979
# help_msg="Surface should be a positive integer")
8080
# e = exc_info.value
8181
# assert str(e) == "Surface should be a positive integer. Error validating [surface=-1]. " \
82-
# "TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1]."
82+
# "TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1."
8383

8484
# (A) custom error message (exception is still a ValidationError)
8585
with pytest.raises(ValidationError) as exc_info:
@@ -99,7 +99,7 @@ class InvalidSurface(ValidationError):
9999
assert isinstance(e, InvalidSurface)
100100
assert str(e) == "Surface should be a positive integer. " \
101101
"Error validating [surface=-1]. " \
102-
"TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1]."
102+
"TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1."
103103

104104
# (C) custom error types with templating
105105
class InvalidSurface(ValidationError):
@@ -112,13 +112,13 @@ class InvalidSurface(ValidationError):
112112
assert isinstance(e, InvalidSurface)
113113
assert type(e).__name__ == "InvalidSurface[ValueError]"
114114
assert str(e) == "Surface should be > 0, found -1. Error validating [surface=-1]. " \
115-
"TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1]."
115+
"TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1."
116116

117117
# (D) ValueError/TypeError
118118
with pytest.raises(ValidationError) as exc_info:
119119
validate('surface', -1, instance_of=int, min_value=0)
120120
e = exc_info.value
121-
assert str(e) == "Error validating [surface=-1]. TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1]."
121+
assert str(e) == "Error validating [surface=-1]. TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1."
122122
assert repr(e.__class__) == "<class 'valid8.entry_points.ValidationError[ValueError]'>"
123123

124124
with pytest.raises(ValidationError) as exc_info:
@@ -142,7 +142,7 @@ def test_readme_index_usage_basic():
142142
e = exc_info.value
143143
assert str(e) == 'Error validating [surface=-1]. ' \
144144
'Validation function [is_multiple_of_100] raised ' \
145-
'IsNotMultipleOf: Value should be a multiple of 100. Wrong value: [-1].'
145+
'IsNotMultipleOf: Value should be a multiple of 100. Wrong value: -1.'
146146

147147
# (2) native mini_lambda support to define validation functions
148148
from mini_lambda import x
@@ -281,7 +281,7 @@ def test_readme_index_usage_customization():
281281
assert_valid('surface', None, x > 0, none_policy=NonePolicy.FAIL)
282282
e = exc_info.value
283283
assert str(e) == 'Error validating [surface=None]. Validation function [reject_none(x > 0)] raised ' \
284-
'ValueIsNone: The value must be non-None. Wrong value: [None].'
284+
'ValueIsNone: The value must be non-None. Wrong value: None.'
285285

286286
# *** (4) TEST: custom Failure (not ValidationError) message. Does it have any interest ? ***
287287
with pytest.raises(ValidationError) as exc_info:
@@ -291,7 +291,7 @@ def test_readme_index_usage_customization():
291291
'Validation function [is_multiple_of_100] raised ' \
292292
'WrappingFailure: Surface should be a multiple of 100. ' \
293293
'Function [is_multiple_of_100] raised ' \
294-
'[IsNotMultipleOf: Value should be a multiple of 100. Wrong value: [-1]].'
294+
'[IsNotMultipleOf: Value should be a multiple of 100. Wrong value: -1].'
295295

296296
# (4) custom error message (exception is still a ValidationError)
297297
with pytest.raises(ValidationError) as exc_info:
@@ -311,7 +311,7 @@ class InvalidSurface(ValidationError):
311311
assert str(e) == 'Surface should be a positive number. ' \
312312
'Error validating [surface=-1]. ' \
313313
'Validation function [is_multiple_of_100] raised ' \
314-
'IsNotMultipleOf: Value should be a multiple of 100. Wrong value: [-1].'
314+
'IsNotMultipleOf: Value should be a multiple of 100. Wrong value: -1.'
315315

316316
# (6) custom error types with templating
317317
class InvalidSurface(ValidationError):
@@ -341,7 +341,7 @@ def test_readme_index_usage_composition():
341341
"Validation function [and((x >= 0) & (x < 10000), is_multiple_of_100)] raised " \
342342
"AtLeastOneFailed: At least one validation function failed validation for value [-1]. " \
343343
"Successes: [] / Failures: {'(x >= 0) & (x < 10000)': 'False', " \
344-
"'is_multiple_of_100': 'IsNotMultipleOf: Value should be a multiple of 100. Wrong value: [-1]'}."
344+
"'is_multiple_of_100': 'IsNotMultipleOf: Value should be a multiple of 100. Wrong value: -1'}."
345345

346346
# (8) ... with a global custom error type. Oh by the way this supports templating
347347
class InvalidSurface(ValidationError):
@@ -358,7 +358,7 @@ class InvalidSurface(ValidationError):
358358
"AtLeastOneFailed: At least one validation function failed validation for value [-1]. " \
359359
"Successes: [] / Failures: {" \
360360
"'(x >= 0) & (x < 10000)': 'False', " \
361-
"'is_multiple_of_100': 'IsNotMultipleOf: Value should be a multiple of 100. Wrong value: [-1]'}."
361+
"'is_multiple_of_100': 'IsNotMultipleOf: Value should be a multiple of 100. Wrong value: -1'}."
362362

363363
# (9) ... and possible user-friendly intermediate failure messages
364364
with pytest.raises(ValidationError) as exc_info:
@@ -374,7 +374,7 @@ class InvalidSurface(ValidationError):
374374
"Function [(x >= 0) & (x < 10000)] returned [False] for value [-1].', " \
375375
"'is_multiple_of_100': 'WrappingFailure: Surface should be a multiple of 100. " \
376376
"Function [is_multiple_of_100] raised [IsNotMultipleOf: Value should be a multiple of 100. " \
377-
"Wrong value: [-1]].'}."
377+
"Wrong value: -1].'}."
378378

379379
# *********** other even more complex tests ***********
380380

@@ -396,4 +396,4 @@ class InvalidSurface(ValidationError):
396396
"Function [(x >= 0) & (x < 10000)] returned [False] for value [-1].', " \
397397
"'is_multiple_of_100': 'WrappingFailure: Surface should be a multiple of 100, found -1. " \
398398
"Function [is_multiple_of_100] raised [IsNotMultipleOf: Value should be a multiple of 100. " \
399-
"Wrong value: [-1]].'}."
399+
"Wrong value: -1].'}."

valid8/tests/readme/test_readme_usage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def hello(age):
4747
assert str(e) == "Error validating [age=152]. Validation function [and(isfinite, between_0_and_150)] raised " \
4848
"AtLeastOneFailed: At least one validation function failed validation for value [152]. " \
4949
"Successes: ['isfinite'] / Failures: {" \
50-
"'between_0_and_150': 'NotInRange: 0 <= x <= 150 does not hold for x=152. Wrong value: [152]'}."
50+
"'between_0_and_150': 'NotInRange: 0 <= x <= 150 does not hold for x=152. Wrong value: 152'}."
5151

5252
# v3: age is an integer
5353
# https://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not
@@ -227,7 +227,7 @@ def test_usage_validators():
227227
validate_is_greater_than_1('val',0.2)
228228
e = exc_info.value
229229
assert str(e) == "Error validating [val=0.2]. Validation function [gt_1] raised " \
230-
"Failure: Wrong value: [x is not greater than 1, x=0.2]."
230+
"Failure: x is not greater than 1, x=0.2. Wrong value: 0.2."
231231
validate_is_greater_than_2('val',2)
232232
with pytest.raises(ValidationError) as exc_info:
233233
validate_is_greater_than_2('val',0.2)
@@ -294,7 +294,7 @@ def gt_0(x):
294294
raise ValueError('x is not greater than 0, x={}'.format(x))
295295
def gt_1(x):
296296
if x < 1:
297-
raise Failure('x is not greater than 1, x={}'.format(x))
297+
raise Failure(x, 'x is not greater than 1, x={}'.format(x))
298298

299299
# (not recommended) relying on assert, only valid in 'debug' mode
300300
def gt_2(x):
@@ -346,7 +346,7 @@ def gt_ex1(x):
346346
if x >= 1:
347347
return True
348348
else:
349-
raise Failure('x >= 1 does not hold for x={}'.format(x))
349+
raise Failure(x, 'x >= 1 does not hold for x={}'.format(x))
350350

351351
def gt_assert2(x):
352352
"""(not recommended) relying on assert, only valid in 'debug' mode"""

valid8/tests/test_entry_points_annotations.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ def test_validate_custom_validators_with_exception():
287287
def gt_ex1(x):
288288
""" A validator raising a custom exception in case of failure """
289289
if not x >= 1:
290-
raise Failure('x >= 1 does not hold for x={val}'.format(val=x))
290+
raise Failure(x, 'x >= 1 does not hold for x={val}'.format(val=x))
291291

292292
def is_mod(ref):
293293
""" A validator generator, with parameters and which raises a custom exception """
294294
def is_mod(x):
295295
if x % ref != 0:
296-
raise Failure('x % {ref} == 0 does not hold for x={val}'.format(ref=ref, val=x))
296+
raise Failure(x, 'x % {ref} == 0 does not hold for x={val}'.format(ref=ref, val=x))
297297
return is_mod
298298

299299
@validate_io(a=[gt_ex1, lt(12), is_mod(5)])
@@ -311,7 +311,7 @@ def myfunc(a):
311311
"Validation function [and(gt_ex1, lesser_than_12, is_mod)] raised " \
312312
"AtLeastOneFailed: At least one validation function failed validation for value [0]. " \
313313
"Successes: ['lesser_than_12', 'is_mod'] / " \
314-
"Failures: {'gt_ex1': 'Failure: Wrong value: [x >= 1 does not hold for x=0]'}."
314+
"Failures: {'gt_ex1': 'Failure: x >= 1 does not hold for x=0. Wrong value: 0'}."
315315

316316
with pytest.raises(InputValidationError) as exc_info:
317317
print(2)
@@ -321,7 +321,7 @@ def myfunc(a):
321321
"Validation function [and(gt_ex1, lesser_than_12, is_mod)] raised " \
322322
"AtLeastOneFailed: At least one validation function failed validation for value [3]. " \
323323
"Successes: ['gt_ex1', 'lesser_than_12'] / " \
324-
"Failures: {'is_mod': 'Failure: Wrong value: [x % 5 == 0 does not hold for x=3]'}."
324+
"Failures: {'is_mod': 'Failure: x % 5 == 0 does not hold for x=3. Wrong value: 3'}."
325325

326326
with pytest.raises(InputValidationError) as exc_info:
327327
print(3)
@@ -331,7 +331,7 @@ def myfunc(a):
331331
"Validation function [and(gt_ex1, lesser_than_12, is_mod)] raised " \
332332
"AtLeastOneFailed: At least one validation function failed validation for value [15]. " \
333333
"Successes: ['gt_ex1', 'is_mod'] / " \
334-
"Failures: {'lesser_than_12': 'TooBig: x <= 12 does not hold for x=15. Wrong value: [15]'}."
334+
"Failures: {'lesser_than_12': 'TooBig: x <= 12 does not hold for x=15. Wrong value: 15'}."
335335

336336

337337
def test_validate_mini_lambda():

valid8/tests/test_entry_points_inline.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_validate_():
2525
validate('surface', surf, instance_of=int, min_value=0)
2626
e = exc_info.value
2727
assert str(e) == "Error validating [surface=-1]. " \
28-
"TooSmall: x >= 0 does not hold for x=-1. Wrong value: [-1]."
28+
"TooSmall: x >= 0 does not hold for x=-1. Wrong value: -1."
2929

3030
# error wrong type
3131
surf = 1j
@@ -34,7 +34,7 @@ def test_validate_():
3434
validate('surface', surf, instance_of=(int, ), min_value=0)
3535
e = exc_info.value
3636
assert str(e) == "Error validating [surface=1j]. " \
37-
"HasWrongType: Value should be an instance of %s. Wrong value: [1j]." % repr(int)
37+
"HasWrongType: Value should be an instance of %s. Wrong value: 1j." % repr(int)
3838

3939

4040
def test_readme_usage_validate__customization():
@@ -48,7 +48,7 @@ def test_readme_usage_validate__customization():
4848
help_msg="Surface should be a positive integer")
4949
e = exc_info.value
5050
assert str(e) == "Surface should be a positive integer. Error validating [surface=1j]. " \
51-
"HasWrongType: Value should be an instance of %s. Wrong value: [1j]." % repr(int)
51+
"HasWrongType: Value should be an instance of %s. Wrong value: 1j." % repr(int)
5252

5353
# (B) custom error types (recommended to provide unique applicative errors)
5454
class InvalidSurface(ValidationError):
@@ -69,7 +69,7 @@ class InvalidSurface(ValidationError):
6969
e = exc_info.value
7070
assert isinstance(e, InvalidSurface)
7171
assert str(e) == "Surface should be > 0, found 1j. Error validating [surface=1j]. " \
72-
"HasWrongType: Value should be an instance of %s. Wrong value: [1j]." % repr(int)
72+
"HasWrongType: Value should be an instance of %s. Wrong value: 1j." % repr(int)
7373

7474

7575
def test_validator_context_manager():

valid8/validation_lib/collections.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ def on_each_(*validation_functions_collection):
374374
def on_each_val(x # type: Tuple
375375
):
376376
if len(validation_function_funcs) != len(x):
377-
raise Failure('on_each_: x does not have the same number of elements than validation_functions_collection.')
377+
raise Failure(x, 'on_each_: x does not have the same number of elements than '
378+
'`validation_functions_collection`.')
378379
else:
379380
# apply each validation_function on the input with the same position in the collection
380381
idx = -1

0 commit comments

Comments
 (0)