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

Function wrapper

Solutions/7_4/validate.py:73–95  ·  view source on GitHub ↗
(*args, **kwargs)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

appendMethod · 0.80
checkMethod · 0.45

Tested by

no test coverage detected