Skip to content

Commit 1f36744

Browse files
author
Sylvain MARIE
committed
Improved composition error messages: the failure type does not appear by default and sentences are smaller.
1 parent 0b171e3 commit 1f36744

6 files changed

Lines changed: 23 additions & 18 deletions

File tree

valid8/composition.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def __init__(self,
6262
if cause is not None:
6363
self.__cause__ = cause
6464

65+
def get_str_for_errors(self):
66+
"""The method called by `ValidationError` and self.get_details() in case of wrapped failure"""
67+
# overridden so that the type is not displayed
68+
return self.to_str(with_type=False, compact_mode=False)
69+
6570
def get_details(self, compact_mode=False):
6671
""" Overrides the base method in order to give details on the various successes and failures """
6772

@@ -125,7 +130,7 @@ class AtLeastOneFailed(CompositionFailure):
125130

126131
def get_what(self):
127132
# type: (...) -> str
128-
return 'At least one validation function failed validation'
133+
return 'At least one validation function failed'
129134

130135

131136
def and_(*validation_func # type: ValidationFuncs
@@ -229,7 +234,7 @@ class AllValidatorsFailed(CompositionFailure):
229234

230235
def get_what(self):
231236
# type: (...) -> str
232-
return 'No validation function succeeded validation'
237+
return 'All validation functions failed'
233238

234239

235240
def or_(*validation_func # type: ValidationFuncs
@@ -279,7 +284,7 @@ class XorTooManySuccess(CompositionFailure):
279284

280285
def get_what(self):
281286
# type: (...) -> str
282-
return 'Too many validation functions (more than 1) succeeded validation'
287+
return 'Too many validation functions (more than 1) succeeded'
283288

284289

285290
def xor_(*validation_func # type: ValidationFuncs

valid8/tests/readme/test_readme_index.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def build_house(name, surface=None):
185185
e = exc_info.value
186186
assert str(e) == "name should be a non-empty string. " \
187187
"Error validating input [name=''] for function [build_house]. " \
188-
"AtLeastOneFailed: At least one validation function failed validation for value <''>. " \
188+
"At least one validation function failed for value <''>. " \
189189
"Successes: [\"instance_of_%s\"] / Failures: {'len(s) > 0': 'Returned False.'}." \
190190
"" % (repr(str), )
191191

@@ -353,7 +353,7 @@ def test_readme_index_usage_composition():
353353
assert_valid('surface', surf, (x >= 0) & (x < 10000), is_multiple_of(100))
354354
e = exc_info.value
355355
assert str(e) == "Error validating [surface=-1]. " \
356-
"AtLeastOneFailed: At least one validation function failed validation for value <-1>. " \
356+
"At least one validation function failed for value <-1>. " \
357357
"Successes: [] / Failures: {'(x >= 0) & (x < 10000)': 'Returned False.', " \
358358
"'is_multiple_of_100': 'IsNotMultipleOf: Value should be a multiple of 100.'}."
359359

@@ -368,7 +368,7 @@ class InvalidSurface(ValidationError):
368368
e = exc_info.value
369369
assert str(e) == "Surface should be between 0 and 10000 and be a multiple of 100, found -1. " \
370370
"Error validating [surface=-1]. " \
371-
"AtLeastOneFailed: At least one validation function failed validation for value <-1>. " \
371+
"At least one validation function failed for value <-1>. " \
372372
"Successes: [] / Failures: {" \
373373
"'(x >= 0) & (x < 10000)': 'Returned False.', " \
374374
"'is_multiple_of_100': 'IsNotMultipleOf: Value should be a multiple of 100.'}."
@@ -380,7 +380,7 @@ class InvalidSurface(ValidationError):
380380
(is_multiple_of(100), 'Surface should be a multiple of 100'))
381381
e = exc_info.value
382382
assert str(e) == "Error validating [surface=-1]. " \
383-
"AtLeastOneFailed: At least one validation function failed validation for value <-1>. " \
383+
"At least one validation function failed for value <-1>. " \
384384
"Successes: [] / Failures: {" \
385385
"'(x >= 0) & (x < 10000)': 'InvalidValue: Surface should be between 0 and 10000. " \
386386
"Returned False.', " \
@@ -400,7 +400,7 @@ class InvalidSurface(ValidationError):
400400
e = exc_info.value
401401
assert str(e) == "Surface should be between 0 and 10000 and be a multiple of 100, found -1. " \
402402
"Error validating [surface=-1]. " \
403-
"AtLeastOneFailed: At least one validation function failed validation for value <-1>. " \
403+
"At least one validation function failed for value <-1>. " \
404404
"Successes: [] / Failures: {" \
405405
"'(x >= 0) & (x < 10000)': 'InvalidValue: Surface should be between 0 and 10000. " \
406406
"Returned False.', " \

valid8/tests/readme/test_readme_syntax.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class NotFinite(ValidationFailure):
5151
(i > 0, 'x should be strictly positive')])
5252

5353
assert str(exc_info.value) == "Error validating [x=-1]. " \
54-
"AtLeastOneFailed: At least one validation function failed validation for value " \
54+
"At least one validation function failed for value " \
5555
"<-1>. Successes: ['isfinite'] / Failures: " \
5656
"{'is_even': 'IsNotEven: Value should be even.', " \
5757
"'i > 0': 'InvalidValue: x should be strictly positive. Returned False.'}."
@@ -79,7 +79,7 @@ class NotFinite(ValidationFailure):
7979

8080
# NOTE:
8181
assert str(exc_info.value) == "Error validating [x=-1]. " \
82-
"AtLeastOneFailed: At least one validation function failed validation for value " \
82+
"At least one validation function failed for value " \
8383
"<-1>. Successes: ['isfinite'] / Failures: " \
8484
"{'is_even': 'InvalidValue: x should be even. IsNotEven: Value should be even.', " \
8585
"'i > 0': 'InvalidValue: x should be strictly positive. Returned False.'}."

valid8/tests/readme/test_readme_usage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def hello(age):
5252
hello(152)
5353
e = exc_info.value
5454
assert str(e) == "Error validating [age=152]. " \
55-
"AtLeastOneFailed: At least one validation function failed validation for value <152>. " \
55+
"At least one validation function failed for value <152>. " \
5656
"Successes: ['isfinite'] / Failures: {" \
5757
"'between_0_and_150': 'NotInRange: 0 <= x <= 150 does not hold for x=152.'}."
5858

@@ -67,7 +67,7 @@ def hello(age):
6767
hello(12.5)
6868
e = exc_info.value
6969
assert str(e) == "Error validating [age=12.5]. " \
70-
"AtLeastOneFailed: At least one validation function failed validation for value <12.5>. " \
70+
"At least one validation function failed for value <12.5>. " \
7171
"Successes: ['isfinite', 'between_0_and_150'] / Failures: {'int(x) == x': 'Returned False.'}."
7272

7373

valid8/tests/test_entry_points_annotations.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def myfunc(a, b):
264264
myfunc(4, 21) # InputValidationError: a is not a multiple of 3
265265
e = exc_info.value
266266
assert str(e) == "Error validating input [a=4] for function [myfunc]. " \
267-
"AtLeastOneFailed: At least one validation function failed validation for value <4>. " \
267+
"At least one validation function failed for value <4>. " \
268268
"Successes: ['gt_assert2'] / Failures: {'is_mod_3': 'Returned False.'}."
269269

270270
with pytest.raises(InputValidationError) as exc_info:
@@ -277,7 +277,7 @@ def myfunc(a, b):
277277
myfunc(1, 0) # InputValidationError caused by AssertionError: a is not >= 2
278278
e = exc_info.value
279279
assert str(e) == "Error validating input [a=1] for function [myfunc]. " \
280-
"AtLeastOneFailed: At least one validation function failed validation for value <1>. " \
280+
"At least one validation function failed for value <1>. " \
281281
"Successes: [] / " \
282282
"Failures: {'gt_assert2': 'AssertionError: assert 1 >= 2', 'is_mod_3': 'Returned False.'}."
283283

@@ -309,7 +309,7 @@ def myfunc(a):
309309
myfunc(0) # InputValidationError: a >= 1 does not hold
310310
e = exc_info.value
311311
assert str(e) == "Error validating input [a=0] for function [myfunc]. " \
312-
"AtLeastOneFailed: At least one validation function failed validation for value <0>. " \
312+
"At least one validation function failed for value <0>. " \
313313
"Successes: ['lesser_than_12', 'is_mod'] / " \
314314
"Failures: {'gt_ex1': 'ValidationFailure: x >= 1 does not hold for x=0.'}."
315315

@@ -318,7 +318,7 @@ def myfunc(a):
318318
myfunc(3) # InputValidationError: a % 5 == 0 does not hold
319319
e = exc_info.value
320320
assert str(e) == "Error validating input [a=3] for function [myfunc]. " \
321-
"AtLeastOneFailed: At least one validation function failed validation for value <3>. " \
321+
"At least one validation function failed for value <3>. " \
322322
"Successes: ['gt_ex1', 'lesser_than_12'] / " \
323323
"Failures: {'is_mod': 'ValidationFailure: x % 5 == 0 does not hold for x=3.'}."
324324

@@ -327,7 +327,7 @@ def myfunc(a):
327327
myfunc(15) # InputValidationError: a < 12 does not hold
328328
e = exc_info.value
329329
assert str(e) == "Error validating input [a=15] for function [myfunc]. " \
330-
"AtLeastOneFailed: At least one validation function failed validation for value <15>. " \
330+
"At least one validation function failed for value <15>. " \
331331
"Successes: ['gt_ex1', 'is_mod'] / " \
332332
"Failures: {'lesser_than_12': 'TooBig: x <= 12 does not hold for x=15.'}."
333333

valid8/validation_lib/collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def on_all_val(x):
307307
# if not result_is_success(res): <= DO NOT REMOVE THIS COMMENT
308308
if (res is not None) and (res is not True):
309309
# one element of x was not valid > raise
310-
# raise ValidationFailure('on_all_(' + str(validation_func) + '): failed validation for input '
310+
# raise ValidationFailure('on_all_(' + str(validation_func) + '): failed for input '
311311
# 'element [' + str(idx) + ']: ' + str(x_elt))
312312
raise InvalidItemInSequence(wrong_value=x_elt, validation_func=validation_function_func,
313313
validation_outcome=res)

0 commit comments

Comments
 (0)