def assert_type(name, obj, types, none_ok=True):
 if (obj is None):
  if none_ok:
   return True
  else:
   raise AssertionError(('%s may not be None' % name))
 if (not isinstance(types, (tuple, list))):
  types = [types]
 for cls in types:
  if isinstance(obj, cls):
   return True
 allowed_types = '|'.join(map((lambda x: str(x)), types))
 stack = traceback.extract_stack()
 stack_msg = ('Function call %s() in %s:%d' % (stack[(-2)][2], stack[(-3)][0], stack[(-3)][1]))
 type_msg = ('%s must be instance of %s (but is %s)' % (name, allowed_types, str(type(obj))))
 raise AssertionError(((stack_msg + ': ') + type_msg))

def assert_type ( name , obj , types , none_ok = True ) : if ( obj is None ) : if none_ok : return True else : raise AssertionError ( ( '%s may not be None' % name ) ) if ( not isinstance ( types , ( tuple , list ) ) ) : types = [ types ] for cls in types : if isinstance ( obj , cls ) : return True allowed_types = '|' . join ( map ( ( lambda x : str ( x ) ) , types ) ) stack = traceback . extract_stack ( ) stack_msg = ( 'Function call %s() in %s:%d' % ( stack [ ( - 2 ) ] [ 2 ] , stack [ ( - 3 ) ] [ 0 ] , stack [ ( - 3 ) ] [ 1 ] ) ) type_msg = ( '%s must be instance of %s (but is %s)' % ( name , allowed_types , str ( type ( obj ) ) ) ) raise AssertionError ( ( ( stack_msg + ': ' ) + type_msg ) )
assert that a parameter is of a given type .