6 from pybind11_tests
import kwargs_and_defaults
as m
10 assert doc(m.kw_func0) ==
"kw_func0(arg0: int, arg1: int) -> str"
11 assert doc(m.kw_func1) ==
"kw_func1(x: int, y: int) -> str"
12 assert doc(m.kw_func2) ==
"kw_func2(x: int = 100, y: int = 200) -> str"
13 assert doc(m.kw_func3) ==
"kw_func3(data: str = 'Hello world!') -> None"
14 assert doc(m.kw_func4) ==
"kw_func4(myList: List[int] = [13, 17]) -> str"
15 assert doc(m.kw_func_udl) ==
"kw_func_udl(x: int, y: int = 300) -> str"
16 assert doc(m.kw_func_udl_z) ==
"kw_func_udl_z(x: int, y: int = 0) -> str"
17 assert doc(m.args_function) ==
"args_function(*args) -> tuple"
19 doc(m.args_kwargs_function) ==
"args_kwargs_function(*args, **kwargs) -> tuple"
23 ==
"foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
27 ==
"foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
32 assert m.kw_func0(5, 10) ==
"x=5, y=10"
34 assert m.kw_func1(5, 10) ==
"x=5, y=10"
35 assert m.kw_func1(5, y=10) ==
"x=5, y=10"
36 assert m.kw_func1(y=10, x=5) ==
"x=5, y=10"
38 assert m.kw_func2() ==
"x=100, y=200"
39 assert m.kw_func2(5) ==
"x=5, y=200"
40 assert m.kw_func2(x=5) ==
"x=5, y=200"
41 assert m.kw_func2(y=10) ==
"x=100, y=10"
42 assert m.kw_func2(5, 10) ==
"x=5, y=10"
43 assert m.kw_func2(x=5, y=10) ==
"x=5, y=10"
45 with pytest.raises(TypeError)
as excinfo:
47 m.kw_func2(x=5, y=10, z=12)
49 r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))"
53 assert m.kw_func4() ==
"{13 17}"
54 assert m.kw_func4(myList=[1, 2, 3]) ==
"{1 2 3}"
56 assert m.kw_func_udl(x=5, y=10) ==
"x=5, y=10"
57 assert m.kw_func_udl_z(x=5) ==
"x=5, y=0"
61 args =
"arg1_value",
"arg2_value", 3
62 assert m.args_function(*args) == args
65 kwargs =
dict(arg3=
"a3", arg4=4)
66 assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
70 mpa = m.mixed_plus_args
71 mpk = m.mixed_plus_kwargs
72 mpak = m.mixed_plus_args_kwargs
73 mpakd = m.mixed_plus_args_kwargs_defaults
75 assert mpa(1, 2.5, 4, 99.5,
None) == (1, 2.5, (4, 99.5,
None))
76 assert mpa(1, 2.5) == (1, 2.5, ())
77 with pytest.raises(TypeError)
as excinfo:
82 mixed_plus_args(): incompatible function arguments. The following argument types are supported:
83 1. (arg0: int, arg1: float, *args) -> tuple
88 with pytest.raises(TypeError)
as excinfo:
93 mixed_plus_args(): incompatible function arguments. The following argument types are supported:
94 1. (arg0: int, arg1: float, *args) -> tuple
100 assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
103 {
"e": 2.71828,
"pi": 3.14159},
105 assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
108 (7.77, 7.777, 7.7777),
111 assert mpakd() == (1, 3.14159, (), {})
112 assert mpakd(3) == (3, 3.14159, (), {})
113 assert mpakd(j=2.71828) == (1, 2.71828, (), {})
114 assert mpakd(k=42) == (1, 3.14159, (), {
"k": 42})
115 assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
119 {
"then": 13,
"followedby": 21},
122 with pytest.raises(TypeError)
as excinfo:
127 mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
128 1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
130 Invoked with: 1; kwargs: i=1
133 with pytest.raises(TypeError)
as excinfo:
134 assert mpakd(1, 2, j=1)
138 mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
139 1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
141 Invoked with: 1, 2; kwargs: j=1
147 assert m.kw_only_all(i=1, j=2) == (1, 2)
148 assert m.kw_only_all(j=1, i=2) == (2, 1)
150 with pytest.raises(TypeError)
as excinfo:
151 assert m.kw_only_all(i=1) == (1,)
152 assert "incompatible function arguments" in str(excinfo.value)
154 with pytest.raises(TypeError)
as excinfo:
155 assert m.kw_only_all(1, 2) == (1, 2)
156 assert "incompatible function arguments" in str(excinfo.value)
158 assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
160 assert m.kw_only_with_defaults(z=8) == (3, 4, 5, 8)
161 assert m.kw_only_with_defaults(2, z=8) == (2, 4, 5, 8)
162 assert m.kw_only_with_defaults(2, j=7, k=8, z=9) == (2, 7, 8, 9)
163 assert m.kw_only_with_defaults(2, 7, z=9, k=8) == (2, 7, 8, 9)
165 assert m.kw_only_mixed(1, j=2) == (1, 2)
166 assert m.kw_only_mixed(j=2, i=3) == (3, 2)
167 assert m.kw_only_mixed(i=2, j=3) == (2, 3)
169 assert m.kw_only_plus_more(4, 5, k=6, extra=7) == (4, 5, 6, {
"extra": 7})
170 assert m.kw_only_plus_more(3, k=5, j=4, extra=6) == (3, 4, 5, {
"extra": 6})
171 assert m.kw_only_plus_more(2, k=3, extra=4) == (2, -1, 3, {
"extra": 4})
173 with pytest.raises(TypeError)
as excinfo:
174 assert m.kw_only_mixed(i=1) == (1,)
175 assert "incompatible function arguments" in str(excinfo.value)
177 with pytest.raises(RuntimeError)
as excinfo:
178 m.register_invalid_kw_only(m)
182 arg(): cannot specify an unnamed argument after an kw_only() annotation
188 assert m.pos_only_all(1, 2) == (1, 2)
189 assert m.pos_only_all(2, 1) == (2, 1)
191 with pytest.raises(TypeError)
as excinfo:
192 m.pos_only_all(i=1, j=2)
193 assert "incompatible function arguments" in str(excinfo.value)
195 assert m.pos_only_mix(1, 2) == (1, 2)
196 assert m.pos_only_mix(2, j=1) == (2, 1)
198 with pytest.raises(TypeError)
as excinfo:
199 m.pos_only_mix(i=1, j=2)
200 assert "incompatible function arguments" in str(excinfo.value)
202 assert m.pos_kw_only_mix(1, 2, k=3) == (1, 2, 3)
203 assert m.pos_kw_only_mix(1, j=2, k=3) == (1, 2, 3)
205 with pytest.raises(TypeError)
as excinfo:
206 m.pos_kw_only_mix(i=1, j=2, k=3)
207 assert "incompatible function arguments" in str(excinfo.value)
209 with pytest.raises(TypeError)
as excinfo:
210 m.pos_kw_only_mix(1, 2, 3)
211 assert "incompatible function arguments" in str(excinfo.value)
213 with pytest.raises(TypeError)
as excinfo:
215 assert "incompatible function arguments" in str(excinfo.value)
217 assert m.pos_only_def_mix(1) == (1, 2, 3)
218 assert m.pos_only_def_mix(1, 4) == (1, 4, 3)
219 assert m.pos_only_def_mix(1, 4, 7) == (1, 4, 7)
220 assert m.pos_only_def_mix(1, 4, k=7) == (1, 4, 7)
222 with pytest.raises(TypeError)
as excinfo:
223 m.pos_only_def_mix(1, j=4)
224 assert "incompatible function arguments" in str(excinfo.value)
228 assert "kw_only_all(*, i: int, j: int) -> tuple\n" == m.kw_only_all.__doc__
229 assert "kw_only_mixed(i: int, *, j: int) -> tuple\n" == m.kw_only_mixed.__doc__
230 assert "pos_only_all(i: int, j: int, /) -> tuple\n" == m.pos_only_all.__doc__
231 assert "pos_only_mix(i: int, /, j: int) -> tuple\n" == m.pos_only_mix.__doc__
233 "pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple\n"
234 == m.pos_kw_only_mix.__doc__
238 @pytest.mark.xfail(
"env.PYPY and env.PY2", reason=
"PyPy2 doesn't double count")
240 """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
242 refcount = m.arg_refcount_h
245 expected = refcount(myval)
246 assert m.arg_refcount_h(myval) == expected
247 assert m.arg_refcount_o(myval) == expected + 1
248 assert m.arg_refcount_h(myval) == expected
249 assert refcount(myval) == expected
251 assert m.mixed_plus_args(1, 2.0,
"a", myval) == (1, 2.0, (
"a", myval))
252 assert refcount(myval) == expected
254 assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {
"a": 1,
"b": myval})
255 assert refcount(myval) == expected
257 assert m.args_function(-1, myval) == (-1, myval)
258 assert refcount(myval) == expected
260 assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (
266 assert refcount(myval) == expected
268 assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == (
270 {
"a": 1,
"b": myval},
272 assert refcount(myval) == expected
274 exp3 = refcount(myval, myval, myval)
275 assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
276 assert refcount(myval) == expected
283 assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)
285 assert m.class_default_argument() ==
"<class 'decimal.Decimal'>"