1717 from valid8 .base import ValidationCallable
1818 # noinspection PyUnresolvedReferences
1919 from valid8 .common_syntax import ValidationFuncs
20+
2021 use_typing = sys .version_info > (3 , 0 )
2122except ImportError :
2223 use_typing = False
@@ -177,6 +178,9 @@ def __init__(self, var_value, condition, **kwargs):
177178 they should subclass `ValidationFailed` instead of `Failure`. See `ValidationFailed` for details.
178179 """
179180
181+ # We do not use slots otherwise `help_msg` cannot easily be overridden by a class attribute
182+ # __slots__ = 'validator', 'var_value', 'var_name', 'validation_outcome', 'append_details', 'context'
183+
180184 @classmethod
181185 def create_manually (cls ,
182186 validation_function_name , # type: str
@@ -187,6 +191,7 @@ def create_manually(cls,
187191 # append_details=True, # type: bool
188192 ** kw_context_args ):
189193 """
194+ TODO remove the method or find a real need.
190195 Creates an instance without using a Validator.
191196
192197 This method is not the primary way that errors are created - they should rather created by the validation entry
@@ -224,7 +229,7 @@ def __init__(self,
224229 append_details = True , # type: bool
225230 ** kw_context_args ):
226231 """
227- Creates a ValidationError associated with validation of `var_value` using `validator`. Additional details
232+ Creates a ` ValidationError` associated with validation of `var_value` using `validator`. Additional details
228233 about the `var_name` and `validation_outcome` (result or exception) can be provided. All of this
229234 information is stored in the exception object so as to be managed by a global error handler at application-level
230235 if needed (for example for internationalization purposes)
@@ -235,11 +240,12 @@ def __init__(self,
235240 :param validation_outcome: the result of the validation process (either a non-True non-None value, or an
236241 exception)
237242 :param help_msg: an optional help message specific to this validation error. If not provided, the class
238- attribute `help_msg` will be used. This behaviour may be redefined by subclasses by overriding `get_help_msg`
243+ attribute `help_msg` will be used. This behaviour may be redefined by subclasses by overriding
244+ `get_help_msg`
239245 :param append_details: a boolean indicating if a default message containing the value should be appended to the
240- string representation. Default is True
246+ string representation. Default is ` True`
241247 :param kw_context_args: optional context (results, other) to store in this failure and that will be also used
242- for help message formatting
248+ for help message formatting
243249 """
244250
245251 self .display_prefix_for_exc_outcomes = True
@@ -279,8 +285,9 @@ def __str__(self):
279285 return self .get_help_msg (** self .__dict__ )
280286 except HelpMsgFormattingException as f :
281287 return str (f )
288+
282289 except Exception as e :
283- return "Error while formatting help message: {}" . format ( e )
290+ return "Error while formatting help message: %s" % e
284291
285292 def __repr__ (self ):
286293 """ Overrides the default exception representation """
@@ -470,19 +477,20 @@ def my_validator(x):
470477 :param kw_context_args: optional contextual information to store in the exception, and that may be also used
471478 to format the help message
472479 """
480+ # pop without setting defaults since we want to check if values were actually provided
473481 error_type , help_msg , none_policy = pop_kwargs (kwargs , [('error_type' , None ),
474482 ('help_msg' , None ),
475483 ('none_policy' , None )], allow_others = True )
476484 # the rest of keyword arguments is used as context.
477485 kw_context_args = kwargs
478486
479487 if help_msg is None and error_type is None and len (kw_context_args ) > 0 :
480- raise ValueError ("Keyword context arguments have been provided but help_msg and error_type are not: {}"
488+ raise ValueError ("Keyword context arguments have been provided but help_msg and error_type have not: {}"
481489 "" .format (kw_context_args ))
482490
483491 self .none_policy = none_policy if none_policy is not None else NonePolicy .VALIDATE
484492
485- self .error_type = error_type or ValidationError
493+ self .error_type = error_type if error_type is not None else ValidationError
486494 if not issubclass (self .error_type , ValidationError ):
487495 raise ValueError ('error_type should be a subclass of ValidationError' )
488496
0 commit comments