Skip to content

Commit 5a1320c

Browse files
author
Sylvain MARIE
committed
Fixed doctests: skipping them in python 2 so that we can get rid of the IGNORE_EXCEPTION_DETAIL directive. Indeed we need to validate the contents of the exception
1 parent 255b5a3 commit 5a1320c

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ test=pytest
2020
[tool:pytest]
2121
norecursedirs=tests/helpers
2222
addopts = --verbose --doctest-modules --ignore-glob='**/_*.py' --ignore=_typing_inspect.py
23-
doctest_optionflags= NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL ELLIPSIS
23+
doctest_optionflags= NORMALIZE_WHITESPACE ELLIPSIS
2424
testpaths = valid8

valid8/base.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,35 @@ def failure_raiser(validation_callable, # type: ValidationCallableOrLambda
439439
Wraps the provided validation function so that in case of failure it raises the given `failure_type` or a
440440
`WrappingFailure` with the given help message.
441441
442-
mini-lambda functions are automatically transformed to functions.
442+
>>> import sys, pytest
443+
>>> if sys.version_info < (3, 0):
444+
... pytest.skip('doctest skipped in python 2 because exception namespace is different but details matter')
445+
446+
>>> def is_big(x): return x > 10
447+
>>> is_big_with_details = failure_raiser(is_big, help_msg="x should be big")
448+
>>> is_big_with_details(11)
449+
>>> is_big_with_details(2)
450+
Traceback (most recent call last):
451+
...
452+
valid8.base.ValidationFailed: x should be big. Function [is_big] returned [False] for value 2.
453+
>>> class MyTooSmall(ValidationFailed):
454+
... help_msg = "x should be bigger than 10. Found {wrong_value}"
455+
>>> is_big_with_details = failure_raiser(is_big, failure_type=MyTooSmall)
456+
>>> is_big_with_details(11)
457+
>>> is_big_with_details(2)
458+
Traceback (most recent call last):
459+
...
460+
valid8.base.MyTooSmall: x should be bigger than 10. Found 2. Function [is_big] returned [False] for value 2.
461+
462+
mini-lambda functions are automatically transformed to functions:
463+
464+
>>> from mini_lambda import x
465+
>>> is_small_with_details = failure_raiser(x < 3, help_msg="x should be smaller than 3")
466+
>>> is_small_with_details(2)
467+
>>> is_small_with_details(11)
468+
Traceback (most recent call last):
469+
...
470+
valid8.base.ValidationFailed: x should be smaller than 3. Function [x < 3] returned [False] for value 11.
443471
444472
:param validation_callable:
445473
:param failure_type: an optional subclass of `WrappingFailure` that should be raised in case of failure, instead of

valid8/common_syntax.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def _make_validation_func_callable(vf_definition # type: ValidationFuncDefiniti
7373
7474
If `vf_definition` is a single <validation_func> callable, it is returned directly (no wrapping)
7575
76+
>>> import sys, pytest
77+
>>> if sys.version_info < (3, 0):
78+
... pytest.skip('doctest skipped in python 2 because exception namespace is different but details matter')
79+
7680
>>> def vf(x): return x + 1 == 0
7781
>>> assert _make_validation_func_callable(vf) is vf
7882
@@ -85,7 +89,7 @@ def _make_validation_func_callable(vf_definition # type: ValidationFuncDefiniti
8589
>>> vf_with_details('hello')
8690
Traceback (most recent call last):
8791
...
88-
MyFailure: blah. Function [vf] raised [TypeError: can...
92+
valid8.common_syntax.MyFailure: blah. Function [vf] raised [TypeError: can...
8993
9094
Notes:
9195
@@ -176,6 +180,10 @@ def _make_validation_func_callables(*vf_definition # type: OneOrSeveralVFDefini
176180
and a tuple containing the results will be returned. See `_make_validation_func_callable` for details on the
177181
supported tuples to use.
178182
183+
>>> import sys, pytest
184+
>>> if sys.version_info < (3, 0):
185+
... pytest.skip('doctest skipped in python 2 because exception namespace is different but details matter')
186+
179187
>>> # two dummy validation callables
180188
>>> def is_big(x): return x > 10
181189
>>> def is_minus_1(x): return x + 1 == 0
@@ -191,7 +199,7 @@ def _make_validation_func_callables(*vf_definition # type: OneOrSeveralVFDefini
191199
>>> several_vfs[1]('hello')
192200
Traceback (most recent call last):
193201
...
194-
MyFailure: not minus 1!. Function [is_minus_1] raised [TypeError: can...
202+
valid8.common_syntax.MyFailure: not minus 1!. Function [is_minus_1] raised [TypeError: can...
195203
196204
If a single `vf_definition` is provided AND it is a non-tuple iterable (typically a list),
197205
`_make_validation_func_callables(vf_definition)` is equivalent to `_make_validation_func_callables(*vf_definition)`
@@ -209,7 +217,7 @@ def _make_validation_func_callables(*vf_definition # type: OneOrSeveralVFDefini
209217
>>> vfs[0](2)
210218
Traceback (most recent call last):
211219
...
212-
WrappingFailure: x should be big. Function [is_big] returned [False] for value 2.
220+
valid8.base.ValidationFailed: x should be big. Function [is_big] returned [False] for value 2.
213221
214222
:param vf_definition: the base validation function or list of base validation functions to use. A callable, a
215223
tuple(callable, help_msg_str), a tuple(callable, failure_type), tuple(callable, help_msg_str, failure_type)

0 commit comments

Comments
 (0)