import sys from random import random import unittest from inspect import signature, Signature, Parameter # Assuming we are importing the target objects from main.py from main import create_function, add_signature_parameters, remove_signature_parameters, with_signature, wraps, create_wrapper class TestDynamicFunctionGeneration(unittest.TestCase): @unittest.expectedFailure def test_ex_nihilo(self): """ First example from the documentation: tests that we can generate a function from a string """ # Parameterize types and decorators for decorator in [False, True]: for type in ['str', 'Signature']: # (1) define the signature. if type == 'str': func_sig = "foo(b, a=0)" func_name = None else: parameters = [ Parameter('b', kind=Parameter.POSITIONAL_OR_KEYWORD), Parameter('a', kind=Parameter.POSITIONAL_OR_KEYWORD, default=0), ] func_sig = Signature(parameters) func_name = 'foo' # (2) define the function implementation def func_impl(*args, **kwargs): """This docstring will be used in the generated function by default""" print("func_impl called !") return args, kwargs # (3) create the dynamic function if decorator: gen_func = with_signature(func_sig, func_name=func_name)(func_impl) else: gen_func = create_function(func_sig, func_impl, func_name=func_name) # first check the source code ref_src = "def foo(b, a=0):\n return _func_impl_(b=b, a=a)\n" print("Generated Source:\n" + gen_func.__source__) self.assertEqual(gen_func.__source__, ref_src) # then the behaviour args, kwargs = gen_func(2) self.assertEqual(args, ()) self.assertEqual(kwargs, {'a': 0, 'b': 2}) @unittest.skipIf(sys.version_info < (3, 0), reason="keyword-only signatures require python 3+") def test_ex_nihilo_kw_only(self): """Same as ex nihilo but keyword only""" def func_impl(*args, **kwargs): """This docstring will be used in the generated function by default""" print("func_impl called !") return args, kwargs func_sig = "foo(b, *, a=0, **kwargs)" gen_func = create_function(func_sig, func_impl) ref_src = "def foo(b, *, a=0, **kwargs):\n return _func_impl_(b=b, a=a, **kwargs)\n" print(gen_func.__source__) self.assertEqual(gen_func.__source__, ref_src) def test_from_sig_wrapper(self): """ Tests that we can create a function from a Signature object """ def foo(b, a=0): print("foo called: b=%s, a=%s" % (b, a)) return b, a # capture the name and signature of existing function `foo` func_name = foo.__name__ original_func_sig = signature(foo) print("Original Signature: %s" % original_func_sig) # modify the signature to add a new parameter params = list(original_func_sig.parameters.values()) params.insert(0, Parameter('z', kind=Parameter.POSITIONAL_OR_KEYWORD)) func_sig = original_func_sig.replace(parameters=params) print("New Signature: %s" % func_sig) # define the implementation def func_impl(z, *args, **kwargs): print("func_impl called ! z=%s" % z) # call the foo function output = foo(*args, **kwargs) # return augmented output return z, output # create the dynamic function gen_func = wraps(foo, new_sig=func_sig)(func_impl) # check the source code ref_src = "def foo(z, b, a=0):\n return _func_impl_(z=z, b=b, a=a)\n" print("Generated Source:\n" + gen_func.__source__) self.assertEqual(gen_func.__source__, ref_src) # then the behaviour self.assertEqual(gen_func(3, 2), (3, (2, 0))) def test_helper_functions(self): """ Tests that the signature modification helpers work """ def foo(b, c, a=0): pass # original signature foo_sig = signature(foo) print("original signature: %s" % foo_sig) # let's modify it new_sig = add_signature_parameters(foo_sig, first=Parameter('z', kind=Parameter.POSITIONAL_OR_KEYWORD), last=Parameter('o', kind=Parameter.POSITIONAL_OR_KEYWORD, default=True) ) new_sig = remove_signature_parameters(new_sig, 'b', 'a') print("modified signature: %s" % new_sig) self.assertEqual(str(new_sig), '(z, c, o=True)') def test_injection(self): """ Tests that the function can be injected as first argument when inject_as_first_arg=True """ def generic_handler(f, *args, **kwargs): print("This is generic handler called by %s" % f.__name__) # here you could use f.__name__ in a if statement to determine what to do if f.__name__ == "func1": print("called from func1 !") return args, kwargs # generate 2 functions func1 = create_function("func1(a, b)", generic_handler, inject_as_first_arg=True) func2 = create_function("func2(a, d)", generic_handler, inject_as_first_arg=True) func1(1, 2) func2(1, 2) def test_var_length(self): """Demonstrates how variable-length arguments are passed to the handler """ def generate_function(func_sig, dummy_call): def func_impl(*args, **kwargs): """This docstring will be used in the generated function by default""" print("func_impl called !") dummy_call(*args, **kwargs) return args, kwargs return create_function(func_sig, func_impl) func_sig = "foo(a, b=0, *args, **kwargs)" def dummy_call(a, b=0, *args, **kwargs): print() gen_func = generate_function(func_sig, dummy_call) print(gen_func.__source__) self.assertEqual(gen_func(0, 1, 2), ((0, 1, 2), {})) self.assertEqual(gen_func(0, b=1), ((0, 1), {})) self.assertEqual(gen_func(b=1, a=0), ((0, 1), {})) with self.assertRaises(TypeError): gen_func(2, a=0, b=1) func_sig = "foo(b=0, *args, **kwargs)" def dummy_call(b=0, *args, **kwargs): print() gen_func = generate_function(func_sig, dummy_call) print(gen_func.__source__) self.assertEqual(gen_func(1, 0), ((1, 0), {})) self.assertEqual(gen_func(b=1), ((1,), {})) with self.assertRaises(TypeError): gen_func(1, b=0) def test_positional_only(self): """Tests that as of today positional-only signatures translate to bad strings """ params = [Parameter('a', kind=Parameter.POSITIONAL_ONLY), Parameter('b', kind=Parameter.POSITIONAL_OR_KEYWORD)] self.assertIn(str(Signature(parameters=params)), {"(, b)", "(a, /, b)"}) def test_with_signature(self): """ Tests that @with_signature works as expected """ @with_signature("foo(a)") def foo(**kwargs): return 'hello' with self.assertRaises(TypeError): foo() self.assertEqual(str(signature(foo)), "(a)") self.assertEqual(foo('dummy'), 'hello') def test_with_signature_none(self): """ Test that using None works """ def foo(a): return a new = with_signature(None, func_name='f')(foo) self.assertEqual(new('hello'), 'hello') self.assertEqual(str(signature(new)), "(a)") # check that the object was not wrapped self.assertIs(new, foo) self.assertEqual(new.__name__, 'f') def test_wraps(self): """ """ # we want to wrap this function f to add some prints before calls def foo(a, b=1): return a + b # create our wrapper: it will have the same signature than f @wraps(foo) def enhanced_foo(*args, **kwargs): print('hello!') print('b=%s' % kwargs['b']) # then call f as usual return foo(*args, **kwargs) self.assertEqual(enhanced_foo(1, 2), 3) self.assertEqual(enhanced_foo(b=0, a=1), 1) self.assertEqual(enhanced_foo(1), 2) with self.assertRaises(TypeError): # does not print anything in case of error enhanced_foo() def test_wraps_functools(self): """ same with functools.wraps """ from functools import wraps # we want to wrap this function f to add some prints before calls def foo(a, b=1): return a + b # create our wrapper: it will have the same signature than f @wraps(foo) def enhanced_foo(*args, **kwargs): print('hello!') print('b=%s' % kwargs['b']) return foo(*args, **kwargs) self.assertEqual(enhanced_foo(b=0, a=1), 1) with self.assertRaises(KeyError): enhanced_foo() def test_wraps_remove(self): def inject_random_a(f): """ A decorator that injects a random number inside the `a` argument, removing it from the exposed signature """ @wraps(f, remove_args='a') def my_wrapper(*args, **kwargs): kwargs['a'] = random() return f(*args, **kwargs) return my_wrapper @inject_random_a def summer(a, b): return a + b self.assertGreaterEqual(summer(b=12), 12) self.assertLessEqual(summer(b=12), 13) def test_wraps_add_doc(self): def foo(b, a=0): print("foo called: b=%s, a=%s" % (b, a)) return b, a @wraps(foo, prepend_args='z') def foo_wrapper(z, *args, **kwargs): print("foo_wrapper called ! z=%s" % z) output = foo(*args, **kwargs) return z, output # call it self.assertEqual(foo_wrapper(3, 2), (3, (2, 0))) @unittest.parametrize("prepend", [True, False], ids="prepend={}".format) def test_wraps_add(self, prepend): def add_a_to_result(f): """ A decorator that injects a random number inside the `a` argument, removing it from the exposed signature """ decorator = wraps(f, prepend_args='a') if prepend else wraps(f, append_args='a') def my_wrapper(*args, **kwargs): a = kwargs.pop('a') return a + f(*args, **kwargs) my_wrapper = decorator(my_wrapper) return my_wrapper @add_a_to_result def identity(b): return b self.assertEqual(identity(b=12, a=0.5), 12.5) ref_str = "(a, b)" if prepend else "(b, a)" self.assertEqual(str(signature(identity)), ref_str) if __name__ == '__main__': unittest.main()