test_kwargs_and_defaults.py
Go to the documentation of this file.
1 import pytest
2 
3 from pybind11_tests import kwargs_and_defaults as m
4 
5 
7  assert doc(m.kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
8  assert doc(m.kw_func1) == "kw_func1(x: int, y: int) -> str"
9  assert doc(m.kw_func2) == "kw_func2(x: int = 100, y: int = 200) -> str"
10  assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None"
11  assert doc(m.kw_func4) == "kw_func4(myList: List[int] = [13, 17]) -> str"
12  assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int = 300) -> str"
13  assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int = 0) -> str"
14  assert doc(m.args_function) == "args_function(*args) -> tuple"
15  assert (
16  doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
17  )
18  assert (
19  doc(m.KWClass.foo0)
20  == "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
21  )
22  assert (
23  doc(m.KWClass.foo1)
24  == "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
25  )
26 
27 
29  assert m.kw_func0(5, 10) == "x=5, y=10"
30 
31  assert m.kw_func1(5, 10) == "x=5, y=10"
32  assert m.kw_func1(5, y=10) == "x=5, y=10"
33  assert m.kw_func1(y=10, x=5) == "x=5, y=10"
34 
35  assert m.kw_func2() == "x=100, y=200"
36  assert m.kw_func2(5) == "x=5, y=200"
37  assert m.kw_func2(x=5) == "x=5, y=200"
38  assert m.kw_func2(y=10) == "x=100, y=10"
39  assert m.kw_func2(5, 10) == "x=5, y=10"
40  assert m.kw_func2(x=5, y=10) == "x=5, y=10"
41 
42  with pytest.raises(TypeError) as excinfo:
43  # noinspection PyArgumentList
44  m.kw_func2(x=5, y=10, z=12)
45  assert excinfo.match(
46  r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))"
47  + "{3}$"
48  )
49 
50  assert m.kw_func4() == "{13 17}"
51  assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
52 
53  assert m.kw_func_udl(x=5, y=10) == "x=5, y=10"
54  assert m.kw_func_udl_z(x=5) == "x=5, y=0"
55 
56 
58  args = "arg1_value", "arg2_value", 3
59  assert m.args_function(*args) == args
60 
61  args = "a1", "a2"
62  kwargs = dict(arg3="a3", arg4=4)
63  assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
64 
65 
67  mpa = m.mixed_plus_args
68  mpk = m.mixed_plus_kwargs
69  mpak = m.mixed_plus_args_kwargs
70  mpakd = m.mixed_plus_args_kwargs_defaults
71 
72  assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
73  assert mpa(1, 2.5) == (1, 2.5, ())
74  with pytest.raises(TypeError) as excinfo:
75  assert mpa(1)
76  assert (
77  msg(excinfo.value)
78  == """
79  mixed_plus_args(): incompatible function arguments. The following argument types are supported:
80  1. (arg0: int, arg1: float, *args) -> tuple
81 
82  Invoked with: 1
83  """
84  )
85  with pytest.raises(TypeError) as excinfo:
86  assert mpa()
87  assert (
88  msg(excinfo.value)
89  == """
90  mixed_plus_args(): incompatible function arguments. The following argument types are supported:
91  1. (arg0: int, arg1: float, *args) -> tuple
92 
93  Invoked with:
94  """
95  )
96 
97  assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
98  -2,
99  3.5,
100  {"e": 2.71828, "pi": 3.14159},
101  )
102  assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
103  7,
104  7.7,
105  (7.77, 7.777, 7.7777),
106  {"minusseven": -7},
107  )
108  assert mpakd() == (1, 3.14159, (), {})
109  assert mpakd(3) == (3, 3.14159, (), {})
110  assert mpakd(j=2.71828) == (1, 2.71828, (), {})
111  assert mpakd(k=42) == (1, 3.14159, (), {"k": 42})
112  assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
113  1,
114  1,
115  (2, 3, 5, 8),
116  {"then": 13, "followedby": 21},
117  )
118  # Arguments specified both positionally and via kwargs should fail:
119  with pytest.raises(TypeError) as excinfo:
120  assert mpakd(1, i=1)
121  assert (
122  msg(excinfo.value)
123  == """
124  mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
125  1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
126 
127  Invoked with: 1; kwargs: i=1
128  """
129  )
130  with pytest.raises(TypeError) as excinfo:
131  assert mpakd(1, 2, j=1)
132  assert (
133  msg(excinfo.value)
134  == """
135  mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
136  1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
137 
138  Invoked with: 1, 2; kwargs: j=1
139  """
140  )
141 
142  # Arguments after a py::args are automatically keyword-only (pybind 2.9+)
143  assert m.args_kwonly(2, 2.5, z=22) == (2, 2.5, (), 22)
144  assert m.args_kwonly(2, 2.5, "a", "b", "c", z=22) == (2, 2.5, ("a", "b", "c"), 22)
145  assert m.args_kwonly(z=22, i=4, j=16) == (4, 16, (), 22)
146 
147  with pytest.raises(TypeError) as excinfo:
148  assert m.args_kwonly(2, 2.5, 22) # missing z= keyword
149  assert (
150  msg(excinfo.value)
151  == """
152  args_kwonly(): incompatible function arguments. The following argument types are supported:
153  1. (i: int, j: float, *args, z: int) -> tuple
154 
155  Invoked with: 2, 2.5, 22
156  """
157  )
158 
159  assert m.args_kwonly_kwargs(i=1, k=4, j=10, z=-1, y=9) == (
160  1,
161  10,
162  (),
163  -1,
164  {"k": 4, "y": 9},
165  )
166  assert m.args_kwonly_kwargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, z=11, y=12) == (
167  1,
168  2,
169  (3, 4, 5, 6, 7, 8, 9, 10),
170  11,
171  {"y": 12},
172  )
173  assert (
174  m.args_kwonly_kwargs.__doc__
175  == "args_kwonly_kwargs(i: int, j: float, *args, z: int, **kwargs) -> tuple\n"
176  )
177 
178  assert (
179  m.args_kwonly_kwargs_defaults.__doc__
180  == "args_kwonly_kwargs_defaults(i: int = 1, j: float = 3.14159, *args, z: int = 42, **kwargs) -> tuple\n" # noqa: E501 line too long
181  )
182  assert m.args_kwonly_kwargs_defaults() == (1, 3.14159, (), 42, {})
183  assert m.args_kwonly_kwargs_defaults(2) == (2, 3.14159, (), 42, {})
184  assert m.args_kwonly_kwargs_defaults(z=-99) == (1, 3.14159, (), -99, {})
185  assert m.args_kwonly_kwargs_defaults(5, 6, 7, 8) == (5, 6, (7, 8), 42, {})
186  assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8) == (5, 6, (7,), 42, {"m": 8})
187  assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8, z=9) == (5, 6, (7,), 9, {"m": 8})
188 
189 
191  assert m.kw_only_all(i=1, j=2) == (1, 2)
192  assert m.kw_only_all(j=1, i=2) == (2, 1)
193 
194  with pytest.raises(TypeError) as excinfo:
195  assert m.kw_only_all(i=1) == (1,)
196  assert "incompatible function arguments" in str(excinfo.value)
197 
198  with pytest.raises(TypeError) as excinfo:
199  assert m.kw_only_all(1, 2) == (1, 2)
200  assert "incompatible function arguments" in str(excinfo.value)
201 
202  assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
203 
204  assert m.kw_only_with_defaults(z=8) == (3, 4, 5, 8)
205  assert m.kw_only_with_defaults(2, z=8) == (2, 4, 5, 8)
206  assert m.kw_only_with_defaults(2, j=7, k=8, z=9) == (2, 7, 8, 9)
207  assert m.kw_only_with_defaults(2, 7, z=9, k=8) == (2, 7, 8, 9)
208 
209  assert m.kw_only_mixed(1, j=2) == (1, 2)
210  assert m.kw_only_mixed(j=2, i=3) == (3, 2)
211  assert m.kw_only_mixed(i=2, j=3) == (2, 3)
212 
213  assert m.kw_only_plus_more(4, 5, k=6, extra=7) == (4, 5, 6, {"extra": 7})
214  assert m.kw_only_plus_more(3, k=5, j=4, extra=6) == (3, 4, 5, {"extra": 6})
215  assert m.kw_only_plus_more(2, k=3, extra=4) == (2, -1, 3, {"extra": 4})
216 
217  with pytest.raises(TypeError) as excinfo:
218  assert m.kw_only_mixed(i=1) == (1,)
219  assert "incompatible function arguments" in str(excinfo.value)
220 
221  with pytest.raises(RuntimeError) as excinfo:
222  m.register_invalid_kw_only(m)
223  assert (
224  msg(excinfo.value)
225  == """
226  arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument
227  """
228  )
229 
230  # https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
231  x = m.first_arg_kw_only(i=1)
232  x.method()
233  x.method(i=1, j=2)
234  assert (
235  m.first_arg_kw_only.__init__.__doc__
236  == "__init__(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: int = 0) -> None\n" # noqa: E501 line too long
237  )
238  assert (
239  m.first_arg_kw_only.method.__doc__
240  == "method(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: int = 1, j: int = 2) -> None\n" # noqa: E501 line too long
241  )
242 
243 
245  assert m.pos_only_all(1, 2) == (1, 2)
246  assert m.pos_only_all(2, 1) == (2, 1)
247 
248  with pytest.raises(TypeError) as excinfo:
249  m.pos_only_all(i=1, j=2)
250  assert "incompatible function arguments" in str(excinfo.value)
251 
252  assert m.pos_only_mix(1, 2) == (1, 2)
253  assert m.pos_only_mix(2, j=1) == (2, 1)
254 
255  with pytest.raises(TypeError) as excinfo:
256  m.pos_only_mix(i=1, j=2)
257  assert "incompatible function arguments" in str(excinfo.value)
258 
259  assert m.pos_kw_only_mix(1, 2, k=3) == (1, 2, 3)
260  assert m.pos_kw_only_mix(1, j=2, k=3) == (1, 2, 3)
261 
262  with pytest.raises(TypeError) as excinfo:
263  m.pos_kw_only_mix(i=1, j=2, k=3)
264  assert "incompatible function arguments" in str(excinfo.value)
265 
266  with pytest.raises(TypeError) as excinfo:
267  m.pos_kw_only_mix(1, 2, 3)
268  assert "incompatible function arguments" in str(excinfo.value)
269 
270  with pytest.raises(TypeError) as excinfo:
271  m.pos_only_def_mix()
272  assert "incompatible function arguments" in str(excinfo.value)
273 
274  assert m.pos_only_def_mix(1) == (1, 2, 3)
275  assert m.pos_only_def_mix(1, 4) == (1, 4, 3)
276  assert m.pos_only_def_mix(1, 4, 7) == (1, 4, 7)
277  assert m.pos_only_def_mix(1, 4, k=7) == (1, 4, 7)
278 
279  with pytest.raises(TypeError) as excinfo:
280  m.pos_only_def_mix(1, j=4)
281  assert "incompatible function arguments" in str(excinfo.value)
282 
283  # Mix it with args and kwargs:
284  assert (
285  m.args_kwonly_full_monty.__doc__
286  == "args_kwonly_full_monty(arg0: int = 1, arg1: int = 2, /, j: float = 3.14159, *args, z: int = 42, **kwargs) -> tuple\n" # noqa: E501 line too long
287  )
288  assert m.args_kwonly_full_monty() == (1, 2, 3.14159, (), 42, {})
289  assert m.args_kwonly_full_monty(8) == (8, 2, 3.14159, (), 42, {})
290  assert m.args_kwonly_full_monty(8, 9) == (8, 9, 3.14159, (), 42, {})
291  assert m.args_kwonly_full_monty(8, 9, 10) == (8, 9, 10.0, (), 42, {})
292  assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
293  3,
294  4,
295  5.0,
296  (
297  6,
298  7,
299  ),
300  9,
301  {"m": 8},
302  )
303  assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
304  3,
305  4,
306  5.0,
307  (
308  6,
309  7,
310  ),
311  9,
312  {"m": 8},
313  )
314  assert m.args_kwonly_full_monty(5, j=7, m=8, z=9) == (5, 2, 7.0, (), 9, {"m": 8})
315  assert m.args_kwonly_full_monty(i=5, j=7, m=8, z=9) == (
316  1,
317  2,
318  7.0,
319  (),
320  9,
321  {"i": 5, "m": 8},
322  )
323 
324  # pos_only at the beginning of the argument list was "broken" in how it was displayed (though
325  # this is fairly useless in practice). Related to:
326  # https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
327  assert (
328  m.first_arg_kw_only.pos_only.__doc__
329  == "pos_only(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, /, i: int, j: int) -> None\n" # noqa: E501 line too long
330  )
331 
332 
334  assert "kw_only_all(*, i: int, j: int) -> tuple\n" == m.kw_only_all.__doc__
335  assert "kw_only_mixed(i: int, *, j: int) -> tuple\n" == m.kw_only_mixed.__doc__
336  assert "pos_only_all(i: int, j: int, /) -> tuple\n" == m.pos_only_all.__doc__
337  assert "pos_only_mix(i: int, /, j: int) -> tuple\n" == m.pos_only_mix.__doc__
338  assert (
339  "pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple\n"
340  == m.pos_kw_only_mix.__doc__
341  )
342 
343 
345  """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
346  arguments"""
347  refcount = m.arg_refcount_h
348 
349  myval = 54321
350  expected = refcount(myval)
351  assert m.arg_refcount_h(myval) == expected
352  assert m.arg_refcount_o(myval) == expected + 1
353  assert m.arg_refcount_h(myval) == expected
354  assert refcount(myval) == expected
355 
356  assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval))
357  assert refcount(myval) == expected
358 
359  assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {"a": 1, "b": myval})
360  assert refcount(myval) == expected
361 
362  assert m.args_function(-1, myval) == (-1, myval)
363  assert refcount(myval) == expected
364 
365  assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (
366  5,
367  6.0,
368  (myval,),
369  {"a": myval},
370  )
371  assert refcount(myval) == expected
372 
373  assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == (
374  (7, 8, myval),
375  {"a": 1, "b": myval},
376  )
377  assert refcount(myval) == expected
378 
379  exp3 = refcount(myval, myval, myval)
380  assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
381  assert refcount(myval) == expected
382 
383  # This function takes the first arg as a `py::object` and the rest as a `py::args`. Unlike the
384  # previous case, when we have both positional and `py::args` we need to construct a new tuple
385  # for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
386  # tuple without having to inc_ref the individual elements, but here we can't, hence the extra
387  # refs.
388  assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)
389 
390  assert m.class_default_argument() == "<class 'decimal.Decimal'>"
Annotation for documentation.
Definition: attr.h:42
Definition: pytypes.h:1403
Definition: pytypes.h:1924


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46