11import sys
2+ from inspect import isclass
23from warnings import warn
34
45from linecache import getline
1213
1314try : # python 3.5+
1415 # noinspection PyUnresolvedReferences
15- from typing import Any , Union , Set , Iterable , Callable , Container
16+ from typing import Any , Union , Set , Iterable , Callable , Container , Tuple
1617 try : # python 3.5.3-
1718 from typing import Type
1819 except ImportError :
2223
2324
2425def assert_instance_of (value ,
25- allowed_types # type: Union[type, Iterable[type ]]
26+ allowed_types # type: Union[Type, Tuple[Type ]]
2627 ):
2728 """
2829 An inlined version of instance_of(var_types)(value) without 'return True': it does not return anything in case of
@@ -31,16 +32,10 @@ def assert_instance_of(value,
3132 Used in validate and validation/validator
3233
3334 :param value: the value to check
34- :param allowed_types: the type(s) to enforce. If a set of types is provided it is considered alternate types: one
35+ :param allowed_types: the type(s) to enforce. If a tuple of types is provided it is considered alternate types: one
3536 match is enough to succeed. If None, type will not be enforced
3637 :return:
3738 """
38- try :
39- # isinstance does not allow anything else than tuples when several are provided
40- allowed_types = tuple (allowed_types )
41- except TypeError :
42- pass
43-
4439 if not isinstance (value , allowed_types ):
4540 try :
4641 # more than 1 ?
@@ -57,7 +52,7 @@ def assert_instance_of(value,
5752
5853
5954def assert_subclass_of (typ ,
60- allowed_types # type: Union[type, Iterable[type ]]
55+ allowed_types # type: Union[Type, Tuple[Type ]]
6156 ):
6257 """
6358 An inlined version of subclass_of(var_types)(value) without 'return True': it does not return anything in case of
@@ -66,16 +61,10 @@ def assert_subclass_of(typ,
6661 Used in validate and validation/validator
6762
6863 :param typ: the type to check
69- :param allowed_types: the type(s) to enforce. If a set of types is provided it is considered alternate types:
64+ :param allowed_types: the type(s) to enforce. If a tuple of types is provided it is considered alternate types:
7065 one match is enough to succeed. If None, type will not be enforced
7166 :return:
7267 """
73- try :
74- # issubclass does not allow anything else than tuples when several are provided
75- allowed_types = tuple (allowed_types )
76- except TypeError :
77- pass
78-
7968 if not issubclass (typ , allowed_types ):
8069 try :
8170 # more than 1 ?
@@ -137,8 +126,8 @@ def validate(name, # type: str
137126 value , # type: Any
138127 enforce_not_none = True , # type: bool
139128 equals = None , # type: Any
140- instance_of = None , # type: Union[type, Iterable[type ]]
141- subclass_of = None , # type: Union[type, Iterable[type ]]
129+ instance_of = None , # type: Union[Type, Tuple[Type ]]
130+ subclass_of = None , # type: Union[Type, Tuple[Type ]]
142131 is_in = None , # type: Container
143132 subset_of = None , # type: Set
144133 contains = None , # type: Union[Any, Iterable]
@@ -172,9 +161,9 @@ def validate(name, # type: str
172161 :param value: the value to check
173162 :param enforce_not_none: boolean, default True. Whether to enforce that `value` is not None.
174163 :param equals: an optional value to enforce.
175- :param instance_of: optional type(s) to enforce. If a set of types is provided it is considered alternate types: one
164+ :param instance_of: optional type(s) to enforce. If a tuple of types is provided it is considered alternate types: one
176165 match is enough to succeed. If None, type will not be enforced
177- :param subclass_of: optional type(s) to enforce. If a set of types is provided it is considered alternate types: one
166+ :param subclass_of: optional type(s) to enforce. If a tuple of types is provided it is considered alternate types: one
178167 match is enough to succeed. If None, type will not be enforced
179168 :param is_in: an optional set of allowed values.
180169 :param subset_of: an optional superset for the variable
@@ -381,8 +370,8 @@ class validator(Validator):
381370 def __init__ (self ,
382371 name , # type: str
383372 value , # type: Any
384- instance_of = None , # type: Union[type, Iterable[type ]]
385- subclass_of = None , # type: Union[type, Iterable[type ]]
373+ instance_of = None , # type: Union[Type, Tuple[Type ]]
374+ subclass_of = None , # type: Union[Type, Tuple[Type ]]
386375 error_type = None , # type: Type[ValidationError]
387376 help_msg = None , # type: str
388377 ** kw_context_args ):
@@ -400,9 +389,9 @@ def __init__(self,
400389
401390 :param name: the name of the variable being validated
402391 :param value: the value being validated
403- :param instance_of: the type(s) to enforce. If a set of types is provided it is considered alternate types:
392+ :param instance_of: the type(s) to enforce. If a tuple of types is provided it is considered alternate types:
404393 one match is enough to succeed. If None, type will not be enforced
405- :param subclass_of: the type(s) to enforce. If a set of types is provided it is considered alternate types: one
394+ :param subclass_of: the type(s) to enforce. If a tuple of types is provided it is considered alternate types: one
406395 match is enough to succeed. If None, type will not be enforced
407396 :param error_type: a subclass of `ValidationError` to raise in case of validation failure. By default a
408397 `ValidationError` will be raised with the provided `help_msg`
0 commit comments