test_kwargs_and_defaults.py revision 12391
111986Sandreas.sandberg@arm.comimport pytest
212391Sjason@lowepower.comfrom pybind11_tests import kwargs_and_defaults as m
311986Sandreas.sandberg@arm.com
411986Sandreas.sandberg@arm.com
511986Sandreas.sandberg@arm.comdef test_function_signatures(doc):
612391Sjason@lowepower.com    assert doc(m.kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
712391Sjason@lowepower.com    assert doc(m.kw_func1) == "kw_func1(x: int, y: int) -> str"
812391Sjason@lowepower.com    assert doc(m.kw_func2) == "kw_func2(x: int=100, y: int=200) -> str"
912391Sjason@lowepower.com    assert doc(m.kw_func3) == "kw_func3(data: str='Hello world!') -> None"
1012391Sjason@lowepower.com    assert doc(m.kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str"
1112391Sjason@lowepower.com    assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str"
1212391Sjason@lowepower.com    assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str"
1312391Sjason@lowepower.com    assert doc(m.args_function) == "args_function(*args) -> tuple"
1412391Sjason@lowepower.com    assert doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
1512391Sjason@lowepower.com    assert doc(m.KWClass.foo0) == \
1612391Sjason@lowepower.com        "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
1712391Sjason@lowepower.com    assert doc(m.KWClass.foo1) == \
1812391Sjason@lowepower.com        "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
1911986Sandreas.sandberg@arm.com
2011986Sandreas.sandberg@arm.com
2111986Sandreas.sandberg@arm.comdef test_named_arguments(msg):
2212391Sjason@lowepower.com    assert m.kw_func0(5, 10) == "x=5, y=10"
2311986Sandreas.sandberg@arm.com
2412391Sjason@lowepower.com    assert m.kw_func1(5, 10) == "x=5, y=10"
2512391Sjason@lowepower.com    assert m.kw_func1(5, y=10) == "x=5, y=10"
2612391Sjason@lowepower.com    assert m.kw_func1(y=10, x=5) == "x=5, y=10"
2711986Sandreas.sandberg@arm.com
2812391Sjason@lowepower.com    assert m.kw_func2() == "x=100, y=200"
2912391Sjason@lowepower.com    assert m.kw_func2(5) == "x=5, y=200"
3012391Sjason@lowepower.com    assert m.kw_func2(x=5) == "x=5, y=200"
3112391Sjason@lowepower.com    assert m.kw_func2(y=10) == "x=100, y=10"
3212391Sjason@lowepower.com    assert m.kw_func2(5, 10) == "x=5, y=10"
3312391Sjason@lowepower.com    assert m.kw_func2(x=5, y=10) == "x=5, y=10"
3411986Sandreas.sandberg@arm.com
3511986Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
3611986Sandreas.sandberg@arm.com        # noinspection PyArgumentList
3712391Sjason@lowepower.com        m.kw_func2(x=5, y=10, z=12)
3812037Sandreas.sandberg@arm.com    assert excinfo.match(
3912037Sandreas.sandberg@arm.com        r'(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))' + '{3}$')
4011986Sandreas.sandberg@arm.com
4112391Sjason@lowepower.com    assert m.kw_func4() == "{13 17}"
4212391Sjason@lowepower.com    assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
4311986Sandreas.sandberg@arm.com
4412391Sjason@lowepower.com    assert m.kw_func_udl(x=5, y=10) == "x=5, y=10"
4512391Sjason@lowepower.com    assert m.kw_func_udl_z(x=5) == "x=5, y=0"
4611986Sandreas.sandberg@arm.com
4711986Sandreas.sandberg@arm.com
4811986Sandreas.sandberg@arm.comdef test_arg_and_kwargs():
4911986Sandreas.sandberg@arm.com    args = 'arg1_value', 'arg2_value', 3
5012391Sjason@lowepower.com    assert m.args_function(*args) == args
5111986Sandreas.sandberg@arm.com
5211986Sandreas.sandberg@arm.com    args = 'a1', 'a2'
5311986Sandreas.sandberg@arm.com    kwargs = dict(arg3='a3', arg4=4)
5412391Sjason@lowepower.com    assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
5512037Sandreas.sandberg@arm.com
5612037Sandreas.sandberg@arm.com
5712037Sandreas.sandberg@arm.comdef test_mixed_args_and_kwargs(msg):
5812391Sjason@lowepower.com    mpa = m.mixed_plus_args
5912391Sjason@lowepower.com    mpk = m.mixed_plus_kwargs
6012391Sjason@lowepower.com    mpak = m.mixed_plus_args_kwargs
6112391Sjason@lowepower.com    mpakd = m.mixed_plus_args_kwargs_defaults
6212037Sandreas.sandberg@arm.com
6312037Sandreas.sandberg@arm.com    assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
6412037Sandreas.sandberg@arm.com    assert mpa(1, 2.5) == (1, 2.5, ())
6512037Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
6612037Sandreas.sandberg@arm.com        assert mpa(1)
6712037Sandreas.sandberg@arm.com    assert msg(excinfo.value) == """
6812037Sandreas.sandberg@arm.com        mixed_plus_args(): incompatible function arguments. The following argument types are supported:
6912037Sandreas.sandberg@arm.com            1. (arg0: int, arg1: float, *args) -> tuple
7012037Sandreas.sandberg@arm.com
7112037Sandreas.sandberg@arm.com        Invoked with: 1
7212037Sandreas.sandberg@arm.com    """  # noqa: E501 line too long
7312037Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
7412037Sandreas.sandberg@arm.com        assert mpa()
7512037Sandreas.sandberg@arm.com    assert msg(excinfo.value) == """
7612037Sandreas.sandberg@arm.com        mixed_plus_args(): incompatible function arguments. The following argument types are supported:
7712037Sandreas.sandberg@arm.com            1. (arg0: int, arg1: float, *args) -> tuple
7812037Sandreas.sandberg@arm.com
7912037Sandreas.sandberg@arm.com        Invoked with:
8012037Sandreas.sandberg@arm.com    """  # noqa: E501 line too long
8112037Sandreas.sandberg@arm.com
8212037Sandreas.sandberg@arm.com    assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (-2, 3.5, {'e': 2.71828, 'pi': 3.14159})
8312037Sandreas.sandberg@arm.com    assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
8412037Sandreas.sandberg@arm.com        7, 7.7, (7.77, 7.777, 7.7777), {'minusseven': -7})
8512037Sandreas.sandberg@arm.com    assert mpakd() == (1, 3.14159, (), {})
8612037Sandreas.sandberg@arm.com    assert mpakd(3) == (3, 3.14159, (), {})
8712037Sandreas.sandberg@arm.com    assert mpakd(j=2.71828) == (1, 2.71828, (), {})
8812037Sandreas.sandberg@arm.com    assert mpakd(k=42) == (1, 3.14159, (), {'k': 42})
8912037Sandreas.sandberg@arm.com    assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
9012037Sandreas.sandberg@arm.com        1, 1, (2, 3, 5, 8), {'then': 13, 'followedby': 21})
9112037Sandreas.sandberg@arm.com    # Arguments specified both positionally and via kwargs should fail:
9212037Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
9312037Sandreas.sandberg@arm.com        assert mpakd(1, i=1)
9412037Sandreas.sandberg@arm.com    assert msg(excinfo.value) == """
9512037Sandreas.sandberg@arm.com        mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
9612037Sandreas.sandberg@arm.com            1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
9712037Sandreas.sandberg@arm.com
9812037Sandreas.sandberg@arm.com        Invoked with: 1; kwargs: i=1
9912037Sandreas.sandberg@arm.com    """  # noqa: E501 line too long
10012037Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
10112037Sandreas.sandberg@arm.com        assert mpakd(1, 2, j=1)
10212037Sandreas.sandberg@arm.com    assert msg(excinfo.value) == """
10312037Sandreas.sandberg@arm.com        mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
10412037Sandreas.sandberg@arm.com            1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
10512037Sandreas.sandberg@arm.com
10612037Sandreas.sandberg@arm.com        Invoked with: 1, 2; kwargs: j=1
10712037Sandreas.sandberg@arm.com    """  # noqa: E501 line too long
108