Skip to content

Commit c0c6630

Browse files
author
Sylvain MARIE
committed
WrappingFailure was renamed ValidationFailed for end-user readability
1 parent 182e0e4 commit c0c6630

7 files changed

Lines changed: 28 additions & 28 deletions

File tree

valid8/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# -- utils_typing
3434
'Boolean', 'is_pep484_nonable',
3535
# -- base
36-
'Failure', 'WrappingFailure', 'failure_raiser',
36+
'Failure', 'ValidationFailed', 'failure_raiser',
3737
# -- composition
3838
'CompositionFailure', 'AtLeastOneFailed', 'and_', 'DidNotFail', 'not_', 'AllValidatorsFailed', 'or_',
3939
'XorTooManySuccess', 'xor_', 'not_all', 'fail_on_none', 'skip_on_none',

valid8/base.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def __init__(self, wrong_value, condition, **kwargs):
275275
```
276276
277277
Note: if users wish to wrap an *existing* function (such as a lambda or mini lambda) with a Failure raiser, then
278-
they should subclass `WrappingFailure` instead of `Failure`. See `WrappingFailure` for details.
278+
they should subclass `ValidationFailed` instead of `Failure`. See `ValidationFailed` for details.
279279
"""
280280

281281
def __init__(self,
@@ -334,7 +334,7 @@ def get_details(self):
334334
return 'Wrong value: %s' % repr(self.wrong_value)
335335

336336

337-
class WrappingFailure(Failure):
337+
class ValidationFailed(Failure):
338338
"""
339339
Represents a Failure associated with a wrapped validation function. It contains additional details about which
340340
validation function it was, and what was the outcome.
@@ -344,7 +344,7 @@ class WrappingFailure(Failure):
344344
345345
* either use the syntax `(<wrapped_callable>, <help_msg>)` when defining validation functions, or explicitly call
346346
`failure_raiser(<wrapped_callable>, help_msg=<help_msg>)`. Both are equivalent and will create instances of
347-
`WrappingFailure` with the provided help message when the wrapped callable fails.
347+
`ValidationFailed` with the provided help message when the wrapped callable fails.
348348
349349
* or subclass it explicitly (recommended) and then use the syntax `(<callable>, <subclass>)` or
350350
`failure_raiser(<wrapped_callable>, failure_type=<subclass>)`. Both are equivalent and will create instances of
@@ -355,7 +355,7 @@ class WrappingFailure(Failure):
355355
providing details about the failure is to override the 'help_msg' field:
356356
357357
```python
358-
class SurfaceNotInValidRange(WrappingFailure):
358+
class SurfaceNotInValidRange(ValidationFailed):
359359
help_msg = 'Surface should be a positive number less than 10000 (square meter)'
360360
```
361361
@@ -370,7 +370,7 @@ def __init__(self,
370370
help_msg=None, # type: str
371371
**kw_context_args):
372372
"""
373-
Creates a WrappingFailure associated with failed validation of `wrong_value` by `wrapped_func`. Additional
373+
Creates a ValidationFailed associated with failed validation of `wrong_value` by `wrapped_func`. Additional
374374
details about the validation outcome (result or exception) can be provided in validation_outcome.
375375
376376
Contextual information may be provided as keyword arguments, and will be stored as fields in the created
@@ -396,7 +396,7 @@ def __init__(self,
396396
self.validation_outcome = validation_outcome
397397

398398
# call super constructor
399-
super(WrappingFailure, self).__init__(wrong_value=wrong_value, help_msg=help_msg, **kw_context_args)
399+
super(ValidationFailed, self).__init__(wrong_value=wrong_value, help_msg=help_msg, **kw_context_args)
400400

401401
# automatically set the exception as the cause, so that we can forget to "raise from"
402402
if isinstance(validation_outcome, Exception):
@@ -431,8 +431,8 @@ def get_context_for_help_msgs(self, context_dict):
431431

432432

433433
def failure_raiser(validation_callable, # type: ValidationCallableOrLambda
434-
failure_type=None, # type: Type[WrappingFailure]
435434
help_msg=None, # type: str
435+
failure_type=None, # type: Type[ValidationFailed]
436436
**kw_context_args):
437437
# type: (...) -> ValidationCallable
438438
"""
@@ -469,7 +469,7 @@ def raiser(x):
469469

470470
# if not result_is_success(res): <= DO NOT REMOVE THIS COMMENT
471471
if (res is not None) and (res is not True):
472-
typ = failure_type or WrappingFailure
472+
typ = failure_type or ValidationFailed
473473
exc = typ(wrapped_func=validation_callable, wrong_value=x, validation_outcome=res,
474474
help_msg=help_msg, **kw_context_args)
475475
raise exc
@@ -538,7 +538,7 @@ def _none_rejecter(validation_callable # type: ValidationCallable
538538
# type: (...) -> ValidationCallable
539539
"""
540540
Wraps the given validation callable to reject None values. When a None value is received by the wrapper,
541-
it is not passed to the validation_callable and instead this function will raise a WrappingFailure. When any other
541+
it is not passed to the validation_callable and instead this function will raise a ValidationFailed. When any other
542542
value is received the validation_callable is called as usual.
543543
544544
Important: mini-lambda expressions are NOT transformed into function. Indeed this function is internal only

valid8/common_syntax.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from itertools import chain
22
from sys import version_info
33

4-
from valid8.base import failure_raiser, WrappingFailure, as_function
4+
from valid8.base import failure_raiser, ValidationFailed, as_function
55

66
try: # python 3.5+
77
# noinspection PyUnresolvedReferences
@@ -18,14 +18,14 @@
1818
# 2. the syntax to optionally transform them into failure raisers by providing a tuple
1919
ValidationFuncDefinition = Union[ValidationCallableOrLambda,
2020
Tuple[ValidationCallableOrLambda, str],
21-
Tuple[ValidationCallableOrLambda, Type[WrappingFailure]],
22-
Tuple[ValidationCallableOrLambda, str, Type[WrappingFailure]]
21+
Tuple[ValidationCallableOrLambda, Type[ValidationFailed]],
22+
Tuple[ValidationCallableOrLambda, str, Type[ValidationFailed]]
2323
]
2424
"""Defines a checker from a base checker function together with optional error message and failure type
2525
(in which case a failure raiser is created to wrap that function)"""
2626

2727
# 3. the syntax to describe several validation functions at once
28-
VFDefinitionElement = Union[str, Type[WrappingFailure], ValidationCallableOrLambda]
28+
VFDefinitionElement = Union[str, Type[ValidationFailed], ValidationCallableOrLambda]
2929
"""This type represents one of the elements that can define a checker"""
3030

3131
OneOrSeveralVFDefinitions = Union[ValidationFuncDefinition,
@@ -79,7 +79,7 @@ def _make_validation_func_callable(vf_definition # type: ValidationFuncDefiniti
7979
If `vf_definition` is a tuple such as (<validation_func>, <err_msg>), (<validation_func>, <failure_type>),
8080
or (<validation_func>, <err_msg>, <failure_type>), a `failure_raiser` is created.
8181
82-
>>> class MyFailure(WrappingFailure):
82+
>>> class MyFailure(ValidationFailed):
8383
... pass
8484
>>> vf_with_details = _make_validation_func_callable((vf, 'blah', MyFailure))
8585
>>> vf_with_details('hello')
@@ -133,7 +133,7 @@ def _make_validation_func_callable(vf_definition # type: ValidationFuncDefiniti
133133
# noinspection PyBroadException
134134
try:
135135
# noinspection PyTypeChecker
136-
if issubclass(failure_type, WrappingFailure):
136+
if issubclass(failure_type, ValidationFailed):
137137
failure_type_ok = True
138138
except: # noqa: E722
139139
pass
@@ -181,7 +181,7 @@ def _make_validation_func_callables(*vf_definition # type: OneOrSeveralVFDefini
181181
>>> def is_minus_1(x): return x + 1 == 0
182182
183183
>>> # a custom failure we would like to be raised
184-
>>> class MyFailure(WrappingFailure):
184+
>>> class MyFailure(ValidationFailed):
185185
... pass
186186
187187
>>> # process both vf1 and v2, reusing vf1 'as is' and enriching vf2 with a custom failure type and error message

valid8/composition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from makefun import with_signature
66

7-
from valid8.base import Failure, WrappingFailure, result_is_success, get_callable_names, get_callable_name, _none_accepter, _none_rejecter
7+
from valid8.base import Failure, ValidationFailed, get_callable_names, get_callable_name, _none_accepter, _none_rejecter
88
from valid8.common_syntax import _make_validation_func_callables
99

1010

@@ -70,7 +70,7 @@ def get_details(self):
7070
for validator, failure in self.failures.items():
7171
name = get_callable_name(validator)
7272
if isinstance(failure, Exception):
73-
if isinstance(failure, WrappingFailure) or isinstance(failure, CompositionFailure):
73+
if isinstance(failure, ValidationFailed) or isinstance(failure, CompositionFailure):
7474
need_to_print_value = False
7575
failures_for_print[name] = '{exc_type}: {msg}'.format(exc_type=type(failure).__name__, msg=str(failure))
7676
else:
@@ -173,7 +173,7 @@ def and_v_(x):
173173
return and_v_
174174

175175

176-
class DidNotFail(WrappingFailure):
176+
class DidNotFail(ValidationFailed):
177177
""" Raised by the not_ operator when the inner validation function did not fail."""
178178
help_msg = '{wrapped_func} validated value {wrong_value} with success, therefore the not() is a failure'
179179

valid8/entry_points.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def __init__(self, var_value, condition, **kwargs):
174174
```
175175
176176
Note: if users wish to wrap an *existing* function (such as a lambda or mini lambda) with a Failure raiser, then
177-
they should subclass `WrappingFailure` instead of `Failure`. See `WrappingFailure` for details.
177+
they should subclass `ValidationFailed` instead of `Failure`. See `ValidationFailed` for details.
178178
"""
179179

180180
@classmethod

valid8/tests/readme/test_readme_index.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def test_readme_index_usage_customization():
295295
e = exc_info.value
296296
assert str(e) == 'Error validating [surface=-1]. ' \
297297
'Validation function [is_multiple_of_100] raised ' \
298-
'WrappingFailure: Surface should be a multiple of 100. ' \
298+
'ValidationFailed: Surface should be a multiple of 100. ' \
299299
'Function [is_multiple_of_100] raised ' \
300300
'[IsNotMultipleOf: Value should be a multiple of 100. Wrong value: -1].'
301301

@@ -377,9 +377,9 @@ class InvalidSurface(ValidationError):
377377
"Validation function [and((x >= 0) & (x < 10000), is_multiple_of_100)] raised " \
378378
"AtLeastOneFailed: At least one validation function failed validation. " \
379379
"Successes: [] / Failures: {" \
380-
"'(x >= 0) & (x < 10000)': 'WrappingFailure: Surface should be between 0 and 10000. " \
380+
"'(x >= 0) & (x < 10000)': 'ValidationFailed: Surface should be between 0 and 10000. " \
381381
"Function [(x >= 0) & (x < 10000)] returned [False] for value -1.', " \
382-
"'is_multiple_of_100': 'WrappingFailure: Surface should be a multiple of 100. " \
382+
"'is_multiple_of_100': 'ValidationFailed: Surface should be a multiple of 100. " \
383383
"Function [is_multiple_of_100] raised [IsNotMultipleOf: Value should be a multiple of 100. " \
384384
"Wrong value: -1].'}."
385385

@@ -399,8 +399,8 @@ class InvalidSurface(ValidationError):
399399
"Validation function [and((x >= 0) & (x < 10000), is_multiple_of_100)] raised " \
400400
"AtLeastOneFailed: At least one validation function failed validation. " \
401401
"Successes: [] / Failures: {" \
402-
"'(x >= 0) & (x < 10000)': 'WrappingFailure: Surface should be between 0 and 10000. " \
402+
"'(x >= 0) & (x < 10000)': 'ValidationFailed: Surface should be between 0 and 10000. " \
403403
"Function [(x >= 0) & (x < 10000)] returned [False] for value -1.', " \
404-
"'is_multiple_of_100': 'WrappingFailure: Surface should be a multiple of 100, found -1. " \
404+
"'is_multiple_of_100': 'ValidationFailed: Surface should be a multiple of 100, found -1. " \
405405
"Function [is_multiple_of_100] raised [IsNotMultipleOf: Value should be a multiple of 100. " \
406406
"Wrong value: -1].'}."

valid8/validation_lib/collections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
pass
66

77
from valid8.composition import and_
8-
from valid8.base import Failure, WrappingFailure, result_is_success, get_callable_name
8+
from valid8.base import Failure, ValidationFailed, get_callable_name
99

1010

1111
class Empty(Failure, ValueError):
@@ -270,7 +270,7 @@ def is_superset_of(x):
270270
return is_superset_of
271271

272272

273-
class InvalidItemInSequence(WrappingFailure, ValueError):
273+
class InvalidItemInSequence(ValidationFailed, ValueError):
274274
""" Custom Failure raised by on_all_ and on_each_ """
275275
help_msg = 'Provided sequence contains one value that is invalid.'
276276

0 commit comments

Comments
 (0)