def checked_f(*args:list, **kwds:dict): argspec = inspect.getfullargspec(f) try: check_args(f, args, argspec) check_kwds(f, kwds, argspec) except TypeDeclarationViolation as e: # It would be confusing to the user to see a big stack of our function calls # here in the stack trace when an error is detected, so if we're in 3.3 or higher # we supress the rest of our own stack trace by the "raise Exception from None" # technique. raise TypeDeclarationViolation(str(e)) from (None if sys.version >= '3.3' else e) # Since errors thrown here have to do with the actual implementation of the # checked function, f, we don't want to re-wrap any exceptions thrown # since they are actually caused by the user's code. rvalue = f(*args, **kwds) try: if isinstance(rvalue, GeneratorType): def checked_gen(): for value in rvalue: yield check_return(f, value, argspec) return checked_gen() else: return check_return(f, rvalue, argspec) except TypeDeclarationViolation as e: raise TypeDeclarationViolation(str(e)) from (None if sys.version >= '3.3' else e)
def checked_gen(): for value in rvalue: yield check_return(f, value, argspec)