def check_implementation(self, klass): klass_methods = {k for k, v in klass.__dict__.items() if callable(v)} intf_methods = {k for k, v in self.__dict__.items() if callable(v)} if not intf_methods <= klass_methods: raise AssertionError(\ 'Methods not implemented:\n{0}.'.format(intf_methods-klass_methods)) for method in intf_methods: intf_spec = argspec(self.__dict__[method]) cls_spec = argspec(klass.__dict__[method]) if klass.__dict__[method].__doc__ is None: klass.__dict__[method].__doc__ = self.__dict__[method].__doc__ if not (cls_spec.args == intf_spec.args): raise AssertionError('Different arguments:\nInterface:\ {0} for {1}\nClass: {2} for {3}.'.format(intf_spec.args, method, cls_spec.args, method)) if bool(intf_spec.varargs)^bool(cls_spec.varargs): raise AssertionError('varargs missing in method {0}.'.format(method)) if bool(intf_spec.varkw)^bool(cls_spec.varkw): raise AssertionError('kwargs missing in method {0}.'.format(method)) if set(intf_spec.kwonlyargs) != set(cls_spec.kwonlyargs): raise AssertionError('kwonlyargs missing in method {0}.'.format(method)) return type.__new__(type(klass), klass.__name__, klass.__bases__, dict(klass.__dict__))
def SubsetEqual(self, arg): val = DoSomething( len(inspect.argspec(self.val)[0]) + len( inspect.argspec(self.val)[1] if inspect.argspec(self.val)[1] != None else [])) for i in val: if type(i) != tuple and type(i) != list: if self.val(i)[1].Equal(Number(0)): return Number(0) if self.val(*i)[1].Equal(Number(0)): return Number(0) return Number(1)
def __call__(self, cmd, arg): do_func = getattr(self, cmd) func_args = argspec(do_func).args if arg: if not func_args or ("self" in func_args and len(func_args) == 1): out = self._repl_error("This command takes no arguments") else: out = do_func(arg) else: out = do_func() return out
def test_decorated_attributes(decorator, func): '''Verify that attributes of a decorated function are same as original. __name__ __doc__ etc ''' decorated = decorator(func) assert decorated.__name__ == func.__name__ assert decorated.__doc__ == func.__doc__ assert decorated.__module__ == func.__module__ print('test_decorated_attributes ok') return 'ok' # We would like to say the parameters are identical but that will have # to wait for version >= 3.4. The decorator module does it but changes # the decorator syntax. from inspect import getargspec as argspec assert argspec(decorated) == argspec(func) print('dict func', func.__dict__) print('dict decorated', decorated.__dict__) assert decorated.__dict__ != func.__dict__ return 'ok'
def wrapper(*args, **kwargs): props = argspec(func) # if 'args' not in props: if isinstance(props[1], type(None)): args = args[:len(props[0])] if ((not isinstance(props[2], type(None))) or (not isinstance(props[3], type(None)))): return func(*args, **kwargs) return func(*args)