Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:54

0001 import pytest
0002 
0003 from pybind11_tests import kwargs_and_defaults as m
0004 
0005 
0006 def test_function_signatures(doc):
0007     assert doc(m.kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
0008     assert doc(m.kw_func1) == "kw_func1(x: int, y: int) -> str"
0009     assert doc(m.kw_func2) == "kw_func2(x: int = 100, y: int = 200) -> str"
0010     assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None"
0011     assert doc(m.kw_func4) == "kw_func4(myList: List[int] = [13, 17]) -> str"
0012     assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int = 300) -> str"
0013     assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int = 0) -> str"
0014     assert doc(m.args_function) == "args_function(*args) -> tuple"
0015     assert (
0016         doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
0017     )
0018     assert (
0019         doc(m.KWClass.foo0)
0020         == "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
0021     )
0022     assert (
0023         doc(m.KWClass.foo1)
0024         == "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
0025     )
0026 
0027 
0028 def test_named_arguments(msg):
0029     assert m.kw_func0(5, 10) == "x=5, y=10"
0030 
0031     assert m.kw_func1(5, 10) == "x=5, y=10"
0032     assert m.kw_func1(5, y=10) == "x=5, y=10"
0033     assert m.kw_func1(y=10, x=5) == "x=5, y=10"
0034 
0035     assert m.kw_func2() == "x=100, y=200"
0036     assert m.kw_func2(5) == "x=5, y=200"
0037     assert m.kw_func2(x=5) == "x=5, y=200"
0038     assert m.kw_func2(y=10) == "x=100, y=10"
0039     assert m.kw_func2(5, 10) == "x=5, y=10"
0040     assert m.kw_func2(x=5, y=10) == "x=5, y=10"
0041 
0042     with pytest.raises(TypeError) as excinfo:
0043         # noinspection PyArgumentList
0044         m.kw_func2(x=5, y=10, z=12)
0045     assert excinfo.match(
0046         r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))"
0047         + "{3}$"
0048     )
0049 
0050     assert m.kw_func4() == "{13 17}"
0051     assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
0052 
0053     assert m.kw_func_udl(x=5, y=10) == "x=5, y=10"
0054     assert m.kw_func_udl_z(x=5) == "x=5, y=0"
0055 
0056 
0057 def test_arg_and_kwargs():
0058     args = "arg1_value", "arg2_value", 3
0059     assert m.args_function(*args) == args
0060 
0061     args = "a1", "a2"
0062     kwargs = dict(arg3="a3", arg4=4)
0063     assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
0064 
0065 
0066 def test_mixed_args_and_kwargs(msg):
0067     mpa = m.mixed_plus_args
0068     mpk = m.mixed_plus_kwargs
0069     mpak = m.mixed_plus_args_kwargs
0070     mpakd = m.mixed_plus_args_kwargs_defaults
0071 
0072     assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
0073     assert mpa(1, 2.5) == (1, 2.5, ())
0074     with pytest.raises(TypeError) as excinfo:
0075         assert mpa(1)
0076     assert (
0077         msg(excinfo.value)
0078         == """
0079         mixed_plus_args(): incompatible function arguments. The following argument types are supported:
0080             1. (arg0: int, arg1: float, *args) -> tuple
0081 
0082         Invoked with: 1
0083     """
0084     )
0085     with pytest.raises(TypeError) as excinfo:
0086         assert mpa()
0087     assert (
0088         msg(excinfo.value)
0089         == """
0090         mixed_plus_args(): incompatible function arguments. The following argument types are supported:
0091             1. (arg0: int, arg1: float, *args) -> tuple
0092 
0093         Invoked with:
0094     """
0095     )
0096 
0097     assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
0098         -2,
0099         3.5,
0100         {"e": 2.71828, "pi": 3.14159},
0101     )
0102     assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
0103         7,
0104         7.7,
0105         (7.77, 7.777, 7.7777),
0106         {"minusseven": -7},
0107     )
0108     assert mpakd() == (1, 3.14159, (), {})
0109     assert mpakd(3) == (3, 3.14159, (), {})
0110     assert mpakd(j=2.71828) == (1, 2.71828, (), {})
0111     assert mpakd(k=42) == (1, 3.14159, (), {"k": 42})
0112     assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
0113         1,
0114         1,
0115         (2, 3, 5, 8),
0116         {"then": 13, "followedby": 21},
0117     )
0118     # Arguments specified both positionally and via kwargs should fail:
0119     with pytest.raises(TypeError) as excinfo:
0120         assert mpakd(1, i=1)
0121     assert (
0122         msg(excinfo.value)
0123         == """
0124         mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
0125             1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
0126 
0127         Invoked with: 1; kwargs: i=1
0128     """
0129     )
0130     with pytest.raises(TypeError) as excinfo:
0131         assert mpakd(1, 2, j=1)
0132     assert (
0133         msg(excinfo.value)
0134         == """
0135         mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
0136             1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
0137 
0138         Invoked with: 1, 2; kwargs: j=1
0139     """
0140     )
0141 
0142     # Arguments after a py::args are automatically keyword-only (pybind 2.9+)
0143     assert m.args_kwonly(2, 2.5, z=22) == (2, 2.5, (), 22)
0144     assert m.args_kwonly(2, 2.5, "a", "b", "c", z=22) == (2, 2.5, ("a", "b", "c"), 22)
0145     assert m.args_kwonly(z=22, i=4, j=16) == (4, 16, (), 22)
0146 
0147     with pytest.raises(TypeError) as excinfo:
0148         assert m.args_kwonly(2, 2.5, 22)  # missing z= keyword
0149     assert (
0150         msg(excinfo.value)
0151         == """
0152         args_kwonly(): incompatible function arguments. The following argument types are supported:
0153             1. (i: int, j: float, *args, z: int) -> tuple
0154 
0155         Invoked with: 2, 2.5, 22
0156     """
0157     )
0158 
0159     assert m.args_kwonly_kwargs(i=1, k=4, j=10, z=-1, y=9) == (
0160         1,
0161         10,
0162         (),
0163         -1,
0164         {"k": 4, "y": 9},
0165     )
0166     assert m.args_kwonly_kwargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, z=11, y=12) == (
0167         1,
0168         2,
0169         (3, 4, 5, 6, 7, 8, 9, 10),
0170         11,
0171         {"y": 12},
0172     )
0173     assert (
0174         m.args_kwonly_kwargs.__doc__
0175         == "args_kwonly_kwargs(i: int, j: float, *args, z: int, **kwargs) -> tuple\n"
0176     )
0177 
0178     assert (
0179         m.args_kwonly_kwargs_defaults.__doc__
0180         == "args_kwonly_kwargs_defaults(i: int = 1, j: float = 3.14159, *args, z: int = 42, **kwargs) -> tuple\n"  # noqa: E501 line too long
0181     )
0182     assert m.args_kwonly_kwargs_defaults() == (1, 3.14159, (), 42, {})
0183     assert m.args_kwonly_kwargs_defaults(2) == (2, 3.14159, (), 42, {})
0184     assert m.args_kwonly_kwargs_defaults(z=-99) == (1, 3.14159, (), -99, {})
0185     assert m.args_kwonly_kwargs_defaults(5, 6, 7, 8) == (5, 6, (7, 8), 42, {})
0186     assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8) == (5, 6, (7,), 42, {"m": 8})
0187     assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8, z=9) == (5, 6, (7,), 9, {"m": 8})
0188 
0189 
0190 def test_keyword_only_args(msg):
0191     assert m.kw_only_all(i=1, j=2) == (1, 2)
0192     assert m.kw_only_all(j=1, i=2) == (2, 1)
0193 
0194     with pytest.raises(TypeError) as excinfo:
0195         assert m.kw_only_all(i=1) == (1,)
0196     assert "incompatible function arguments" in str(excinfo.value)
0197 
0198     with pytest.raises(TypeError) as excinfo:
0199         assert m.kw_only_all(1, 2) == (1, 2)
0200     assert "incompatible function arguments" in str(excinfo.value)
0201 
0202     assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
0203 
0204     assert m.kw_only_with_defaults(z=8) == (3, 4, 5, 8)
0205     assert m.kw_only_with_defaults(2, z=8) == (2, 4, 5, 8)
0206     assert m.kw_only_with_defaults(2, j=7, k=8, z=9) == (2, 7, 8, 9)
0207     assert m.kw_only_with_defaults(2, 7, z=9, k=8) == (2, 7, 8, 9)
0208 
0209     assert m.kw_only_mixed(1, j=2) == (1, 2)
0210     assert m.kw_only_mixed(j=2, i=3) == (3, 2)
0211     assert m.kw_only_mixed(i=2, j=3) == (2, 3)
0212 
0213     assert m.kw_only_plus_more(4, 5, k=6, extra=7) == (4, 5, 6, {"extra": 7})
0214     assert m.kw_only_plus_more(3, k=5, j=4, extra=6) == (3, 4, 5, {"extra": 6})
0215     assert m.kw_only_plus_more(2, k=3, extra=4) == (2, -1, 3, {"extra": 4})
0216 
0217     with pytest.raises(TypeError) as excinfo:
0218         assert m.kw_only_mixed(i=1) == (1,)
0219     assert "incompatible function arguments" in str(excinfo.value)
0220 
0221     with pytest.raises(RuntimeError) as excinfo:
0222         m.register_invalid_kw_only(m)
0223     assert (
0224         msg(excinfo.value)
0225         == """
0226         arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument
0227     """
0228     )
0229 
0230     # https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
0231     x = m.first_arg_kw_only(i=1)
0232     x.method()
0233     x.method(i=1, j=2)
0234     assert (
0235         m.first_arg_kw_only.__init__.__doc__
0236         == "__init__(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: int = 0) -> None\n"  # noqa: E501 line too long
0237     )
0238     assert (
0239         m.first_arg_kw_only.method.__doc__
0240         == "method(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: int = 1, j: int = 2) -> None\n"  # noqa: E501 line too long
0241     )
0242 
0243 
0244 def test_positional_only_args(msg):
0245     assert m.pos_only_all(1, 2) == (1, 2)
0246     assert m.pos_only_all(2, 1) == (2, 1)
0247 
0248     with pytest.raises(TypeError) as excinfo:
0249         m.pos_only_all(i=1, j=2)
0250     assert "incompatible function arguments" in str(excinfo.value)
0251 
0252     assert m.pos_only_mix(1, 2) == (1, 2)
0253     assert m.pos_only_mix(2, j=1) == (2, 1)
0254 
0255     with pytest.raises(TypeError) as excinfo:
0256         m.pos_only_mix(i=1, j=2)
0257     assert "incompatible function arguments" in str(excinfo.value)
0258 
0259     assert m.pos_kw_only_mix(1, 2, k=3) == (1, 2, 3)
0260     assert m.pos_kw_only_mix(1, j=2, k=3) == (1, 2, 3)
0261 
0262     with pytest.raises(TypeError) as excinfo:
0263         m.pos_kw_only_mix(i=1, j=2, k=3)
0264     assert "incompatible function arguments" in str(excinfo.value)
0265 
0266     with pytest.raises(TypeError) as excinfo:
0267         m.pos_kw_only_mix(1, 2, 3)
0268     assert "incompatible function arguments" in str(excinfo.value)
0269 
0270     with pytest.raises(TypeError) as excinfo:
0271         m.pos_only_def_mix()
0272     assert "incompatible function arguments" in str(excinfo.value)
0273 
0274     assert m.pos_only_def_mix(1) == (1, 2, 3)
0275     assert m.pos_only_def_mix(1, 4) == (1, 4, 3)
0276     assert m.pos_only_def_mix(1, 4, 7) == (1, 4, 7)
0277     assert m.pos_only_def_mix(1, 4, k=7) == (1, 4, 7)
0278 
0279     with pytest.raises(TypeError) as excinfo:
0280         m.pos_only_def_mix(1, j=4)
0281     assert "incompatible function arguments" in str(excinfo.value)
0282 
0283     # Mix it with args and kwargs:
0284     assert (
0285         m.args_kwonly_full_monty.__doc__
0286         == "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
0287     )
0288     assert m.args_kwonly_full_monty() == (1, 2, 3.14159, (), 42, {})
0289     assert m.args_kwonly_full_monty(8) == (8, 2, 3.14159, (), 42, {})
0290     assert m.args_kwonly_full_monty(8, 9) == (8, 9, 3.14159, (), 42, {})
0291     assert m.args_kwonly_full_monty(8, 9, 10) == (8, 9, 10.0, (), 42, {})
0292     assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
0293         3,
0294         4,
0295         5.0,
0296         (
0297             6,
0298             7,
0299         ),
0300         9,
0301         {"m": 8},
0302     )
0303     assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
0304         3,
0305         4,
0306         5.0,
0307         (
0308             6,
0309             7,
0310         ),
0311         9,
0312         {"m": 8},
0313     )
0314     assert m.args_kwonly_full_monty(5, j=7, m=8, z=9) == (5, 2, 7.0, (), 9, {"m": 8})
0315     assert m.args_kwonly_full_monty(i=5, j=7, m=8, z=9) == (
0316         1,
0317         2,
0318         7.0,
0319         (),
0320         9,
0321         {"i": 5, "m": 8},
0322     )
0323 
0324     # pos_only at the beginning of the argument list was "broken" in how it was displayed (though
0325     # this is fairly useless in practice).  Related to:
0326     # https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
0327     assert (
0328         m.first_arg_kw_only.pos_only.__doc__
0329         == "pos_only(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, /, i: int, j: int) -> None\n"  # noqa: E501 line too long
0330     )
0331 
0332 
0333 def test_signatures():
0334     assert "kw_only_all(*, i: int, j: int) -> tuple\n" == m.kw_only_all.__doc__
0335     assert "kw_only_mixed(i: int, *, j: int) -> tuple\n" == m.kw_only_mixed.__doc__
0336     assert "pos_only_all(i: int, j: int, /) -> tuple\n" == m.pos_only_all.__doc__
0337     assert "pos_only_mix(i: int, /, j: int) -> tuple\n" == m.pos_only_mix.__doc__
0338     assert (
0339         "pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple\n"
0340         == m.pos_kw_only_mix.__doc__
0341     )
0342 
0343 
0344 def test_args_refcount():
0345     """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
0346     arguments"""
0347     refcount = m.arg_refcount_h
0348 
0349     myval = 54321
0350     expected = refcount(myval)
0351     assert m.arg_refcount_h(myval) == expected
0352     assert m.arg_refcount_o(myval) == expected + 1
0353     assert m.arg_refcount_h(myval) == expected
0354     assert refcount(myval) == expected
0355 
0356     assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval))
0357     assert refcount(myval) == expected
0358 
0359     assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {"a": 1, "b": myval})
0360     assert refcount(myval) == expected
0361 
0362     assert m.args_function(-1, myval) == (-1, myval)
0363     assert refcount(myval) == expected
0364 
0365     assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (
0366         5,
0367         6.0,
0368         (myval,),
0369         {"a": myval},
0370     )
0371     assert refcount(myval) == expected
0372 
0373     assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == (
0374         (7, 8, myval),
0375         {"a": 1, "b": myval},
0376     )
0377     assert refcount(myval) == expected
0378 
0379     exp3 = refcount(myval, myval, myval)
0380     assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
0381     assert refcount(myval) == expected
0382 
0383     # This function takes the first arg as a `py::object` and the rest as a `py::args`.  Unlike the
0384     # previous case, when we have both positional and `py::args` we need to construct a new tuple
0385     # for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
0386     # tuple without having to inc_ref the individual elements, but here we can't, hence the extra
0387     # refs.
0388     assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)
0389 
0390     assert m.class_default_argument() == "<class 'decimal.Decimal'>"