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

Function wrapper

Solutions/7_2/validate.py:75–97  ·  view source on GitHub ↗
(*args, **kwargs)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

appendMethod · 0.80
checkMethod · 0.45

Tested by

no test coverage detected