2 from pybind11_tests
import kwargs_and_defaults
as m
5 def test_function_signatures(doc
):
6 assert doc(m
.kw_func0
) == "kw_func0(arg0: int, arg1: int) -> str"
7 assert doc(m
.kw_func1
) == "kw_func1(x: int, y: int) -> str"
8 assert doc(m
.kw_func2
) == "kw_func2(x: int=100, y: int=200) -> str"
9 assert doc(m
.kw_func3
) == "kw_func3(data: str='Hello world!') -> None"
10 assert doc(m
.kw_func4
) == "kw_func4(myList: List[int]=[13, 17]) -> str"
11 assert doc(m
.kw_func_udl
) == "kw_func_udl(x: int, y: int=300) -> str"
12 assert doc(m
.kw_func_udl_z
) == "kw_func_udl_z(x: int, y: int=0) -> str"
13 assert doc(m
.args_function
) == "args_function(*args) -> tuple"
14 assert doc(m
.args_kwargs_function
) == "args_kwargs_function(*args, **kwargs) -> tuple"
15 assert doc(m
.KWClass
.foo0
) == \
16 "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
17 assert doc(m
.KWClass
.foo1
) == \
18 "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
21 def test_named_arguments(msg
):
22 assert m
.kw_func0(5, 10) == "x=5, y=10"
24 assert m
.kw_func1(5, 10) == "x=5, y=10"
25 assert m
.kw_func1(5, y
=10) == "x=5, y=10"
26 assert m
.kw_func1(y
=10, x
=5) == "x=5, y=10"
28 assert m
.kw_func2() == "x=100, y=200"
29 assert m
.kw_func2(5) == "x=5, y=200"
30 assert m
.kw_func2(x
=5) == "x=5, y=200"
31 assert m
.kw_func2(y
=10) == "x=100, y=10"
32 assert m
.kw_func2(5, 10) == "x=5, y=10"
33 assert m
.kw_func2(x
=5, y
=10) == "x=5, y=10"
35 with pytest
.raises(TypeError) as excinfo
:
36 # noinspection PyArgumentList
37 m
.kw_func2(x
=5, y
=10, z
=12)
39 r
'(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))' + '{3}$')
41 assert m
.kw_func4() == "{13 17}"
42 assert m
.kw_func4(myList
=[1, 2, 3]) == "{1 2 3}"
44 assert m
.kw_func_udl(x
=5, y
=10) == "x=5, y=10"
45 assert m
.kw_func_udl_z(x
=5) == "x=5, y=0"
48 def test_arg_and_kwargs():
49 args
= 'arg1_value', 'arg2_value', 3
50 assert m
.args_function(*args
) == args
53 kwargs
= dict(arg3
='a3', arg4
=4)
54 assert m
.args_kwargs_function(*args
, **kwargs
) == (args
, kwargs
)
57 def test_mixed_args_and_kwargs(msg
):
58 mpa
= m
.mixed_plus_args
59 mpk
= m
.mixed_plus_kwargs
60 mpak
= m
.mixed_plus_args_kwargs
61 mpakd
= m
.mixed_plus_args_kwargs_defaults
63 assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
64 assert mpa(1, 2.5) == (1, 2.5, ())
65 with pytest
.raises(TypeError) as excinfo
:
67 assert msg(excinfo
.value
) == """
68 mixed_plus_args(): incompatible function arguments. The following argument types are supported:
69 1. (arg0: int, arg1: float, *args) -> tuple
72 """ # noqa: E501 line too long
73 with pytest
.raises(TypeError) as excinfo
:
75 assert msg(excinfo
.value
) == """
76 mixed_plus_args(): incompatible function arguments. The following argument types are supported:
77 1. (arg0: int, arg1: float, *args) -> tuple
80 """ # noqa: E501 line too long
82 assert mpk(-2, 3.5, pi
=3.14159, e
=2.71828) == (-2, 3.5, {'e': 2.71828, 'pi': 3.14159})
83 assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven
=-7) == (
84 7, 7.7, (7.77, 7.777, 7.7777), {'minusseven': -7})
85 assert mpakd() == (1, 3.14159, (), {})
86 assert mpakd(3) == (3, 3.14159, (), {})
87 assert mpakd(j
=2.71828) == (1, 2.71828, (), {})
88 assert mpakd(k
=42) == (1, 3.14159, (), {'k': 42})
89 assert mpakd(1, 1, 2, 3, 5, 8, then
=13, followedby
=21) == (
90 1, 1, (2, 3, 5, 8), {'then': 13, 'followedby': 21})
91 # Arguments specified both positionally and via kwargs should fail:
92 with pytest
.raises(TypeError) as excinfo
:
94 assert msg(excinfo
.value
) == """
95 mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
96 1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
98 Invoked with: 1; kwargs: i=1
99 """ # noqa: E501 line too long
100 with pytest
.raises(TypeError) as excinfo
:
101 assert mpakd(1, 2, j
=1)
102 assert msg(excinfo
.value
) == """
103 mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
104 1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
106 Invoked with: 1, 2; kwargs: j=1
107 """ # noqa: E501 line too long