• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2import pytest
3
4import env  # noqa: F401
5
6from pybind11_tests import kwargs_and_defaults as m
7
8
9def test_function_signatures(doc):
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 (
19        doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
20    )
21    assert (
22        doc(m.KWClass.foo0)
23        == "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
24    )
25    assert (
26        doc(m.KWClass.foo1)
27        == "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
28    )
29
30
31def test_named_arguments(msg):
32    assert m.kw_func0(5, 10) == "x=5, y=10"
33
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"
37
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"
44
45    with pytest.raises(TypeError) as excinfo:
46        # noinspection PyArgumentList
47        m.kw_func2(x=5, y=10, z=12)
48    assert excinfo.match(
49        r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))"
50        + "{3}$"
51    )
52
53    assert m.kw_func4() == "{13 17}"
54    assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
55
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"
58
59
60def test_arg_and_kwargs():
61    args = "arg1_value", "arg2_value", 3
62    assert m.args_function(*args) == args
63
64    args = "a1", "a2"
65    kwargs = dict(arg3="a3", arg4=4)
66    assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
67
68
69def test_mixed_args_and_kwargs(msg):
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
74
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:
78        assert mpa(1)
79    assert (
80        msg(excinfo.value)
81        == """
82        mixed_plus_args(): incompatible function arguments. The following argument types are supported:
83            1. (arg0: int, arg1: float, *args) -> tuple
84
85        Invoked with: 1
86    """  # noqa: E501 line too long
87    )
88    with pytest.raises(TypeError) as excinfo:
89        assert mpa()
90    assert (
91        msg(excinfo.value)
92        == """
93        mixed_plus_args(): incompatible function arguments. The following argument types are supported:
94            1. (arg0: int, arg1: float, *args) -> tuple
95
96        Invoked with:
97    """  # noqa: E501 line too long
98    )
99
100    assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
101        -2,
102        3.5,
103        {"e": 2.71828, "pi": 3.14159},
104    )
105    assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
106        7,
107        7.7,
108        (7.77, 7.777, 7.7777),
109        {"minusseven": -7},
110    )
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) == (
116        1,
117        1,
118        (2, 3, 5, 8),
119        {"then": 13, "followedby": 21},
120    )
121    # Arguments specified both positionally and via kwargs should fail:
122    with pytest.raises(TypeError) as excinfo:
123        assert mpakd(1, i=1)
124    assert (
125        msg(excinfo.value)
126        == """
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
129
130        Invoked with: 1; kwargs: i=1
131    """  # noqa: E501 line too long
132    )
133    with pytest.raises(TypeError) as excinfo:
134        assert mpakd(1, 2, j=1)
135    assert (
136        msg(excinfo.value)
137        == """
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
140
141        Invoked with: 1, 2; kwargs: j=1
142    """  # noqa: E501 line too long
143    )
144
145
146def test_keyword_only_args(msg):
147    assert m.kw_only_all(i=1, j=2) == (1, 2)
148    assert m.kw_only_all(j=1, i=2) == (2, 1)
149
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)
153
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)
157
158    assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
159
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)
164
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)
168
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})
172
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)
176
177    with pytest.raises(RuntimeError) as excinfo:
178        m.register_invalid_kw_only(m)
179    assert (
180        msg(excinfo.value)
181        == """
182        arg(): cannot specify an unnamed argument after an kw_only() annotation
183    """
184    )
185
186
187def test_positional_only_args(msg):
188    assert m.pos_only_all(1, 2) == (1, 2)
189    assert m.pos_only_all(2, 1) == (2, 1)
190
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)
194
195    assert m.pos_only_mix(1, 2) == (1, 2)
196    assert m.pos_only_mix(2, j=1) == (2, 1)
197
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)
201
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)
204
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)
208
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)
212
213    with pytest.raises(TypeError) as excinfo:
214        m.pos_only_def_mix()
215    assert "incompatible function arguments" in str(excinfo.value)
216
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)
221
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)
225
226
227def test_signatures():
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__
232    assert (
233        "pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple\n"
234        == m.pos_kw_only_mix.__doc__
235    )
236
237
238@pytest.mark.xfail("env.PYPY and env.PY2", reason="PyPy2 doesn't double count")
239def test_args_refcount():
240    """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
241    arguments"""
242    refcount = m.arg_refcount_h
243
244    myval = 54321
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
250
251    assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval))
252    assert refcount(myval) == expected
253
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
256
257    assert m.args_function(-1, myval) == (-1, myval)
258    assert refcount(myval) == expected
259
260    assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (
261        5,
262        6.0,
263        (myval,),
264        {"a": myval},
265    )
266    assert refcount(myval) == expected
267
268    assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == (
269        (7, 8, myval),
270        {"a": 1, "b": myval},
271    )
272    assert refcount(myval) == expected
273
274    exp3 = refcount(myval, myval, myval)
275    assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
276    assert refcount(myval) == expected
277
278    # This function takes the first arg as a `py::object` and the rest as a `py::args`.  Unlike the
279    # previous case, when we have both positional and `py::args` we need to construct a new tuple
280    # for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
281    # tuple without having to inc_ref the individual elements, but here we can't, hence the extra
282    # refs.
283    assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)
284
285    assert m.class_default_argument() == "<class 'decimal.Decimal'>"
286