<s> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT>' <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> import inspect <EOL> import logging <EOL> import traceback <EOL> try : <EOL> from mock import Mock <EOL> except : <EOL> Mock = None <EOL> try : <EOL> range_type = xrange <EOL> except NameError : <EOL> range_type = range <EOL> try : <EOL> string_type = basestring <EOL> except NameError : <EOL> string_type = str <EOL> _decorator_enabled = True <EOL> _enabled = False <EOL> _logger = logging . getLogger ( __name__ ) <EOL> _loglevel = None <EOL> _exception = False <EOL> def setup_typecheck ( enabled = True , exception = TypeError , loglevel = None ) : <EOL> """<STR_LIT>""" <EOL> global _decorator_enabled , _enabled , _loglevel , _exception <EOL> _enabled = _decorator_enabled = enabled <EOL> _exception = exception <EOL> _loglevel = loglevel <EOL> def _type_error ( msg , stack = None ) : <EOL> if not _enabled : <EOL> return <EOL> if _loglevel : <EOL> if not stack : <EOL> stack = traceback . extract_stack ( ) [ - <NUM_LIT:4> ] <EOL> path , line , in_func , instr = stack <EOL> if instr : <EOL> instr = '<STR_LIT>' + instr <EOL> log_msg = '<STR_LIT>' % ( <EOL> path , line , in_func , msg , instr ) <EOL> _logger . log ( _loglevel , log_msg ) <EOL> if _exception : <EOL> raise _exception ( msg ) <EOL> class Union ( object ) : <EOL> __slots__ = ( '<STR_LIT>' , ) <EOL> def __init__ ( self , * types ) : <EOL> self . types = types <EOL> def __iter__ ( self ) : <EOL> return iter ( self . types ) <EOL> def Nullable ( t ) : <EOL> return Union ( t , type ( None ) ) <EOL> def _constraint_to_string ( t ) : <EOL> if isinstance ( t , type ) : <EOL> return t . __name__ <EOL> if isinstance ( t , string_type ) : <EOL> return t <EOL> elif isinstance ( t , list ) and len ( t ) == <NUM_LIT:1> : <EOL> return '<STR_LIT>' % _constraint_to_string ( t [ <NUM_LIT:0> ] ) <EOL> elif isinstance ( t , tuple ) : <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( _constraint_to_string ( x ) for x in t ) <EOL> elif isinstance ( t , dict ) and len ( t ) == <NUM_LIT:1> : <EOL> k , v = list ( t . items ( ) ) [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' % ( _constraint_to_string ( k ) , _constraint_to_string ( v ) ) <EOL> elif isinstance ( t , set ) and len ( t ) == <NUM_LIT:1> : <EOL> return '<STR_LIT>' % _constraint_to_string ( list ( t ) [ <NUM_LIT:0> ] ) <EOL> elif isinstance ( t , Union ) : <EOL> return '<STR_LIT>' % ( '<STR_LIT:U+002CU+0020>' . join ( _constraint_to_string ( x ) for x in t ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def _check_constraint_validity ( t ) : <EOL> if isinstance ( t , type ) : <EOL> return True <EOL> elif isinstance ( t , string_type ) : <EOL> return True <EOL> elif isinstance ( t , list ) and len ( t ) == <NUM_LIT:1> : <EOL> return _check_constraint_validity ( t [ <NUM_LIT:0> ] ) <EOL> elif isinstance ( t , tuple ) : <EOL> return all ( _check_constraint_validity ( x ) for x in t ) <EOL> elif isinstance ( t , dict ) and len ( t ) == <NUM_LIT:1> : <EOL> k , v = list ( t . items ( ) ) [ <NUM_LIT:0> ] <EOL> return _check_constraint_validity ( k ) and _check_constraint_validity ( v ) <EOL> elif isinstance ( t , set ) and len ( t ) == <NUM_LIT:1> : <EOL> return _check_constraint_validity ( list ( t ) [ <NUM_LIT:0> ] ) <EOL> elif isinstance ( t , Union ) : <EOL> return all ( _check_constraint_validity ( x ) for x in t ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def _verify_type_constraint ( v , t ) : <EOL> if Mock and isinstance ( v , Mock ) : <EOL> return True <EOL> if t is range_type and hasattr ( v , '<STR_LIT>' ) and callable ( v . __iter__ ) : <EOL> return True <EOL> elif isinstance ( t , type ) : <EOL> return isinstance ( v , t ) <EOL> elif isinstance ( t , string_type ) and type ( v ) . __name__ == t : <EOL> return True <EOL> elif isinstance ( t , list ) and isinstance ( v , list ) : <EOL> return all ( _verify_type_constraint ( vx , t [ <NUM_LIT:0> ] ) for vx in v ) <EOL> elif isinstance ( t , tuple ) and isinstance ( v , tuple ) and len ( t ) == len ( v ) : <EOL> return all ( _verify_type_constraint ( vx , tx ) for vx , tx in zip ( v , t ) ) <EOL> elif isinstance ( t , dict ) and isinstance ( v , dict ) : <EOL> tk , tv = list ( t . items ( ) ) [ <NUM_LIT:0> ] <EOL> return all ( _verify_type_constraint ( vk , tk ) and <EOL> _verify_type_constraint ( vv , tv ) for vk , vv in v . items ( ) ) <EOL> elif isinstance ( t , set ) and isinstance ( v , set ) : <EOL> tx = list ( t ) [ <NUM_LIT:0> ] <EOL> return all ( _verify_type_constraint ( vx , tx ) for vx in v ) <EOL> elif isinstance ( t , Union ) : <EOL> return any ( _verify_type_constraint ( v , tx ) for tx in t ) <EOL> else : <EOL> return False <EOL> def returns ( return_type ) : <EOL> """<STR_LIT>""" <EOL> _check_constraint_validity ( return_type ) <EOL> def deco ( fn ) : <EOL> if not _decorator_enabled : <EOL> return fn <EOL> if not hasattr ( fn , '<STR_LIT>' ) : <EOL> if hasattr ( fn , '<STR_LIT>' ) : <EOL> fc = fn . __code__ <EOL> else : <EOL> fc = fn . func_code <EOL> fn . __def_site__ = ( fc . co_filename , fc . co_firstlineno , fn . __name__ , <EOL> '<STR_LIT>' ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> retval = fn ( * args , ** kwargs ) <EOL> if _enabled : <EOL> if not _verify_type_constraint ( retval , return_type ) : <EOL> if retval is None and return_type is not type ( None ) : <EOL> _type_error ( "<STR_LIT>" , <EOL> stack = fn . __def_site__ ) <EOL> elif retval is not None and return_type is type ( None ) : <EOL> _type_error ( "<STR_LIT>" , <EOL> stack = fn . __def_site__ ) <EOL> else : <EOL> _type_error ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( repr ( retval ) , <EOL> _constraint_to_string ( return_type ) ) , <EOL> stack = fn . __def_site__ ) <EOL> return retval <EOL> wrapper . __name__ = fn . __name__ <EOL> wrapper . __doc__ = fn . __doc__ <EOL> wrapper . __module__ = fn . __module__ <EOL> wrapper . __return_type__ = return_type <EOL> return wrapper <EOL> return deco <EOL> void = returns ( type ( None ) ) <EOL> void . __doc__ = """<STR_LIT>""" <EOL> def params ( ** types ) : <EOL> """<STR_LIT>""" <EOL> for arg_name , arg_type in types . items ( ) : <EOL> _check_constraint_validity ( arg_type ) <EOL> def deco ( fn ) : <EOL> if not _decorator_enabled : <EOL> return fn <EOL> if hasattr ( fn , '<STR_LIT>' ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if hasattr ( fn , '<STR_LIT>' ) : <EOL> fc = fn . __code__ <EOL> else : <EOL> fc = fn . func_code <EOL> if not hasattr ( fn , '<STR_LIT>' ) : <EOL> fn . __def_site__ = ( fc . co_filename , fc . co_firstlineno , fn . __name__ , <EOL> '<STR_LIT>' ) <EOL> if hasattr ( inspect , '<STR_LIT>' ) : <EOL> arg_names , va_args , va_kwargs , _ , _ , _ , _ = inspect . getfullargspec ( fn ) <EOL> else : <EOL> arg_names , va_args , va_kwargs , _ = inspect . getargspec ( fn ) <EOL> if any ( arg not in arg_names for arg in types . keys ( ) ) or any ( arg not in types for arg in arg_names ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> if _enabled : <EOL> for arg , name in zip ( args , arg_names ) : <EOL> if not _verify_type_constraint ( arg , types [ name ] ) : <EOL> _type_error ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , repr ( arg ) , <EOL> _constraint_to_string ( types [ name ] ) ) ) <EOL> for k , v in kwargs . items ( ) : <EOL> if k not in types : <EOL> if not va_kwargs : <EOL> _type_error ( "<STR_LIT>" <EOL> "<STR_LIT>" % k ) <EOL> elif not _verify_type_constraint ( v , types [ k ] ) : <EOL> _type_error ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( k , repr ( v ) , <EOL> _constraint_to_string ( types [ k ] ) ) ) <EOL> return fn ( * args , ** kwargs ) <EOL> wrapper . __name__ = fn . __name__ <EOL> wrapper . __doc__ = fn . __doc__ <EOL> wrapper . __module__ = fn . __module__ <EOL> return wrapper <EOL> return deco <EOL> def typed ( fn ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( fn , '<STR_LIT>' ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return_type = fn . __annotations__ . get ( '<STR_LIT>' ) <EOL> param_types = fn . __annotations__ . copy ( ) <EOL> if return_type : <EOL> del param_types [ '<STR_LIT>' ] <EOL> if param_types : <EOL> fn = params ( ** param_types ) ( fn ) <EOL> if return_type : <EOL> fn = returns ( return_type ) ( fn ) <EOL> fn . __annotations__ = { } <EOL> return fn </s>
