test_kwargs_and_defaults.py revision 12037
111986Sandreas.sandberg@arm.comimport pytest
211986Sandreas.sandberg@arm.comfrom pybind11_tests import (kw_func0, kw_func1, kw_func2, kw_func3, kw_func4, args_function,
311986Sandreas.sandberg@arm.com                            args_kwargs_function, kw_func_udl, kw_func_udl_z, KWClass)
411986Sandreas.sandberg@arm.com
511986Sandreas.sandberg@arm.com
611986Sandreas.sandberg@arm.comdef test_function_signatures(doc):
711986Sandreas.sandberg@arm.com    assert doc(kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
811986Sandreas.sandberg@arm.com    assert doc(kw_func1) == "kw_func1(x: int, y: int) -> str"
911986Sandreas.sandberg@arm.com    assert doc(kw_func2) == "kw_func2(x: int=100, y: int=200) -> str"
1011986Sandreas.sandberg@arm.com    assert doc(kw_func3) == "kw_func3(data: str='Hello world!') -> None"
1111986Sandreas.sandberg@arm.com    assert doc(kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str"
1211986Sandreas.sandberg@arm.com    assert doc(kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str"
1311986Sandreas.sandberg@arm.com    assert doc(kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str"
1411986Sandreas.sandberg@arm.com    assert doc(args_function) == "args_function(*args) -> tuple"
1511986Sandreas.sandberg@arm.com    assert doc(args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
1611986Sandreas.sandberg@arm.com    assert doc(KWClass.foo0) == "foo0(self: m.KWClass, arg0: int, arg1: float) -> None"
1711986Sandreas.sandberg@arm.com    assert doc(KWClass.foo1) == "foo1(self: m.KWClass, x: int, y: float) -> None"
1811986Sandreas.sandberg@arm.com
1911986Sandreas.sandberg@arm.com
2011986Sandreas.sandberg@arm.comdef test_named_arguments(msg):
2111986Sandreas.sandberg@arm.com    assert kw_func0(5, 10) == "x=5, y=10"
2211986Sandreas.sandberg@arm.com
2311986Sandreas.sandberg@arm.com    assert kw_func1(5, 10) == "x=5, y=10"
2411986Sandreas.sandberg@arm.com    assert kw_func1(5, y=10) == "x=5, y=10"
2511986Sandreas.sandberg@arm.com    assert kw_func1(y=10, x=5) == "x=5, y=10"
2611986Sandreas.sandberg@arm.com
2711986Sandreas.sandberg@arm.com    assert kw_func2() == "x=100, y=200"
2811986Sandreas.sandberg@arm.com    assert kw_func2(5) == "x=5, y=200"
2911986Sandreas.sandberg@arm.com    assert kw_func2(x=5) == "x=5, y=200"
3011986Sandreas.sandberg@arm.com    assert kw_func2(y=10) == "x=100, y=10"
3111986Sandreas.sandberg@arm.com    assert kw_func2(5, 10) == "x=5, y=10"
3211986Sandreas.sandberg@arm.com    assert kw_func2(x=5, y=10) == "x=5, y=10"
3311986Sandreas.sandberg@arm.com
3411986Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
3511986Sandreas.sandberg@arm.com        # noinspection PyArgumentList
3611986Sandreas.sandberg@arm.com        kw_func2(x=5, y=10, z=12)
3712037Sandreas.sandberg@arm.com    assert excinfo.match(
3812037Sandreas.sandberg@arm.com        r'(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))' + '{3}$')
3911986Sandreas.sandberg@arm.com
4011986Sandreas.sandberg@arm.com    assert kw_func4() == "{13 17}"
4111986Sandreas.sandberg@arm.com    assert kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
4211986Sandreas.sandberg@arm.com
4311986Sandreas.sandberg@arm.com    assert kw_func_udl(x=5, y=10) == "x=5, y=10"
4411986Sandreas.sandberg@arm.com    assert kw_func_udl_z(x=5) == "x=5, y=0"
4511986Sandreas.sandberg@arm.com
4611986Sandreas.sandberg@arm.com
4711986Sandreas.sandberg@arm.comdef test_arg_and_kwargs():
4811986Sandreas.sandberg@arm.com    args = 'arg1_value', 'arg2_value', 3
4911986Sandreas.sandberg@arm.com    assert args_function(*args) == args
5011986Sandreas.sandberg@arm.com
5111986Sandreas.sandberg@arm.com    args = 'a1', 'a2'
5211986Sandreas.sandberg@arm.com    kwargs = dict(arg3='a3', arg4=4)
5311986Sandreas.sandberg@arm.com    assert args_kwargs_function(*args, **kwargs) == (args, kwargs)
5412037Sandreas.sandberg@arm.com
5512037Sandreas.sandberg@arm.com
5612037Sandreas.sandberg@arm.comdef test_mixed_args_and_kwargs(msg):
5712037Sandreas.sandberg@arm.com    from pybind11_tests import (mixed_plus_args, mixed_plus_kwargs, mixed_plus_args_kwargs,
5812037Sandreas.sandberg@arm.com                                mixed_plus_args_kwargs_defaults)
5912037Sandreas.sandberg@arm.com    mpa = mixed_plus_args
6012037Sandreas.sandberg@arm.com    mpk = mixed_plus_kwargs
6112037Sandreas.sandberg@arm.com    mpak = mixed_plus_args_kwargs
6212037Sandreas.sandberg@arm.com    mpakd = mixed_plus_args_kwargs_defaults
6312037Sandreas.sandberg@arm.com
6412037Sandreas.sandberg@arm.com    assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
6512037Sandreas.sandberg@arm.com    assert mpa(1, 2.5) == (1, 2.5, ())
6612037Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
6712037Sandreas.sandberg@arm.com        assert mpa(1)
6812037Sandreas.sandberg@arm.com    assert msg(excinfo.value) == """
6912037Sandreas.sandberg@arm.com        mixed_plus_args(): incompatible function arguments. The following argument types are supported:
7012037Sandreas.sandberg@arm.com            1. (arg0: int, arg1: float, *args) -> tuple
7112037Sandreas.sandberg@arm.com
7212037Sandreas.sandberg@arm.com        Invoked with: 1
7312037Sandreas.sandberg@arm.com    """  # noqa: E501 line too long
7412037Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
7512037Sandreas.sandberg@arm.com        assert mpa()
7612037Sandreas.sandberg@arm.com    assert msg(excinfo.value) == """
7712037Sandreas.sandberg@arm.com        mixed_plus_args(): incompatible function arguments. The following argument types are supported:
7812037Sandreas.sandberg@arm.com            1. (arg0: int, arg1: float, *args) -> tuple
7912037Sandreas.sandberg@arm.com
8012037Sandreas.sandberg@arm.com        Invoked with:
8112037Sandreas.sandberg@arm.com    """  # noqa: E501 line too long
8212037Sandreas.sandberg@arm.com
8312037Sandreas.sandberg@arm.com    assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (-2, 3.5, {'e': 2.71828, 'pi': 3.14159})
8412037Sandreas.sandberg@arm.com    assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
8512037Sandreas.sandberg@arm.com        7, 7.7, (7.77, 7.777, 7.7777), {'minusseven': -7})
8612037Sandreas.sandberg@arm.com    assert mpakd() == (1, 3.14159, (), {})
8712037Sandreas.sandberg@arm.com    assert mpakd(3) == (3, 3.14159, (), {})
8812037Sandreas.sandberg@arm.com    assert mpakd(j=2.71828) == (1, 2.71828, (), {})
8912037Sandreas.sandberg@arm.com    assert mpakd(k=42) == (1, 3.14159, (), {'k': 42})
9012037Sandreas.sandberg@arm.com    assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
9112037Sandreas.sandberg@arm.com        1, 1, (2, 3, 5, 8), {'then': 13, 'followedby': 21})
9212037Sandreas.sandberg@arm.com    # Arguments specified both positionally and via kwargs should fail:
9312037Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
9412037Sandreas.sandberg@arm.com        assert mpakd(1, i=1)
9512037Sandreas.sandberg@arm.com    assert msg(excinfo.value) == """
9612037Sandreas.sandberg@arm.com        mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
9712037Sandreas.sandberg@arm.com            1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
9812037Sandreas.sandberg@arm.com
9912037Sandreas.sandberg@arm.com        Invoked with: 1; kwargs: i=1
10012037Sandreas.sandberg@arm.com    """  # noqa: E501 line too long
10112037Sandreas.sandberg@arm.com    with pytest.raises(TypeError) as excinfo:
10212037Sandreas.sandberg@arm.com        assert mpakd(1, 2, j=1)
10312037Sandreas.sandberg@arm.com    assert msg(excinfo.value) == """
10412037Sandreas.sandberg@arm.com        mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
10512037Sandreas.sandberg@arm.com            1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
10612037Sandreas.sandberg@arm.com
10712037Sandreas.sandberg@arm.com        Invoked with: 1, 2; kwargs: j=1
10812037Sandreas.sandberg@arm.com    """  # noqa: E501 line too long
109