@@ -99,23 +99,48 @@ def get_callable_names(validation_callables # type: Iterable[ValidationCallable
9999SUCCESS_CONDITIONS = 'in {None, True}' # was used in some error messages
100100
101101
102- def result_is_success (validation_result # type: Any
103- ):
104- # type: (...) -> bool
105- """
106- Helper function to check if some results returned by a validation function mean success or failure.
102+ try :
103+ import numpy as np
104+ except ImportError :
105+ NP_TRUE = None # not available - use None as it is already a success condition
107106
108- The result should be True or None for a validation to be considered valid. Note that this is
109- quite different from the standard python truth value test (where None is equivalent to False), but it seems
110- more adapted to an intuitive usage, where a function that returns silently without any output means a
111- successful validation.
107+ def result_is_success (validation_result # type: Any
108+ ):
109+ # type: (...) -> bool
110+ """
111+ Helper function to check if some results returned by a validation function mean success or failure.
112112
113- :param validation_result:
114- :return:
115- """
116- # WARNING: if you change this definition, do not forget to do a search on all occurences of `result_is_success` in
117- # the code base, and replace all inlined versions accordingly
118- return (validation_result is None ) or (validation_result is True )
113+ The result should be True or None for a validation to be considered valid. Note that this is
114+ quite different from the standard python truth value test (where None is equivalent to False), but it seems
115+ more adapted to an intuitive usage, where a function that returns silently without any output means a
116+ successful validation.
117+
118+ :param validation_result:
119+ :return:
120+ """
121+ # WARNING: if you change this definition, do not forget to do a search on all occurences of `result_is_success`
122+ # in the code base, and replace all inlined versions accordingly
123+ return (validation_result is None ) or (validation_result is True )
124+ else :
125+ NP_TRUE = np .bool_ (True )
126+
127+ def result_is_success (validation_result # type: Any
128+ ):
129+ # type: (...) -> bool
130+ """
131+ Helper function to check if some results returned by a validation function mean success or failure.
132+
133+ The result should be True or None for a validation to be considered valid. Note that this is
134+ quite different from the standard python truth value test (where None is equivalent to False), but it seems
135+ more adapted to an intuitive usage, where a function that returns silently without any output means a
136+ successful validation.
137+
138+ :param validation_result:
139+ :return:
140+ """
141+ # WARNING: if you change this definition, do not forget to do a search on all occurences of `result_is_success`
142+ # in the code base, and replace all inlined versions accordingly
143+ return (validation_result is None ) or (validation_result is True ) or (validation_result is NP_TRUE )
119144
120145
121146def is_error_of_type (exc , ref_type ):
@@ -546,7 +571,7 @@ def raiser(x, **ctx):
546571 # perform validation
547572 res = call_it (x , ** ctx )
548573 # if not result_is_success(res): <= DO NOT REMOVE THIS COMMENT
549- success = (res is None ) or (res is True )
574+ success = (res is None ) or (res is True ) or ( res is NP_TRUE )
550575
551576 except ValidationFailure as f :
552577 # failures should be raised "as is"
0 commit comments