2024-06-22 04:55:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
import pytest
|
2020-08-16 20:02:12 +00:00
|
|
|
|
2024-10-07 21:12:04 +00:00
|
|
|
import env # noqa: F401
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
from pybind11_tests import kwargs_and_defaults as m
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_function_signatures(doc):
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert doc(m.kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
|
|
|
|
assert doc(m.kw_func1) == "kw_func1(x: int, y: int) -> str"
|
2017-11-27 04:00:35 +00:00
|
|
|
assert doc(m.kw_func2) == "kw_func2(x: int = 100, y: int = 200) -> str"
|
|
|
|
assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None"
|
2023-09-12 19:46:58 +00:00
|
|
|
assert doc(m.kw_func4) == "kw_func4(myList: list[int] = [13, 17]) -> str"
|
2017-11-27 04:00:35 +00:00
|
|
|
assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int = 300) -> str"
|
|
|
|
assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int = 0) -> str"
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert doc(m.args_function) == "args_function(*args) -> tuple"
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
|
|
|
|
)
|
2024-09-24 17:28:22 +00:00
|
|
|
assert (
|
|
|
|
doc(m.args_kwargs_subclass_function)
|
|
|
|
== "args_kwargs_subclass_function(*Args, **KWArgs) -> tuple"
|
|
|
|
)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
doc(m.KWClass.foo0)
|
|
|
|
== "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.KWClass.foo1)
|
|
|
|
== "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
|
|
|
|
)
|
2023-08-15 14:48:59 +00:00
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func0)
|
|
|
|
== "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func1)
|
|
|
|
== "kw_lb_func1(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func2)
|
|
|
|
== "kw_lb_func2(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func3)
|
|
|
|
== "kw_lb_func3(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func4)
|
|
|
|
== "kw_lb_func4(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func5)
|
|
|
|
== "kw_lb_func5(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func6)
|
|
|
|
== "kw_lb_func6(custom: m.kwargs_and_defaults.CustomRepr = ) -> None"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func7)
|
|
|
|
== "kw_lb_func7(str_arg: str = 'First line.\\n Second line.') -> None"
|
|
|
|
)
|
2023-09-14 16:47:34 +00:00
|
|
|
assert (
|
|
|
|
doc(m.kw_lb_func8)
|
|
|
|
== "kw_lb_func8(custom: m.kwargs_and_defaults.CustomRepr = ) -> None"
|
|
|
|
)
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
2023-02-22 14:18:55 +00:00
|
|
|
def test_named_arguments():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.kw_func0(5, 10) == "x=5, y=10"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.kw_func1(5, 10) == "x=5, y=10"
|
|
|
|
assert m.kw_func1(5, y=10) == "x=5, y=10"
|
|
|
|
assert m.kw_func1(y=10, x=5) == "x=5, y=10"
|
2016-08-12 20:28:31 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.kw_func2() == "x=100, y=200"
|
|
|
|
assert m.kw_func2(5) == "x=5, y=200"
|
|
|
|
assert m.kw_func2(x=5) == "x=5, y=200"
|
|
|
|
assert m.kw_func2(y=10) == "x=100, y=10"
|
|
|
|
assert m.kw_func2(5, 10) == "x=5, y=10"
|
|
|
|
assert m.kw_func2(x=5, y=10) == "x=5, y=10"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
# noinspection PyArgumentList
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
m.kw_func2(x=5, y=10, z=12)
|
2017-03-10 14:42:42 +00:00
|
|
|
assert excinfo.match(
|
2023-02-22 14:18:55 +00:00
|
|
|
r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$)){3}$"
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2016-08-12 11:50:00 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.kw_func4() == "{13 17}"
|
|
|
|
assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.kw_func_udl(x=5, y=10) == "x=5, y=10"
|
|
|
|
assert m.kw_func_udl_z(x=5) == "x=5, y=0"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
2016-08-19 11:45:36 +00:00
|
|
|
def test_arg_and_kwargs():
|
2020-10-16 20:38:13 +00:00
|
|
|
args = "arg1_value", "arg2_value", 3
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.args_function(*args) == args
|
2016-08-19 11:45:36 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
args = "a1", "a2"
|
2023-02-22 14:18:55 +00:00
|
|
|
kwargs = {"arg3": "a3", "arg4": 4}
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
|
2024-09-24 17:28:22 +00:00
|
|
|
assert m.args_kwargs_subclass_function(*args, **kwargs) == (args, kwargs)
|
2017-01-22 04:42:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_mixed_args_and_kwargs(msg):
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
mpa = m.mixed_plus_args
|
|
|
|
mpk = m.mixed_plus_kwargs
|
|
|
|
mpak = m.mixed_plus_args_kwargs
|
|
|
|
mpakd = m.mixed_plus_args_kwargs_defaults
|
2017-01-22 04:42:14 +00:00
|
|
|
|
|
|
|
assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
|
|
|
|
assert mpa(1, 2.5) == (1, 2.5, ())
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
assert mpa(1)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
2017-01-22 04:42:14 +00:00
|
|
|
mixed_plus_args(): incompatible function arguments. The following argument types are supported:
|
|
|
|
1. (arg0: int, arg1: float, *args) -> tuple
|
|
|
|
|
|
|
|
Invoked with: 1
|
2022-02-11 02:28:08 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-01-22 04:42:14 +00:00
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
assert mpa()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
2017-01-22 04:42:14 +00:00
|
|
|
mixed_plus_args(): incompatible function arguments. The following argument types are supported:
|
|
|
|
1. (arg0: int, arg1: float, *args) -> tuple
|
|
|
|
|
|
|
|
Invoked with:
|
2022-02-11 02:28:08 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-01-22 04:42:14 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (
|
|
|
|
-2,
|
|
|
|
3.5,
|
|
|
|
{"e": 2.71828, "pi": 3.14159},
|
|
|
|
)
|
2017-01-22 04:42:14 +00:00
|
|
|
assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
|
2020-10-16 20:38:13 +00:00
|
|
|
7,
|
|
|
|
7.7,
|
|
|
|
(7.77, 7.777, 7.7777),
|
|
|
|
{"minusseven": -7},
|
|
|
|
)
|
2017-01-22 04:42:14 +00:00
|
|
|
assert mpakd() == (1, 3.14159, (), {})
|
|
|
|
assert mpakd(3) == (3, 3.14159, (), {})
|
|
|
|
assert mpakd(j=2.71828) == (1, 2.71828, (), {})
|
2020-10-16 20:38:13 +00:00
|
|
|
assert mpakd(k=42) == (1, 3.14159, (), {"k": 42})
|
2017-01-22 04:42:14 +00:00
|
|
|
assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
|
2020-10-16 20:38:13 +00:00
|
|
|
1,
|
|
|
|
1,
|
|
|
|
(2, 3, 5, 8),
|
|
|
|
{"then": 13, "followedby": 21},
|
|
|
|
)
|
2017-02-24 02:04:46 +00:00
|
|
|
# Arguments specified both positionally and via kwargs should fail:
|
2017-01-22 04:42:14 +00:00
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
assert mpakd(1, i=1)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
2017-02-24 02:04:46 +00:00
|
|
|
mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
|
2017-11-27 04:00:35 +00:00
|
|
|
1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
|
2017-02-24 02:04:46 +00:00
|
|
|
|
|
|
|
Invoked with: 1; kwargs: i=1
|
2022-02-11 02:28:08 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-01-22 04:42:14 +00:00
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
assert mpakd(1, 2, j=1)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
2017-02-24 02:04:46 +00:00
|
|
|
mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
|
2017-11-27 04:00:35 +00:00
|
|
|
1. (i: int = 1, j: float = 3.14159, *args, **kwargs) -> tuple
|
2017-02-24 02:04:46 +00:00
|
|
|
|
|
|
|
Invoked with: 1, 2; kwargs: j=1
|
2022-02-11 02:28:08 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-12-15 17:22:15 +00:00
|
|
|
|
feat: allow kw-only args after a py::args (#3402)
* Simply has_kw_only_args handling
This simplifies tracking the number of kw-only args by instead tracking
the number of positional arguments (which is really what we care about
everywhere this is used).
* Allow keyword-only arguments to follow py::args
This removes the constraint that py::args has to be last (or
second-last, with py::kwargs) and instead makes py::args imply
py::kw_only for any remaining arguments, allowing you to bind a function
that works the same way as a Python function such as:
def f(a, *args, b):
return a * b + sum(args)
f(10, 1, 2, 3, b=20) # == 206
With this change, you can bind such a function using:
m.def("f", [](int a, py::args args, int b) { /* ... */ },
"a"_a, "b"_a);
Or, to be more explicit about the keyword-only arguments:
m.def("g", [](int a, py::args args, int b) { /* ... */ },
"a"_a, py::kw_only{}, "b"_a);
(The only difference between the two is that the latter will fail at
binding time if the `kw_only{}` doesn't match the `py::args` position).
This doesn't affect backwards compatibility at all because, currently,
you can't have a py::args anywhere except the end/2nd-last.
* Take args/kwargs by const lvalue ref
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-29 03:16:55 +00:00
|
|
|
# Arguments after a py::args are automatically keyword-only (pybind 2.9+)
|
|
|
|
assert m.args_kwonly(2, 2.5, z=22) == (2, 2.5, (), 22)
|
|
|
|
assert m.args_kwonly(2, 2.5, "a", "b", "c", z=22) == (2, 2.5, ("a", "b", "c"), 22)
|
|
|
|
assert m.args_kwonly(z=22, i=4, j=16) == (4, 16, (), 22)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
assert m.args_kwonly(2, 2.5, 22) # missing z= keyword
|
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
|
|
|
args_kwonly(): incompatible function arguments. The following argument types are supported:
|
|
|
|
1. (i: int, j: float, *args, z: int) -> tuple
|
|
|
|
|
|
|
|
Invoked with: 2, 2.5, 22
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
assert m.args_kwonly_kwargs(i=1, k=4, j=10, z=-1, y=9) == (
|
|
|
|
1,
|
|
|
|
10,
|
|
|
|
(),
|
|
|
|
-1,
|
|
|
|
{"k": 4, "y": 9},
|
|
|
|
)
|
|
|
|
assert m.args_kwonly_kwargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, z=11, y=12) == (
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
(3, 4, 5, 6, 7, 8, 9, 10),
|
|
|
|
11,
|
|
|
|
{"y": 12},
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
m.args_kwonly_kwargs.__doc__
|
|
|
|
== "args_kwonly_kwargs(i: int, j: float, *args, z: int, **kwargs) -> tuple\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
m.args_kwonly_kwargs_defaults.__doc__
|
2023-02-22 14:18:55 +00:00
|
|
|
== "args_kwonly_kwargs_defaults(i: int = 1, j: float = 3.14159, *args, z: int = 42, **kwargs) -> tuple\n"
|
feat: allow kw-only args after a py::args (#3402)
* Simply has_kw_only_args handling
This simplifies tracking the number of kw-only args by instead tracking
the number of positional arguments (which is really what we care about
everywhere this is used).
* Allow keyword-only arguments to follow py::args
This removes the constraint that py::args has to be last (or
second-last, with py::kwargs) and instead makes py::args imply
py::kw_only for any remaining arguments, allowing you to bind a function
that works the same way as a Python function such as:
def f(a, *args, b):
return a * b + sum(args)
f(10, 1, 2, 3, b=20) # == 206
With this change, you can bind such a function using:
m.def("f", [](int a, py::args args, int b) { /* ... */ },
"a"_a, "b"_a);
Or, to be more explicit about the keyword-only arguments:
m.def("g", [](int a, py::args args, int b) { /* ... */ },
"a"_a, py::kw_only{}, "b"_a);
(The only difference between the two is that the latter will fail at
binding time if the `kw_only{}` doesn't match the `py::args` position).
This doesn't affect backwards compatibility at all because, currently,
you can't have a py::args anywhere except the end/2nd-last.
* Take args/kwargs by const lvalue ref
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-29 03:16:55 +00:00
|
|
|
)
|
|
|
|
assert m.args_kwonly_kwargs_defaults() == (1, 3.14159, (), 42, {})
|
|
|
|
assert m.args_kwonly_kwargs_defaults(2) == (2, 3.14159, (), 42, {})
|
|
|
|
assert m.args_kwonly_kwargs_defaults(z=-99) == (1, 3.14159, (), -99, {})
|
|
|
|
assert m.args_kwonly_kwargs_defaults(5, 6, 7, 8) == (5, 6, (7, 8), 42, {})
|
|
|
|
assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8) == (5, 6, (7,), 42, {"m": 8})
|
|
|
|
assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8, z=9) == (5, 6, (7,), 9, {"m": 8})
|
|
|
|
|
2017-12-15 17:22:15 +00:00
|
|
|
|
2017-12-23 22:56:07 +00:00
|
|
|
def test_keyword_only_args(msg):
|
2020-09-05 00:02:05 +00:00
|
|
|
assert m.kw_only_all(i=1, j=2) == (1, 2)
|
|
|
|
assert m.kw_only_all(j=1, i=2) == (2, 1)
|
2017-12-23 22:56:07 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
2020-09-05 00:02:05 +00:00
|
|
|
assert m.kw_only_all(i=1) == (1,)
|
2017-12-23 22:56:07 +00:00
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
2020-09-05 00:02:05 +00:00
|
|
|
assert m.kw_only_all(1, 2) == (1, 2)
|
2017-12-23 22:56:07 +00:00
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
2020-09-05 00:02:05 +00:00
|
|
|
assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3)
|
2017-12-23 22:56:07 +00:00
|
|
|
|
2020-09-05 00:02:05 +00:00
|
|
|
assert m.kw_only_with_defaults(z=8) == (3, 4, 5, 8)
|
|
|
|
assert m.kw_only_with_defaults(2, z=8) == (2, 4, 5, 8)
|
|
|
|
assert m.kw_only_with_defaults(2, j=7, k=8, z=9) == (2, 7, 8, 9)
|
|
|
|
assert m.kw_only_with_defaults(2, 7, z=9, k=8) == (2, 7, 8, 9)
|
2017-12-23 22:56:07 +00:00
|
|
|
|
2020-09-05 00:02:05 +00:00
|
|
|
assert m.kw_only_mixed(1, j=2) == (1, 2)
|
|
|
|
assert m.kw_only_mixed(j=2, i=3) == (3, 2)
|
|
|
|
assert m.kw_only_mixed(i=2, j=3) == (2, 3)
|
2017-12-23 22:56:07 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert m.kw_only_plus_more(4, 5, k=6, extra=7) == (4, 5, 6, {"extra": 7})
|
|
|
|
assert m.kw_only_plus_more(3, k=5, j=4, extra=6) == (3, 4, 5, {"extra": 6})
|
|
|
|
assert m.kw_only_plus_more(2, k=3, extra=4) == (2, -1, 3, {"extra": 4})
|
2017-12-23 22:56:07 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
2020-09-05 00:02:05 +00:00
|
|
|
assert m.kw_only_mixed(i=1) == (1,)
|
2017-12-23 22:56:07 +00:00
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
2020-09-05 00:02:05 +00:00
|
|
|
m.register_invalid_kw_only(m)
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value)
|
|
|
|
== """
|
feat: allow kw-only args after a py::args (#3402)
* Simply has_kw_only_args handling
This simplifies tracking the number of kw-only args by instead tracking
the number of positional arguments (which is really what we care about
everywhere this is used).
* Allow keyword-only arguments to follow py::args
This removes the constraint that py::args has to be last (or
second-last, with py::kwargs) and instead makes py::args imply
py::kw_only for any remaining arguments, allowing you to bind a function
that works the same way as a Python function such as:
def f(a, *args, b):
return a * b + sum(args)
f(10, 1, 2, 3, b=20) # == 206
With this change, you can bind such a function using:
m.def("f", [](int a, py::args args, int b) { /* ... */ },
"a"_a, "b"_a);
Or, to be more explicit about the keyword-only arguments:
m.def("g", [](int a, py::args args, int b) { /* ... */ },
"a"_a, py::kw_only{}, "b"_a);
(The only difference between the two is that the latter will fail at
binding time if the `kw_only{}` doesn't match the `py::args` position).
This doesn't affect backwards compatibility at all because, currently,
you can't have a py::args anywhere except the end/2nd-last.
* Take args/kwargs by const lvalue ref
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-29 03:16:55 +00:00
|
|
|
arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument
|
2017-12-23 22:56:07 +00:00
|
|
|
"""
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2017-12-23 22:56:07 +00:00
|
|
|
|
2021-11-21 00:01:57 +00:00
|
|
|
# https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
|
|
|
|
x = m.first_arg_kw_only(i=1)
|
|
|
|
x.method()
|
|
|
|
x.method(i=1, j=2)
|
|
|
|
assert (
|
|
|
|
m.first_arg_kw_only.__init__.__doc__
|
2023-02-22 14:18:55 +00:00
|
|
|
== "__init__(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: int = 0) -> None\n"
|
2021-11-21 00:01:57 +00:00
|
|
|
)
|
|
|
|
assert (
|
|
|
|
m.first_arg_kw_only.method.__doc__
|
2023-02-22 14:18:55 +00:00
|
|
|
== "method(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: int = 1, j: int = 2) -> None\n"
|
2021-11-21 00:01:57 +00:00
|
|
|
)
|
|
|
|
|
2017-12-23 22:56:07 +00:00
|
|
|
|
2023-02-22 14:18:55 +00:00
|
|
|
def test_positional_only_args():
|
2020-09-05 00:02:05 +00:00
|
|
|
assert m.pos_only_all(1, 2) == (1, 2)
|
|
|
|
assert m.pos_only_all(2, 1) == (2, 1)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.pos_only_all(i=1, j=2)
|
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
|
|
|
assert m.pos_only_mix(1, 2) == (1, 2)
|
|
|
|
assert m.pos_only_mix(2, j=1) == (2, 1)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.pos_only_mix(i=1, j=2)
|
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
|
|
|
assert m.pos_kw_only_mix(1, 2, k=3) == (1, 2, 3)
|
|
|
|
assert m.pos_kw_only_mix(1, j=2, k=3) == (1, 2, 3)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.pos_kw_only_mix(i=1, j=2, k=3)
|
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.pos_kw_only_mix(1, 2, 3)
|
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.pos_only_def_mix()
|
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
|
|
|
assert m.pos_only_def_mix(1) == (1, 2, 3)
|
|
|
|
assert m.pos_only_def_mix(1, 4) == (1, 4, 3)
|
|
|
|
assert m.pos_only_def_mix(1, 4, 7) == (1, 4, 7)
|
|
|
|
assert m.pos_only_def_mix(1, 4, k=7) == (1, 4, 7)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
m.pos_only_def_mix(1, j=4)
|
|
|
|
assert "incompatible function arguments" in str(excinfo.value)
|
|
|
|
|
feat: allow kw-only args after a py::args (#3402)
* Simply has_kw_only_args handling
This simplifies tracking the number of kw-only args by instead tracking
the number of positional arguments (which is really what we care about
everywhere this is used).
* Allow keyword-only arguments to follow py::args
This removes the constraint that py::args has to be last (or
second-last, with py::kwargs) and instead makes py::args imply
py::kw_only for any remaining arguments, allowing you to bind a function
that works the same way as a Python function such as:
def f(a, *args, b):
return a * b + sum(args)
f(10, 1, 2, 3, b=20) # == 206
With this change, you can bind such a function using:
m.def("f", [](int a, py::args args, int b) { /* ... */ },
"a"_a, "b"_a);
Or, to be more explicit about the keyword-only arguments:
m.def("g", [](int a, py::args args, int b) { /* ... */ },
"a"_a, py::kw_only{}, "b"_a);
(The only difference between the two is that the latter will fail at
binding time if the `kw_only{}` doesn't match the `py::args` position).
This doesn't affect backwards compatibility at all because, currently,
you can't have a py::args anywhere except the end/2nd-last.
* Take args/kwargs by const lvalue ref
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-29 03:16:55 +00:00
|
|
|
# Mix it with args and kwargs:
|
|
|
|
assert (
|
|
|
|
m.args_kwonly_full_monty.__doc__
|
2023-02-22 14:18:55 +00:00
|
|
|
== "args_kwonly_full_monty(arg0: int = 1, arg1: int = 2, /, j: float = 3.14159, *args, z: int = 42, **kwargs) -> tuple\n"
|
feat: allow kw-only args after a py::args (#3402)
* Simply has_kw_only_args handling
This simplifies tracking the number of kw-only args by instead tracking
the number of positional arguments (which is really what we care about
everywhere this is used).
* Allow keyword-only arguments to follow py::args
This removes the constraint that py::args has to be last (or
second-last, with py::kwargs) and instead makes py::args imply
py::kw_only for any remaining arguments, allowing you to bind a function
that works the same way as a Python function such as:
def f(a, *args, b):
return a * b + sum(args)
f(10, 1, 2, 3, b=20) # == 206
With this change, you can bind such a function using:
m.def("f", [](int a, py::args args, int b) { /* ... */ },
"a"_a, "b"_a);
Or, to be more explicit about the keyword-only arguments:
m.def("g", [](int a, py::args args, int b) { /* ... */ },
"a"_a, py::kw_only{}, "b"_a);
(The only difference between the two is that the latter will fail at
binding time if the `kw_only{}` doesn't match the `py::args` position).
This doesn't affect backwards compatibility at all because, currently,
you can't have a py::args anywhere except the end/2nd-last.
* Take args/kwargs by const lvalue ref
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-29 03:16:55 +00:00
|
|
|
)
|
|
|
|
assert m.args_kwonly_full_monty() == (1, 2, 3.14159, (), 42, {})
|
|
|
|
assert m.args_kwonly_full_monty(8) == (8, 2, 3.14159, (), 42, {})
|
|
|
|
assert m.args_kwonly_full_monty(8, 9) == (8, 9, 3.14159, (), 42, {})
|
|
|
|
assert m.args_kwonly_full_monty(8, 9, 10) == (8, 9, 10.0, (), 42, {})
|
|
|
|
assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5.0,
|
|
|
|
(
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
),
|
|
|
|
9,
|
|
|
|
{"m": 8},
|
|
|
|
)
|
|
|
|
assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == (
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5.0,
|
|
|
|
(
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
),
|
|
|
|
9,
|
|
|
|
{"m": 8},
|
|
|
|
)
|
|
|
|
assert m.args_kwonly_full_monty(5, j=7, m=8, z=9) == (5, 2, 7.0, (), 9, {"m": 8})
|
|
|
|
assert m.args_kwonly_full_monty(i=5, j=7, m=8, z=9) == (
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
7.0,
|
|
|
|
(),
|
|
|
|
9,
|
|
|
|
{"i": 5, "m": 8},
|
|
|
|
)
|
|
|
|
|
2021-11-21 00:01:57 +00:00
|
|
|
# pos_only at the beginning of the argument list was "broken" in how it was displayed (though
|
|
|
|
# this is fairly useless in practice). Related to:
|
|
|
|
# https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
|
|
|
|
assert (
|
|
|
|
m.first_arg_kw_only.pos_only.__doc__
|
2023-02-22 14:18:55 +00:00
|
|
|
== "pos_only(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, /, i: int, j: int) -> None\n"
|
2021-11-21 00:01:57 +00:00
|
|
|
)
|
|
|
|
|
2020-09-05 00:02:05 +00:00
|
|
|
|
|
|
|
def test_signatures():
|
2023-02-22 14:18:55 +00:00
|
|
|
assert m.kw_only_all.__doc__ == "kw_only_all(*, i: int, j: int) -> tuple\n"
|
|
|
|
assert m.kw_only_mixed.__doc__ == "kw_only_mixed(i: int, *, j: int) -> tuple\n"
|
|
|
|
assert m.pos_only_all.__doc__ == "pos_only_all(i: int, j: int, /) -> tuple\n"
|
|
|
|
assert m.pos_only_mix.__doc__ == "pos_only_mix(i: int, /, j: int) -> tuple\n"
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
2023-02-22 14:18:55 +00:00
|
|
|
m.pos_kw_only_mix.__doc__
|
|
|
|
== "pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple\n"
|
2020-10-16 20:38:13 +00:00
|
|
|
)
|
2020-09-05 00:02:05 +00:00
|
|
|
|
|
|
|
|
2024-10-07 21:12:04 +00:00
|
|
|
@pytest.mark.skipif("env.GRAALPY", reason="Different refcounting mechanism")
|
2017-12-15 17:22:15 +00:00
|
|
|
def test_args_refcount():
|
|
|
|
"""Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular
|
|
|
|
arguments"""
|
|
|
|
refcount = m.arg_refcount_h
|
|
|
|
|
2024-05-30 05:40:55 +00:00
|
|
|
myval = object()
|
2017-12-15 17:22:15 +00:00
|
|
|
expected = refcount(myval)
|
|
|
|
assert m.arg_refcount_h(myval) == expected
|
2024-05-30 05:40:55 +00:00
|
|
|
assert m.arg_refcount_o(myval) == expected + 1
|
2017-12-15 17:22:15 +00:00
|
|
|
assert m.arg_refcount_h(myval) == expected
|
|
|
|
assert refcount(myval) == expected
|
|
|
|
|
|
|
|
assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval))
|
|
|
|
assert refcount(myval) == expected
|
|
|
|
|
|
|
|
assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {"a": 1, "b": myval})
|
|
|
|
assert refcount(myval) == expected
|
|
|
|
|
|
|
|
assert m.args_function(-1, myval) == (-1, myval)
|
|
|
|
assert refcount(myval) == expected
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == (
|
|
|
|
5,
|
|
|
|
6.0,
|
|
|
|
(myval,),
|
|
|
|
{"a": myval},
|
|
|
|
)
|
2017-12-15 17:22:15 +00:00
|
|
|
assert refcount(myval) == expected
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == (
|
|
|
|
(7, 8, myval),
|
|
|
|
{"a": 1, "b": myval},
|
|
|
|
)
|
2017-12-15 17:22:15 +00:00
|
|
|
assert refcount(myval) == expected
|
|
|
|
|
2024-09-24 17:28:22 +00:00
|
|
|
assert m.args_kwargs_subclass_function(7, 8, myval, a=1, b=myval) == (
|
|
|
|
(7, 8, myval),
|
|
|
|
{"a": 1, "b": myval},
|
|
|
|
)
|
|
|
|
assert refcount(myval) == expected
|
|
|
|
|
2017-12-15 17:22:15 +00:00
|
|
|
exp3 = refcount(myval, myval, myval)
|
|
|
|
assert m.args_refcount(myval, myval, myval) == (exp3, exp3, exp3)
|
|
|
|
assert refcount(myval) == expected
|
|
|
|
|
|
|
|
# This function takes the first arg as a `py::object` and the rest as a `py::args`. Unlike the
|
|
|
|
# previous case, when we have both positional and `py::args` we need to construct a new tuple
|
|
|
|
# for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
|
|
|
|
# tuple without having to inc_ref the individual elements, but here we can't, hence the extra
|
|
|
|
# refs.
|
2024-05-30 05:40:55 +00:00
|
|
|
exp3_3 = exp3 + 3
|
2024-05-28 13:07:59 +00:00
|
|
|
assert m.mixed_args_refcount(myval, myval, myval) == (exp3_3, exp3_3, exp3_3)
|
2020-07-24 16:43:59 +00:00
|
|
|
|
|
|
|
assert m.class_default_argument() == "<class 'decimal.Decimal'>"
|