Use locally defined bindings to avoid dependency on test_stl.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2023-11-14 22:28:38 -08:00
parent e5f210e61b
commit d1694d9ac5
2 changed files with 17 additions and 8 deletions

View File

@ -1,6 +1,8 @@
#include "pybind11/stl.h"
#include "pybind11/stl_bind.h" #include "pybind11/stl_bind.h"
#include "pybind11_tests.h" #include "pybind11_tests.h"
#include <array>
#include <map> #include <map>
namespace test_cases_for_stubgen { namespace test_cases_for_stubgen {
@ -41,6 +43,11 @@ struct type_caster<test_cases_for_stubgen::UserType> : test_cases_for_stubgen::m
} // namespace detail } // namespace detail
} // namespace pybind11 } // namespace pybind11
PYBIND11_MAKE_OPAQUE(std::map<int, test_cases_for_stubgen::UserType>);
PYBIND11_MAKE_OPAQUE(std::map<test_cases_for_stubgen::UserType, int>);
PYBIND11_MAKE_OPAQUE(std::map<float, test_cases_for_stubgen::UserType>);
PYBIND11_MAKE_OPAQUE(std::map<test_cases_for_stubgen::UserType, float>);
TEST_SUBMODULE(cases_for_stubgen, m) { TEST_SUBMODULE(cases_for_stubgen, m) {
using UserType = test_cases_for_stubgen::UserType; using UserType = test_cases_for_stubgen::UserType;
@ -50,7 +57,7 @@ TEST_SUBMODULE(cases_for_stubgen, m) {
py::bind_map<std::map<int, UserType>>(m, "MapIntUserType"); py::bind_map<std::map<int, UserType>>(m, "MapIntUserType");
py::bind_map<std::map<UserType, int>>(m, "MapUserTypeInt"); py::bind_map<std::map<UserType, int>>(m, "MapUserTypeInt");
#define MAP_TYPE(MapTypePythonName, ...) \ #define LOCAL_HELPER(MapTypePythonName, ...) \
py::class_<__VA_ARGS__>(m, MapTypePythonName) \ py::class_<__VA_ARGS__>(m, MapTypePythonName) \
.def( \ .def( \
"keys", \ "keys", \
@ -65,8 +72,11 @@ TEST_SUBMODULE(cases_for_stubgen, m) {
[](const __VA_ARGS__ &v) { return py::make_iterator(v.begin(), v.end()); }, \ [](const __VA_ARGS__ &v) { return py::make_iterator(v.begin(), v.end()); }, \
py::keep_alive<0, 1>()) py::keep_alive<0, 1>())
MAP_TYPE("MapFloatUserType", std::map<float, UserType>); LOCAL_HELPER("MapFloatUserType", std::map<float, UserType>);
MAP_TYPE("MapUserTypeFloat", std::map<UserType, float>); LOCAL_HELPER("MapUserTypeFloat", std::map<UserType, float>);
#undef MAP_TYPE #undef MAP_TYPE
m.def("pass_std_array_int_2", [](const std::array<int, 2> &) {});
m.def("return_std_array_int_3", []() { return std::array<int, 3>{{1, 2, 3}}; });
} }

View File

@ -1,7 +1,6 @@
import pytest import pytest
from pybind11_tests import cases_for_stubgen as m from pybind11_tests import cases_for_stubgen as m
from pybind11_tests import stl as test_stl
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -64,12 +63,12 @@ from pybind11_tests import stl as test_stl
'__iter__(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[tuple[Annotated[Any, "test_cases_for_stubgen::UserType"], float]]\n', '__iter__(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[tuple[Annotated[Any, "test_cases_for_stubgen::UserType"], float]]\n',
), ),
( (
test_stl.cast_array.__doc__, m.pass_std_array_int_2.__doc__,
"cast_array() -> Annotated[list[int], FixedSize(2)]\n", "pass_std_array_int_2(arg0: Annotated[list[int], FixedSize(2)]) -> None\n",
), ),
( (
test_stl.load_array.__doc__, m.return_std_array_int_3.__doc__,
"load_array(arg0: Annotated[list[int], FixedSize(2)]) -> bool\n", "return_std_array_int_3() -> Annotated[list[int], FixedSize(3)]\n",
), ),
], ],
) )