test_kwargs_and_defaults.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import pytest
4 
5 from pybind11_tests import kwargs_and_defaults as m
6 
7 
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"
17  assert (
18  doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
19  )
20  assert (
21  doc(m.KWClass.foo0)
22  == "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
23  )
24  assert (
25  doc(m.KWClass.foo1)
26  == "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
27  )
28  assert (
29  doc(m.kw_lb_func0)
30  == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
31  )
32  assert (
33  doc(m.kw_lb_func1)
34  == "kw_lb_func1(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
35  )
36  assert (
37  doc(m.kw_lb_func2)
38  == "kw_lb_func2(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
39  )
40  assert (
41  doc(m.kw_lb_func3)
42  == "kw_lb_func3(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
43  )
44  assert (
45  doc(m.kw_lb_func4)
46  == "kw_lb_func4(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
47  )
48  assert (
49  doc(m.kw_lb_func5)
50  == "kw_lb_func5(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
51  )
52  assert (
53  doc(m.kw_lb_func6)
54  == "kw_lb_func6(custom: m.kwargs_and_defaults.CustomRepr = ) -> None"
55  )
56  assert (
57  doc(m.kw_lb_func7)
58  == "kw_lb_func7(str_arg: str = 'First line.\\n Second line.') -> None"
59  )
60  assert (
61  doc(m.kw_lb_func8)
62  == "kw_lb_func8(custom: m.kwargs_and_defaults.CustomRepr = ) -> None"
63  )
64 
65 
67  assert m.kw_func0(5, 10) == "x=5, y=10"
68 
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"
72 
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"
79 
80  with pytest.raises(TypeError) as excinfo:
81  # noinspection PyArgumentList
82  m.kw_func2(x=5, y=10, z=12)
83  assert excinfo.match(
84  r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$)){3}$"
85  )
86 
87  assert m.kw_func4() == "{13 17}"
88  assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
89 
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"
92 
93 
95  args = "arg1_value", "arg2_value", 3
96  assert m.args_function(*args) == args
97 
98  args = "a1", "a2"
99  kwargs = {"arg3": "a3", "arg4": 4}
100  assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
101 
102 
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
108 
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:
112  assert mpa(1)
113  assert (
114  msg(excinfo.value)
115  == """
116  mixed_plus_args(): incompatible function arguments. The following argument types are supported:
117  1. (arg0: int, arg1: float, *args) -> tuple
118 
119  Invoked with: 1
120  """
121  )
122  with pytest.raises(TypeError) as excinfo:
123  assert mpa()
124  assert (
125  msg(excinfo.value)
126  == """
127  mixed_plus_args(): incompatible function arguments. The following argument types are supported:
128  1. (arg0: int, arg1: float, *args) -> tuple
129 
130  Invoked with:
131  """
132  )
133 
134  assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
135  -2,
136  3.5,
137  {"e": 2.71828, "pi": 3.14159},
138  )
139  assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
140  7,
141  7.7,
142  (7.77, 7.777, 7.7777),
143  {"minusseven": -7},
144  )
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) == (
150  1,
151  1,
152  (2, 3, 5, 8),
153  {"then": 13, "followedby": 21},
154  )
155  # Arguments specified both positionally and via kwargs should fail:
156  with pytest.raises(TypeError) as excinfo:
157  assert mpakd(1, i=1)
158  assert (
159  msg(excinfo.value)
160  == """
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
163 
164  Invoked with: 1; kwargs: i=1
165  """
166  )
167  with pytest.raises(TypeError) as excinfo:
168  assert mpakd(1, 2, j=1)
169  assert (
170  msg(excinfo.value)
171  == """
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
174 
175  Invoked with: 1, 2; kwargs: j=1
176  """
177  )
178 
179  # Arguments after a py::args are automatically keyword-only (pybind 2.9+)
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)
183 
184  with pytest.raises(TypeError) as excinfo:
185  assert m.args_kwonly(2, 2.5, 22) # missing z= keyword
186  assert (
187  msg(excinfo.value)
188  == """
189  args_kwonly(): incompatible function arguments. The following argument types are supported:
190  1. (i: int, j: float, *args, z: int) -> tuple
191 
192  Invoked with: 2, 2.5, 22
193  """
194  )
195 
196  assert m.args_kwonly_kwargs(i=1, k=4, j=10, z=-1, y=9) == (
197  1,
198  10,
199  (),
200  -1,
201  {"k": 4, "y": 9},
202  )
203  assert m.args_kwonly_kwargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, z=11, y=12) == (
204  1,
205  2,
206  (3, 4, 5, 6, 7, 8, 9, 10),
207  11,
208  {"y": 12},
209  )
210  assert (
211  m.args_kwonly_kwargs.__doc__
212  == "args_kwonly_kwargs(i: int, j: float, *args, z: int, **kwargs) -> tuple\n"
213  )
214 
215  assert (
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"
218  )
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})
225 
226 
228  assert m.kw_only_all(i=1, j=2) == (1, 2)
229  assert m.kw_only_all(j=1, i=2) == (2, 1)
230 
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)
234 
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)
238 
239  assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
240 
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)
245 
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)
249 
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})
253 
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)
257 
258  with pytest.raises(RuntimeError) as excinfo:
259  m.register_invalid_kw_only(m)
260  assert (
261  msg(excinfo.value)
262  == """
263  arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument
264  """
265  )
266 
267  # https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
268  x = m.first_arg_kw_only(i=1)
269  x.method()
270  x.method(i=1, j=2)
271  assert (
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"
274  )
275  assert (
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"
278  )
279 
280 
282  assert m.pos_only_all(1, 2) == (1, 2)
283  assert m.pos_only_all(2, 1) == (2, 1)
284 
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)
288 
289  assert m.pos_only_mix(1, 2) == (1, 2)
290  assert m.pos_only_mix(2, j=1) == (2, 1)
291 
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)
295 
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)
298 
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)
302 
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)
306 
307  with pytest.raises(TypeError) as excinfo:
308  m.pos_only_def_mix()
309  assert "incompatible function arguments" in str(excinfo.value)
310 
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)
315 
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)
319 
320  # Mix it with args and kwargs:
321  assert (
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"
324  )
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) == (
330  3,
331  4,
332  5.0,
333  (
334  6,
335  7,
336  ),
337  9,
338  {"m": 8},
339  )
340  assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
341  3,
342  4,
343  5.0,
344  (
345  6,
346  7,
347  ),
348  9,
349  {"m": 8},
350  )
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) == (
353  1,
354  2,
355  7.0,
356  (),
357  9,
358  {"i": 5, "m": 8},
359  )
360 
361  # pos_only at the beginning of the argument list was "broken" in how it was displayed (though
362  # this is fairly useless in practice). Related to:
363  # https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
364  assert (
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"
367  )
368 
369 
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"
375  assert (
376  m.pos_kw_only_mix.__doc__
377  == "pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple\n"
378  )
379 
380 
382  """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
383  arguments"""
384  refcount = m.arg_refcount_h
385 
386  myval = object()
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
392 
393  assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval))
394  assert refcount(myval) == expected
395 
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
398 
399  assert m.args_function(-1, myval) == (-1, myval)
400  assert refcount(myval) == expected
401 
402  assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (
403  5,
404  6.0,
405  (myval,),
406  {"a": myval},
407  )
408  assert refcount(myval) == expected
409 
410  assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == (
411  (7, 8, myval),
412  {"a": 1, "b": myval},
413  )
414  assert refcount(myval) == expected
415 
416  exp3 = refcount(myval, myval, myval)
417  assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
418  assert refcount(myval) == expected
419 
420  # This function takes the first arg as a `py::object` and the rest as a `py::args`. Unlike the
421  # previous case, when we have both positional and `py::args` we need to construct a new tuple
422  # for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
423  # tuple without having to inc_ref the individual elements, but here we can't, hence the extra
424  # refs.
425  exp3_3 = exp3 + 3
426  assert m.mixed_args_refcount(myval, myval, myval) == (exp3_3, exp3_3, exp3_3)
427 
428  assert m.class_default_argument() == "<class 'decimal.Decimal'>"
test_kwargs_and_defaults.test_named_arguments
def test_named_arguments()
Definition: test_kwargs_and_defaults.py:66
test_kwargs_and_defaults.test_arg_and_kwargs
def test_arg_and_kwargs()
Definition: test_kwargs_and_defaults.py:94
test_kwargs_and_defaults.test_signatures
def test_signatures()
Definition: test_kwargs_and_defaults.py:370
object
Definition: pytypes.h:364
doc
Annotation for documentation.
Definition: attr.h:45
test_kwargs_and_defaults.test_mixed_args_and_kwargs
def test_mixed_args_and_kwargs(msg)
Definition: test_kwargs_and_defaults.py:103
test_kwargs_and_defaults.test_keyword_only_args
def test_keyword_only_args(msg)
Definition: test_kwargs_and_defaults.py:227
str
Definition: pytypes.h:1558
test_kwargs_and_defaults.test_positional_only_args
def test_positional_only_args()
Definition: test_kwargs_and_defaults.py:281
test_kwargs_and_defaults.test_args_refcount
def test_args_refcount()
Definition: test_kwargs_and_defaults.py:381
pybind11.msg
msg
Definition: wrap/pybind11/pybind11/__init__.py:6
test_kwargs_and_defaults.test_function_signatures
def test_function_signatures(doc)
Definition: test_kwargs_and_defaults.py:8


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:01