def test_success(self): self.assertSigsEqual(s('a, b:1'), signature(modifiers.annotate(b=1)(f('a, b')))) self.assertSigsEqual( s('a:1, b:2'), signature(modifiers.annotate(a=1, b=2)(f('a, b')))) self.assertSigsEqual(s('a:1, b', 2), signature(modifiers.annotate(2, a=1)(f('a, b'))))
def test_preserve_annotations(self): func = f('self, a:2, b, c:3', 4) tr = modifiers._PokTranslator(func, kwoargs=('a', 'b')) self.assertSigsEqual(s('self, c:3, *, a:2, b', 4), signature(tr)) self.assertSigsEqual(s('c:3, *, a:2, b', 4), signature(safe_get(tr, object(), object)))
def _test(self, expected_sig_str, orig_sig_str, exceptions): orig_func = f(orig_sig_str) expected_sig = s(expected_sig_str) func = modifiers.autokwoargs(exceptions=exceptions)(orig_func) self.assertSigsEqual(expected_sig, signature(func)) if not exceptions: # test the arg-less form of @autokwargs func = modifiers.autokwoargs(orig_func) self.assertSigsEqual(expected_sig, signature(func))
def _test(self, func, ensure_incoherent=True): self.assertSigsEqual(specifiers.signature(func), signatures.signature(func)) if ensure_incoherent: with self.assertRaises(AssertionError): support.test_func_sig_coherent(func, check_return=False, check_invalid=False)
def _test(self, func, sig_str, args, kwargs, ret, decorators): """Tests .wrappers.wrapper_decorator Checks its reported signature, that it chains functions correctly and that it reports its decorators properly via .wrappers.wrappers """ self.assertSigsEqual(signatures.signature(func), support.s(sig_str)) self.assertEqual(func(*args, **kwargs), ret) self.assertEqual(list(wrappers.wrappers(func)), [x.wrapper for x in decorators])
def test_missing_freevar(self): def make_closure(): var = 1 del var def func(a, *p, **k): var(*p, **k) # pyflakes: silence return func func = make_closure() self.assertSigsEqual( specifiers.signature(func), signatures.signature(func))
def poktranslator_test(self, expected_sig_str, orig_sig_str, posoargs, kwoargs): expected_sig = s(expected_sig_str) orig_sig = s(orig_sig_str) for exp, orig in defaults_variations(expected_sig, orig_sig): for exp, orig in stars_variations(exp, orig): func = modifiers._PokTranslator(func_from_sig(orig), posoargs, kwoargs) self.assertSigsEqual(exp, signature(func)) test_func_sig_coherent(func) repr(func) # must not cause an error
def _test(self, func, sig_str, args, kwargs, ret, exp_sources): """Tests .wrappers.decorator Checks its reported signature, that it chains functions correctly and that it its decorators are in the signature sources """ sig = signatures.signature(func) self.assertSigsEqual(sig, support.s(sig_str)) self.assertEqual(func(*args, **kwargs), ret) self.assertSourcesEqual(sig.sources, exp_sources, func, depth_order=True)
def test_as_forged_dunder_call_method(self): class MyClass(object): __signature__ = specifiers.as_forged @specifiers.forwards_to_method('method') def __call__(self, x, *args, **kwags): raise NotImplementedError def method(self, a, b, c): raise NotImplementedError exp = support.s('x, a, b, c') self.assertSigsEqual(signatures.signature(MyClass()), exp) self.assertSigsEqual(specifiers.signature(MyClass()), exp)
def test_as_forged_forwards(self): def function(a, b, c): raise NotImplementedError class MyClass(object): __signature__ = specifiers.as_forged def __init__(self): specifiers.forwards_to_function(function)(self) def __call__(self, x, *args, **kwargs): raise NotImplementedError self.assertSigsEqual(signatures.signature(MyClass()), support.s('x, a, b, c'))
def forwards(wrapper, wrapped, *args, **kwargs): """Returns an effective signature of ``wrapper`` when it forwards its ``*args`` and ``**kwargs`` to ``wrapped``. :param callable wrapper: The outer callable :param callable wrapped: The callable ``wrapper``'s extra arguments are passed to. :return: a `inspect.Signature` object |forwards_params| """ return signatures.forwards( signatures.signature(wrapper), signature(wrapped), *args, **kwargs)
def test_as_forged(self): sig = support.s('this, isa, test') @specifiers.forger_function def forger(obj): return sig class MyClass(object): __signature__ = specifiers.as_forged def __init__(self): forger()(self) def __call__(self): raise NotImplementedError self.assertSigsEqual(signatures.signature(MyClass()), sig)
def _test(self, func, sig_str, args, kwargs, ret, exp_sources, decorators): """Tests .wrappers.decorator Checks its reported signature, that it chains functions correctly and that it its decorators are in the signature sources """ sig = signatures.signature(func) self.assertSigsEqual(sig, support.s(sig_str)) self.assertEqual(func(*args, **kwargs), ret) self.assertSourcesEqual(sig.sources, exp_sources, func, depth_order=True) self.assertEqual(list(wrappers.wrappers(func)), [ getclosure(d)[getcode(d).co_freevars.index('func')].cell_contents for d in decorators ])
def test_emulation(self): func = specifiers.forwards_to_method('abc', emulate=False)(_func) self.assertTrue(_func is func) func = specifiers.forwards_to_method('abc')(_func) self.assertTrue(_func is func) class Cls(object): func = specifiers.forwards_to_method('abc')(_func) func = getattr(Cls.func, '__func__', func) self.assertTrue(_func is func) self.assertTrue(_func is Cls().func.__func__) class Cls(object): def func(self, abc, *args, **kwargs): raise NotImplementedError def abc(self, x): raise NotImplementedError method = Cls().func func = specifiers.forwards_to_method('abc')(method) self.assertTrue(isinstance(func, specifiers._ForgerWrapper)) self.assertEqual(func.__wrapped__, method) self.assertRaises(AttributeError, specifiers.forwards_to_method('abc', emulate=False), Cls().func) exp = support.s('abc, x') self.assertSigsEqual(signatures.signature(func), exp) self.assertSigsEqual(specifiers.signature(func), exp) class Emulator(object): def __init__(self, obj, forger): self.obj = obj self.forger = forger func = specifiers.forwards_to_function(func, emulate=Emulator)(_func) self.assertTrue(isinstance(func, Emulator)) @specifiers.forwards_to_function(_func, emulate=True) def func(x, y, *args, **kwargs): return x + y self.assertEqual(5, func(2, 3))
def _test(self, expected_sig_str, orig_sig_str, end): orig_func = f(orig_sig_str) func = modifiers.posoargs(end=end)(orig_func) self.assertSigsEqual(s(expected_sig_str), signature(func))
def _test(self, func): self.assertSigsEqual(specifiers.signature(func), signatures.signature(func))
def wd_tests(self, func, sig_str, args, kwargs, ret, decorators): self.assertSigsEqual(signatures.signature(func), support.s(sig_str)) self.assertEqual(func(*args, **kwargs), ret) self.assertEqual(list(wrappers.wrappers(func)), [x.wrapper for x in decorators])
def test_pok_interact(self): pok = f('self, a, *, b') annotated = modifiers.annotate(a=1, b=2)(pok) self.assertSigsEqual(s('self, a:1, *, b:2'), signature(annotated)) self.assertSigsEqual(s('a:1, *, b:2'), signature(safe_get(annotated, object(), object)))
def test_use_twice(self): annotator = modifiers.annotate(a=1) self.assertSigsEqual(s('a:1, b'), signature(annotator(f('a, b')))) self.assertSigsEqual(s('a:1'), signature(annotator(f('a'))))
def get_signature(self, obj): return signatures.merge( signatures.signature(self), *(specifiers.signature(func) for func in self.functions) )
def test_merge_other(self): orig_func = f('a, b') func = modifiers.kwoargs('b')(modifiers.posoargs(end='a')(orig_func)) self.assertSigsEqual(s('<a>, *, b'), signature(func))
def _test(self, expected_sig_str, orig_sig_str, start): orig_func = f(orig_sig_str) func = modifiers.kwoargs(start=start)(orig_func) self.assertSigsEqual(s(expected_sig_str), signature(func))