MCPcopy Create free account
hub / github.com/dabeaz-course/python-mastery / wrapper

Function wrapper

Solutions/7_3/validate.py:74–96  ·  view source on GitHub ↗
(*args, **kwargs)

Source from the content-addressed store, hash-verified

72
73 @wraps(func)
74 def wrapper(*args, **kwargs):
75 bound = sig.bind(*args, **kwargs)
76 errors = []
77
78 # Enforce argument checks
79 for name, validator in annotations.items():
80 try:
81 validator.check(bound.arguments[name])
82 except Exception as e:
83 errors.append(f' {name}: {e}')
84
85 if errors:
86 raise TypeError('Bad Arguments\n' + '\n'.join(errors))
87
88 result = func(*args, **kwargs)
89
90 # Enforce return check (if any)
91 if retcheck:
92 try:
93 retcheck.check(result)
94 except Exception as e:
95 raise TypeError(f'Bad return: {e}') from None
96 return result
97
98 return wrapper
99

Callers

nothing calls this directly

Calls 2

appendMethod · 0.80
checkMethod · 0.45

Tested by

no test coverage detected