blob: 90f8489ed78fd7525685019b0363b0d4809921d5 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
Dean Moldovan16db1bf2016-09-03 17:25:40 +02002from pybind11_tests import (kw_func0, kw_func1, kw_func2, kw_func3, kw_func4, args_function,
3 args_kwargs_function, kw_func_udl, kw_func_udl_z, KWClass)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02004
5
6def test_function_signatures(doc):
Dean Moldovan665e8802016-08-12 22:28:31 +02007 assert doc(kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
8 assert doc(kw_func1) == "kw_func1(x: int, y: int) -> str"
9 assert doc(kw_func2) == "kw_func2(x: int=100, y: int=200) -> str"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010 assert doc(kw_func3) == "kw_func3(data: str='Hello world!') -> None"
Dean Moldovan665e8802016-08-12 22:28:31 +020011 assert doc(kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str"
12 assert doc(kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str"
13 assert doc(kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str"
Dean Moldovan99dbdc12016-08-19 13:45:36 +020014 assert doc(args_function) == "args_function(*args) -> tuple"
15 assert doc(args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020016 assert doc(KWClass.foo0) == "foo0(self: m.KWClass, arg0: int, arg1: float) -> None"
17 assert doc(KWClass.foo1) == "foo1(self: m.KWClass, x: int, y: float) -> None"
18
19
Dean Moldovan665e8802016-08-12 22:28:31 +020020def test_named_arguments(msg):
21 assert kw_func0(5, 10) == "x=5, y=10"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020022
Dean Moldovan665e8802016-08-12 22:28:31 +020023 assert kw_func1(5, 10) == "x=5, y=10"
24 assert kw_func1(5, y=10) == "x=5, y=10"
25 assert kw_func1(y=10, x=5) == "x=5, y=10"
26
27 assert kw_func2() == "x=100, y=200"
28 assert kw_func2(5) == "x=5, y=200"
29 assert kw_func2(x=5) == "x=5, y=200"
30 assert kw_func2(y=10) == "x=100, y=10"
31 assert kw_func2(5, 10) == "x=5, y=10"
32 assert kw_func2(x=5, y=10) == "x=5, y=10"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020033
34 with pytest.raises(TypeError) as excinfo:
35 # noinspection PyArgumentList
36 kw_func2(x=5, y=10, z=12)
Dean Moldovand47febc2017-03-10 15:42:42 +010037 assert excinfo.match(
38 r'(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))' + '{3}$')
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020039
Dean Moldovan665e8802016-08-12 22:28:31 +020040 assert kw_func4() == "{13 17}"
41 assert kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020042
Dean Moldovan665e8802016-08-12 22:28:31 +020043 assert kw_func_udl(x=5, y=10) == "x=5, y=10"
44 assert kw_func_udl_z(x=5) == "x=5, y=0"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020045
46
Dean Moldovan99dbdc12016-08-19 13:45:36 +020047def test_arg_and_kwargs():
Dean Moldovan99dbdc12016-08-19 13:45:36 +020048 args = 'arg1_value', 'arg2_value', 3
49 assert args_function(*args) == args
50
51 args = 'a1', 'a2'
52 kwargs = dict(arg3='a3', arg4=4)
53 assert args_kwargs_function(*args, **kwargs) == (args, kwargs)
Jason Rhinelander2686da82017-01-21 23:42:14 -050054
55
56def test_mixed_args_and_kwargs(msg):
57 from pybind11_tests import (mixed_plus_args, mixed_plus_kwargs, mixed_plus_args_kwargs,
58 mixed_plus_args_kwargs_defaults)
59 mpa = mixed_plus_args
60 mpk = mixed_plus_kwargs
61 mpak = mixed_plus_args_kwargs
62 mpakd = mixed_plus_args_kwargs_defaults
63
64 assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
65 assert mpa(1, 2.5) == (1, 2.5, ())
66 with pytest.raises(TypeError) as excinfo:
67 assert mpa(1)
68 assert msg(excinfo.value) == """
69 mixed_plus_args(): incompatible function arguments. The following argument types are supported:
70 1. (arg0: int, arg1: float, *args) -> tuple
71
72 Invoked with: 1
Jason Rhinelander231e1672017-02-23 21:04:46 -050073 """ # noqa: E501 line too long
Jason Rhinelander2686da82017-01-21 23:42:14 -050074 with pytest.raises(TypeError) as excinfo:
75 assert mpa()
76 assert msg(excinfo.value) == """
77 mixed_plus_args(): incompatible function arguments. The following argument types are supported:
78 1. (arg0: int, arg1: float, *args) -> tuple
79
80 Invoked with:
Jason Rhinelander231e1672017-02-23 21:04:46 -050081 """ # noqa: E501 line too long
Jason Rhinelander2686da82017-01-21 23:42:14 -050082
83 assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (-2, 3.5, {'e': 2.71828, 'pi': 3.14159})
84 assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
85 7, 7.7, (7.77, 7.777, 7.7777), {'minusseven': -7})
86 assert mpakd() == (1, 3.14159, (), {})
87 assert mpakd(3) == (3, 3.14159, (), {})
88 assert mpakd(j=2.71828) == (1, 2.71828, (), {})
89 assert mpakd(k=42) == (1, 3.14159, (), {'k': 42})
90 assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
91 1, 1, (2, 3, 5, 8), {'then': 13, 'followedby': 21})
Jason Rhinelander231e1672017-02-23 21:04:46 -050092 # Arguments specified both positionally and via kwargs should fail:
Jason Rhinelander2686da82017-01-21 23:42:14 -050093 with pytest.raises(TypeError) as excinfo:
94 assert mpakd(1, i=1)
95 assert msg(excinfo.value) == """
Jason Rhinelander231e1672017-02-23 21:04:46 -050096 mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
97 1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
98
99 Invoked with: 1; kwargs: i=1
100 """ # noqa: E501 line too long
Jason Rhinelander2686da82017-01-21 23:42:14 -0500101 with pytest.raises(TypeError) as excinfo:
102 assert mpakd(1, 2, j=1)
103 assert msg(excinfo.value) == """
Jason Rhinelander231e1672017-02-23 21:04:46 -0500104 mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
105 1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
106
107 Invoked with: 1, 2; kwargs: j=1
108 """ # noqa: E501 line too long