1 from __future__
import annotations
5 from pybind11_tests
import kwargs_and_defaults
as m
9 assert doc(m.kw_func0) ==
"kw_func0(arg0: int, arg1: int) -> str"
10 assert doc(m.kw_func1) ==
"kw_func1(x: int, y: int) -> str"
11 assert doc(m.kw_func2) ==
"kw_func2(x: int = 100, y: int = 200) -> str"
12 assert doc(m.kw_func3) ==
"kw_func3(data: str = 'Hello world!') -> None"
13 assert doc(m.kw_func4) ==
"kw_func4(myList: list[int] = [13, 17]) -> str"
14 assert doc(m.kw_func_udl) ==
"kw_func_udl(x: int, y: int = 300) -> str"
15 assert doc(m.kw_func_udl_z) ==
"kw_func_udl_z(x: int, y: int = 0) -> str"
16 assert doc(m.args_function) ==
"args_function(*args) -> tuple"
18 doc(m.args_kwargs_function) ==
"args_kwargs_function(*args, **kwargs) -> tuple"
22 ==
"foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
26 ==
"foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
30 ==
"kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
34 ==
"kw_lb_func1(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
38 ==
"kw_lb_func2(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
42 ==
"kw_lb_func3(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
46 ==
"kw_lb_func4(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
50 ==
"kw_lb_func5(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
54 ==
"kw_lb_func6(custom: m.kwargs_and_defaults.CustomRepr = ) -> None"
58 ==
"kw_lb_func7(str_arg: str = 'First line.\\n Second line.') -> None"
62 ==
"kw_lb_func8(custom: m.kwargs_and_defaults.CustomRepr = ) -> None"
67 assert m.kw_func0(5, 10) ==
"x=5, y=10"
69 assert m.kw_func1(5, 10) ==
"x=5, y=10"
70 assert m.kw_func1(5, y=10) ==
"x=5, y=10"
71 assert m.kw_func1(y=10, x=5) ==
"x=5, y=10"
73 assert m.kw_func2() ==
"x=100, y=200"
74 assert m.kw_func2(5) ==
"x=5, y=200"
75 assert m.kw_func2(x=5) ==
"x=5, y=200"
76 assert m.kw_func2(y=10) ==
"x=100, y=10"
77 assert m.kw_func2(5, 10) ==
"x=5, y=10"
78 assert m.kw_func2(x=5, y=10) ==
"x=5, y=10"
80 with pytest.raises(TypeError)
as excinfo:
82 m.kw_func2(x=5, y=10, z=12)
84 r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$)){3}$"
87 assert m.kw_func4() ==
"{13 17}"
88 assert m.kw_func4(myList=[1, 2, 3]) ==
"{1 2 3}"
90 assert m.kw_func_udl(x=5, y=10) ==
"x=5, y=10"
91 assert m.kw_func_udl_z(x=5) ==
"x=5, y=0"
95 args =
"arg1_value",
"arg2_value", 3
96 assert m.args_function(*args) == args
99 kwargs = {
"arg3":
"a3",
"arg4": 4}
100 assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
104 mpa = m.mixed_plus_args
105 mpk = m.mixed_plus_kwargs
106 mpak = m.mixed_plus_args_kwargs
107 mpakd = m.mixed_plus_args_kwargs_defaults
109 assert mpa(1, 2.5, 4, 99.5,
None) == (1, 2.5, (4, 99.5,
None))
110 assert mpa(1, 2.5) == (1, 2.5, ())
111 with pytest.raises(TypeError)
as excinfo:
116 mixed_plus_args(): incompatible function arguments. The following argument types are supported:
117 1. (arg0: int, arg1: float, *args) -> tuple
122 with pytest.raises(TypeError)
as excinfo:
127 mixed_plus_args(): incompatible function arguments. The following argument types are supported:
128 1. (arg0: int, arg1: float, *args) -> tuple
134 assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
137 {
"e": 2.71828,
"pi": 3.14159},
139 assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
142 (7.77, 7.777, 7.7777),
145 assert mpakd() == (1, 3.14159, (), {})
146 assert mpakd(3) == (3, 3.14159, (), {})
147 assert mpakd(j=2.71828) == (1, 2.71828, (), {})
148 assert mpakd(k=42) == (1, 3.14159, (), {
"k": 42})
149 assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
153 {
"then": 13,
"followedby": 21},
156 with pytest.raises(TypeError)
as excinfo:
161 mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
162 1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
164 Invoked with: 1; kwargs: i=1
167 with pytest.raises(TypeError)
as excinfo:
168 assert mpakd(1, 2, j=1)
172 mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
173 1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
175 Invoked with: 1, 2; kwargs: j=1
180 assert m.args_kwonly(2, 2.5, z=22) == (2, 2.5, (), 22)
181 assert m.args_kwonly(2, 2.5,
"a",
"b",
"c", z=22) == (2, 2.5, (
"a",
"b",
"c"), 22)
182 assert m.args_kwonly(z=22, i=4, j=16) == (4, 16, (), 22)
184 with pytest.raises(TypeError)
as excinfo:
185 assert m.args_kwonly(2, 2.5, 22)
189 args_kwonly(): incompatible function arguments. The following argument types are supported:
190 1. (i: int, j: float, *args, z: int) -> tuple
192 Invoked with: 2, 2.5, 22
196 assert m.args_kwonly_kwargs(i=1, k=4, j=10, z=-1, y=9) == (
203 assert m.args_kwonly_kwargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, z=11, y=12) == (
206 (3, 4, 5, 6, 7, 8, 9, 10),
211 m.args_kwonly_kwargs.__doc__
212 ==
"args_kwonly_kwargs(i: int, j: float, *args, z: int, **kwargs) -> tuple\n"
216 m.args_kwonly_kwargs_defaults.__doc__
217 ==
"args_kwonly_kwargs_defaults(i: int = 1, j: float = 3.14159, *args, z: int = 42, **kwargs) -> tuple\n"
219 assert m.args_kwonly_kwargs_defaults() == (1, 3.14159, (), 42, {})
220 assert m.args_kwonly_kwargs_defaults(2) == (2, 3.14159, (), 42, {})
221 assert m.args_kwonly_kwargs_defaults(z=-99) == (1, 3.14159, (), -99, {})
222 assert m.args_kwonly_kwargs_defaults(5, 6, 7, 8) == (5, 6, (7, 8), 42, {})
223 assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8) == (5, 6, (7,), 42, {
"m": 8})
224 assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8, z=9) == (5, 6, (7,), 9, {
"m": 8})
228 assert m.kw_only_all(i=1, j=2) == (1, 2)
229 assert m.kw_only_all(j=1, i=2) == (2, 1)
231 with pytest.raises(TypeError)
as excinfo:
232 assert m.kw_only_all(i=1) == (1,)
233 assert "incompatible function arguments" in str(excinfo.value)
235 with pytest.raises(TypeError)
as excinfo:
236 assert m.kw_only_all(1, 2) == (1, 2)
237 assert "incompatible function arguments" in str(excinfo.value)
239 assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
241 assert m.kw_only_with_defaults(z=8) == (3, 4, 5, 8)
242 assert m.kw_only_with_defaults(2, z=8) == (2, 4, 5, 8)
243 assert m.kw_only_with_defaults(2, j=7, k=8, z=9) == (2, 7, 8, 9)
244 assert m.kw_only_with_defaults(2, 7, z=9, k=8) == (2, 7, 8, 9)
246 assert m.kw_only_mixed(1, j=2) == (1, 2)
247 assert m.kw_only_mixed(j=2, i=3) == (3, 2)
248 assert m.kw_only_mixed(i=2, j=3) == (2, 3)
250 assert m.kw_only_plus_more(4, 5, k=6, extra=7) == (4, 5, 6, {
"extra": 7})
251 assert m.kw_only_plus_more(3, k=5, j=4, extra=6) == (3, 4, 5, {
"extra": 6})
252 assert m.kw_only_plus_more(2, k=3, extra=4) == (2, -1, 3, {
"extra": 4})
254 with pytest.raises(TypeError)
as excinfo:
255 assert m.kw_only_mixed(i=1) == (1,)
256 assert "incompatible function arguments" in str(excinfo.value)
258 with pytest.raises(RuntimeError)
as excinfo:
259 m.register_invalid_kw_only(m)
263 arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument
268 x = m.first_arg_kw_only(i=1)
272 m.first_arg_kw_only.__init__.__doc__
273 ==
"__init__(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: int = 0) -> None\n"
276 m.first_arg_kw_only.method.__doc__
277 ==
"method(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: int = 1, j: int = 2) -> None\n"
282 assert m.pos_only_all(1, 2) == (1, 2)
283 assert m.pos_only_all(2, 1) == (2, 1)
285 with pytest.raises(TypeError)
as excinfo:
286 m.pos_only_all(i=1, j=2)
287 assert "incompatible function arguments" in str(excinfo.value)
289 assert m.pos_only_mix(1, 2) == (1, 2)
290 assert m.pos_only_mix(2, j=1) == (2, 1)
292 with pytest.raises(TypeError)
as excinfo:
293 m.pos_only_mix(i=1, j=2)
294 assert "incompatible function arguments" in str(excinfo.value)
296 assert m.pos_kw_only_mix(1, 2, k=3) == (1, 2, 3)
297 assert m.pos_kw_only_mix(1, j=2, k=3) == (1, 2, 3)
299 with pytest.raises(TypeError)
as excinfo:
300 m.pos_kw_only_mix(i=1, j=2, k=3)
301 assert "incompatible function arguments" in str(excinfo.value)
303 with pytest.raises(TypeError)
as excinfo:
304 m.pos_kw_only_mix(1, 2, 3)
305 assert "incompatible function arguments" in str(excinfo.value)
307 with pytest.raises(TypeError)
as excinfo:
309 assert "incompatible function arguments" in str(excinfo.value)
311 assert m.pos_only_def_mix(1) == (1, 2, 3)
312 assert m.pos_only_def_mix(1, 4) == (1, 4, 3)
313 assert m.pos_only_def_mix(1, 4, 7) == (1, 4, 7)
314 assert m.pos_only_def_mix(1, 4, k=7) == (1, 4, 7)
316 with pytest.raises(TypeError)
as excinfo:
317 m.pos_only_def_mix(1, j=4)
318 assert "incompatible function arguments" in str(excinfo.value)
322 m.args_kwonly_full_monty.__doc__
323 ==
"args_kwonly_full_monty(arg0: int = 1, arg1: int = 2, /, j: float = 3.14159, *args, z: int = 42, **kwargs) -> tuple\n"
325 assert m.args_kwonly_full_monty() == (1, 2, 3.14159, (), 42, {})
326 assert m.args_kwonly_full_monty(8) == (8, 2, 3.14159, (), 42, {})
327 assert m.args_kwonly_full_monty(8, 9) == (8, 9, 3.14159, (), 42, {})
328 assert m.args_kwonly_full_monty(8, 9, 10) == (8, 9, 10.0, (), 42, {})
329 assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
340 assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
351 assert m.args_kwonly_full_monty(5, j=7, m=8, z=9) == (5, 2, 7.0, (), 9, {
"m": 8})
352 assert m.args_kwonly_full_monty(i=5, j=7, m=8, z=9) == (
365 m.first_arg_kw_only.pos_only.__doc__
366 ==
"pos_only(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, /, i: int, j: int) -> None\n"
371 assert m.kw_only_all.__doc__ ==
"kw_only_all(*, i: int, j: int) -> tuple\n"
372 assert m.kw_only_mixed.__doc__ ==
"kw_only_mixed(i: int, *, j: int) -> tuple\n"
373 assert m.pos_only_all.__doc__ ==
"pos_only_all(i: int, j: int, /) -> tuple\n"
374 assert m.pos_only_mix.__doc__ ==
"pos_only_mix(i: int, /, j: int) -> tuple\n"
376 m.pos_kw_only_mix.__doc__
377 ==
"pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple\n"
382 """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
384 refcount = m.arg_refcount_h
387 expected = refcount(myval)
388 assert m.arg_refcount_h(myval) == expected
389 assert m.arg_refcount_o(myval) == expected + 1
390 assert m.arg_refcount_h(myval) == expected
391 assert refcount(myval) == expected
393 assert m.mixed_plus_args(1, 2.0,
"a", myval) == (1, 2.0, (
"a", myval))
394 assert refcount(myval) == expected
396 assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {
"a": 1,
"b": myval})
397 assert refcount(myval) == expected
399 assert m.args_function(-1, myval) == (-1, myval)
400 assert refcount(myval) == expected
402 assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (
408 assert refcount(myval) == expected
410 assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == (
412 {
"a": 1,
"b": myval},
414 assert refcount(myval) == expected
416 exp3 = refcount(myval, myval, myval)
417 assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
418 assert refcount(myval) == expected
426 assert m.mixed_args_refcount(myval, myval, myval) == (exp3_3, exp3_3, exp3_3)
428 assert m.class_default_argument() ==
"<class 'decimal.Decimal'>"