test_kwargs_and_defaults.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import pytest
3 
4 import env # noqa: F401
5 
6 from pybind11_tests import kwargs_and_defaults as m
7 
8 
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"
18  assert doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
19  assert doc(m.KWClass.foo0) == \
20  "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
21  assert doc(m.KWClass.foo1) == \
22  "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
23 
24 
26  assert m.kw_func0(5, 10) == "x=5, y=10"
27 
28  assert m.kw_func1(5, 10) == "x=5, y=10"
29  assert m.kw_func1(5, y=10) == "x=5, y=10"
30  assert m.kw_func1(y=10, x=5) == "x=5, y=10"
31 
32  assert m.kw_func2() == "x=100, y=200"
33  assert m.kw_func2(5) == "x=5, y=200"
34  assert m.kw_func2(x=5) == "x=5, y=200"
35  assert m.kw_func2(y=10) == "x=100, y=10"
36  assert m.kw_func2(5, 10) == "x=5, y=10"
37  assert m.kw_func2(x=5, y=10) == "x=5, y=10"
38 
39  with pytest.raises(TypeError) as excinfo:
40  # noinspection PyArgumentList
41  m.kw_func2(x=5, y=10, z=12)
42  assert excinfo.match(
43  r'(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))' + '{3}$')
44 
45  assert m.kw_func4() == "{13 17}"
46  assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
47 
48  assert m.kw_func_udl(x=5, y=10) == "x=5, y=10"
49  assert m.kw_func_udl_z(x=5) == "x=5, y=0"
50 
51 
53  args = 'arg1_value', 'arg2_value', 3
54  assert m.args_function(*args) == args
55 
56  args = 'a1', 'a2'
57  kwargs = dict(arg3='a3', arg4=4)
58  assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
59 
60 
62  mpa = m.mixed_plus_args
63  mpk = m.mixed_plus_kwargs
64  mpak = m.mixed_plus_args_kwargs
65  mpakd = m.mixed_plus_args_kwargs_defaults
66 
67  assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
68  assert mpa(1, 2.5) == (1, 2.5, ())
69  with pytest.raises(TypeError) as excinfo:
70  assert mpa(1)
71  assert msg(excinfo.value) == """
72  mixed_plus_args(): incompatible function arguments. The following argument types are supported:
73  1. (arg0: int, arg1: float, *args) -> tuple
74 
75  Invoked with: 1
76  """ # noqa: E501 line too long
77  with pytest.raises(TypeError) as excinfo:
78  assert mpa()
79  assert msg(excinfo.value) == """
80  mixed_plus_args(): incompatible function arguments. The following argument types are supported:
81  1. (arg0: int, arg1: float, *args) -> tuple
82 
83  Invoked with:
84  """ # noqa: E501 line too long
85 
86  assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (-2, 3.5, {'e': 2.71828, 'pi': 3.14159})
87  assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
88  7, 7.7, (7.77, 7.777, 7.7777), {'minusseven': -7})
89  assert mpakd() == (1, 3.14159, (), {})
90  assert mpakd(3) == (3, 3.14159, (), {})
91  assert mpakd(j=2.71828) == (1, 2.71828, (), {})
92  assert mpakd(k=42) == (1, 3.14159, (), {'k': 42})
93  assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
94  1, 1, (2, 3, 5, 8), {'then': 13, 'followedby': 21})
95  # Arguments specified both positionally and via kwargs should fail:
96  with pytest.raises(TypeError) as excinfo:
97  assert mpakd(1, i=1)
98  assert msg(excinfo.value) == """
99  mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
100  1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
101 
102  Invoked with: 1; kwargs: i=1
103  """ # noqa: E501 line too long
104  with pytest.raises(TypeError) as excinfo:
105  assert mpakd(1, 2, j=1)
106  assert msg(excinfo.value) == """
107  mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
108  1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
109 
110  Invoked with: 1, 2; kwargs: j=1
111  """ # noqa: E501 line too long
112 
113 
115  assert m.kw_only_all(i=1, j=2) == (1, 2)
116  assert m.kw_only_all(j=1, i=2) == (2, 1)
117 
118  with pytest.raises(TypeError) as excinfo:
119  assert m.kw_only_all(i=1) == (1,)
120  assert "incompatible function arguments" in str(excinfo.value)
121 
122  with pytest.raises(TypeError) as excinfo:
123  assert m.kw_only_all(1, 2) == (1, 2)
124  assert "incompatible function arguments" in str(excinfo.value)
125 
126  assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
127 
128  assert m.kw_only_with_defaults(z=8) == (3, 4, 5, 8)
129  assert m.kw_only_with_defaults(2, z=8) == (2, 4, 5, 8)
130  assert m.kw_only_with_defaults(2, j=7, k=8, z=9) == (2, 7, 8, 9)
131  assert m.kw_only_with_defaults(2, 7, z=9, k=8) == (2, 7, 8, 9)
132 
133  assert m.kw_only_mixed(1, j=2) == (1, 2)
134  assert m.kw_only_mixed(j=2, i=3) == (3, 2)
135  assert m.kw_only_mixed(i=2, j=3) == (2, 3)
136 
137  assert m.kw_only_plus_more(4, 5, k=6, extra=7) == (4, 5, 6, {'extra': 7})
138  assert m.kw_only_plus_more(3, k=5, j=4, extra=6) == (3, 4, 5, {'extra': 6})
139  assert m.kw_only_plus_more(2, k=3, extra=4) == (2, -1, 3, {'extra': 4})
140 
141  with pytest.raises(TypeError) as excinfo:
142  assert m.kw_only_mixed(i=1) == (1,)
143  assert "incompatible function arguments" in str(excinfo.value)
144 
145  with pytest.raises(RuntimeError) as excinfo:
146  m.register_invalid_kw_only(m)
147  assert msg(excinfo.value) == """
148  arg(): cannot specify an unnamed argument after an kw_only() annotation
149  """
150 
151 
153  assert m.pos_only_all(1, 2) == (1, 2)
154  assert m.pos_only_all(2, 1) == (2, 1)
155 
156  with pytest.raises(TypeError) as excinfo:
157  m.pos_only_all(i=1, j=2)
158  assert "incompatible function arguments" in str(excinfo.value)
159 
160  assert m.pos_only_mix(1, 2) == (1, 2)
161  assert m.pos_only_mix(2, j=1) == (2, 1)
162 
163  with pytest.raises(TypeError) as excinfo:
164  m.pos_only_mix(i=1, j=2)
165  assert "incompatible function arguments" in str(excinfo.value)
166 
167  assert m.pos_kw_only_mix(1, 2, k=3) == (1, 2, 3)
168  assert m.pos_kw_only_mix(1, j=2, k=3) == (1, 2, 3)
169 
170  with pytest.raises(TypeError) as excinfo:
171  m.pos_kw_only_mix(i=1, j=2, k=3)
172  assert "incompatible function arguments" in str(excinfo.value)
173 
174  with pytest.raises(TypeError) as excinfo:
175  m.pos_kw_only_mix(1, 2, 3)
176  assert "incompatible function arguments" in str(excinfo.value)
177 
178  with pytest.raises(TypeError) as excinfo:
179  m.pos_only_def_mix()
180  assert "incompatible function arguments" in str(excinfo.value)
181 
182  assert m.pos_only_def_mix(1) == (1, 2, 3)
183  assert m.pos_only_def_mix(1, 4) == (1, 4, 3)
184  assert m.pos_only_def_mix(1, 4, 7) == (1, 4, 7)
185  assert m.pos_only_def_mix(1, 4, k=7) == (1, 4, 7)
186 
187  with pytest.raises(TypeError) as excinfo:
188  m.pos_only_def_mix(1, j=4)
189  assert "incompatible function arguments" in str(excinfo.value)
190 
191 
193  assert "kw_only_all(*, i: int, j: int) -> tuple\n" == m.kw_only_all.__doc__
194  assert "kw_only_mixed(i: int, *, j: int) -> tuple\n" == m.kw_only_mixed.__doc__
195  assert "pos_only_all(i: int, j: int, /) -> tuple\n" == m.pos_only_all.__doc__
196  assert "pos_only_mix(i: int, /, j: int) -> tuple\n" == m.pos_only_mix.__doc__
197  assert "pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple\n" == m.pos_kw_only_mix.__doc__
198 
199 
200 @pytest.mark.xfail("env.PYPY and env.PY2", reason="PyPy2 doesn't double count")
202  """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
203  arguments"""
204  refcount = m.arg_refcount_h
205 
206  myval = 54321
207  expected = refcount(myval)
208  assert m.arg_refcount_h(myval) == expected
209  assert m.arg_refcount_o(myval) == expected + 1
210  assert m.arg_refcount_h(myval) == expected
211  assert refcount(myval) == expected
212 
213  assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval))
214  assert refcount(myval) == expected
215 
216  assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {"a": 1, "b": myval})
217  assert refcount(myval) == expected
218 
219  assert m.args_function(-1, myval) == (-1, myval)
220  assert refcount(myval) == expected
221 
222  assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (5, 6.0, (myval,), {"a": myval})
223  assert refcount(myval) == expected
224 
225  assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == \
226  ((7, 8, myval), {"a": 1, "b": myval})
227  assert refcount(myval) == expected
228 
229  exp3 = refcount(myval, myval, myval)
230  assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
231  assert refcount(myval) == expected
232 
233  # This function takes the first arg as a `py::object` and the rest as a `py::args`. Unlike the
234  # previous case, when we have both positional and `py::args` we need to construct a new tuple
235  # for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
236  # tuple without having to inc_ref the individual elements, but here we can't, hence the extra
237  # refs.
238  assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)
239 
240  assert m.class_default_argument() == "<class 'decimal.Decimal'>"
Annotation for documentation.
Definition: attr.h:33
Definition: pytypes.h:928
Definition: pytypes.h:1255


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03