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

Class ValidatedFunction

Solutions/6_5/validate.py:57–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55from inspect import signature
56
57class ValidatedFunction:
58 def __init__(self, func):
59 self.func = func
60 self.signature = signature(func)
61 self.annotations = dict(func.__annotations__)
62 self.retcheck = self.annotations.pop('return', None)
63
64 def __call__(self, *args, **kwargs):
65 bound = self.signature.bind(*args, **kwargs)
66
67 for name, val in self.annotations.items():
68 val.check(bound.arguments[name])
69
70 result = self.func(*args, **kwargs)
71
72 if self.retcheck:
73 self.retcheck.check(result)
74
75 return result
76
77# Examples
78if __name__ == '__main__':

Callers 2

validate.pyFile · 0.85
StockClass · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected