def on(self, obj): """ Wraps specified object's methods in @expects/@returns decorators, according to those defined on interface's method. """ for method_name, method in self._interface.__dict__.iteritems(): if not isinstance(method, Method): continue if not method.is_checked(): continue obj_method = getattr(obj, method_name, None) if not obj_method: exc_tuple = (obj, method_name, self._interface.__name__) raise TypeError, "Object (%r) does have required method %s of interface %s" % exc_tuple # decorate with @returns if method._returns: returns_decorator = ReturnValueDecorator(method._returns) obj_method = returns_decorator(obj_method) # decorate with @expects if method._arguments: expects_decorator = ExpectedParametersDecorator() expects_decorator.arg_spec = method._arguments obj_method = expects_decorator(obj_method) setattr(obj, method_name, obj_method) return obj
def overloaded_function(*args, **kwargs): for func in typed_functions: # TODO: more intelligent overload resolution arg_spec = getattr(func, '_arguments', None) if not arg_spec: continue # TODO: encapsulate this mechanism inside EPD class expects_decorator = ExpectedParametersDecorator() expects_decorator.arg_spec = arg_spec expects_decorator.omit_self = getattr(func, '_has_self', False) try: expects_decorator._validate_arguments(args, kwargs) except ArgumentError: continue return func(*args, **kwargs)