def test_get_func_params_no_kwarg(): def func1(*, a): pass given = {} with pytest.raises(TypeError): get_func_params(func1, given)
def test_get_func_params_unexpected_param(): def func1(a): pass given = {'a': 1, 'b': 2} with pytest.raises(TypeError): get_func_params(func1, given)
def test_get_func_params_as_func(): def a2(a, b, c=0, *, e, f=1): pass params_a2 = get_func_params(a2, {"a": 1, "b": 2, 'e': 3}) assert {'a': 1, 'b': 2, 'c': 0, 'e': 3, 'f': 1} == params_a2 params_a3 = get_func_params(a2, {"a": 1, "b": 2, "c": 3, 'e': 3, 'f': 2}) assert {'a': 1, 'b': 2, 'c': 3, 'e': 3, 'f': 2} == params_a3 with pytest.raises(TypeError): get_func_params(a2, {"a": 1, "b": 2, "c": 3, 'f': 2})
def test_get_func_params(): def func1(a, b, c=3, d=4): pass given = {'a': 1, 'b': 2, 'd': 44} expected = {'a': 1, 'b': 2, 'c': 3, 'd': 44} assert expected == get_func_params(func1, given) # неверный вызов функции with pytest.raises(UserWarning): get_func_params(func1, 'incorrect') # аргумент b не передан with pytest.raises(Exception): get_func_params(func1, {'a': 1})
def test_get_func_params_with_functools(): def a4(a, b, c=3, e=4, *, f=3): pass pa4 = partial(a4, 1, c=0) params_a4 = get_func_params(pa4, {"b": 22}) assert {'b': 22, 'c': 0, 'e': 4, 'f': 3} == params_a4
def test_get_func_params_as_method(): class A1: def func(self, a, b=0): pass params_a1 = get_func_params(A1().func, {"a": 1}) assert {'a': 1, 'b': 0} == params_a1
def test_get_func_params_with_kwargs(): def a5(a, **kwargs): pass params_a5 = get_func_params(a5, {"a": 1, "b": 22}) assert {'b': 22, 'a': 1} == params_a5