Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type_caster<std::monostate> #3818

Merged
merged 7 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ struct variant_caster<V<Ts...>> {
#if defined(PYBIND11_HAS_VARIANT)
template <typename... Ts>
struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> {};

template <>
struct type_caster<std::monostate> : public void_caster<std::monostate> {};
#endif

PYBIND11_NAMESPACE_END(detail)
Expand Down
20 changes: 18 additions & 2 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ struct type_caster<boost::none_t> : void_caster<boost::none_t> {};
// Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14
#if defined(PYBIND11_HAS_VARIANT)
using std::variant;
# define PYBIND11_TEST_VARIANT 1
#elif defined(PYBIND11_TEST_BOOST)
# include <boost/variant.hpp>
# define PYBIND11_HAS_VARIANT 1
# define PYBIND11_TEST_VARIANT 1
using boost::variant;

namespace pybind11 {
Expand Down Expand Up @@ -424,7 +425,7 @@ TEST_SUBMODULE(stl, m) {
m.def("parent_path", [](const std::filesystem::path &p) { return p.parent_path(); });
#endif

#ifdef PYBIND11_HAS_VARIANT
#ifdef PYBIND11_TEST_VARIANT
static_assert(std::is_same<py::detail::variant_caster_visitor::result_type, py::handle>::value,
"visitor::result_type is required by boost::variant in C++11 mode");

Expand All @@ -435,6 +436,9 @@ TEST_SUBMODULE(stl, m) {
result_type operator()(const std::string &) { return "std::string"; }
result_type operator()(double) { return "double"; }
result_type operator()(std::nullptr_t) { return "std::nullptr_t"; }
# if defined(PYBIND11_HAS_VARIANT)
result_type operator()(std::monostate) { return "std::monostate"; }
# endif
};

// test_variant
Expand All @@ -448,6 +452,18 @@ TEST_SUBMODULE(stl, m) {
using V = variant<int, std::string>;
return py::make_tuple(V(5), V("Hello"));
});

# if defined(PYBIND11_HAS_VARIANT)
// std::monostate tests.
m.def("load_monostate_variant",
[](const variant<std::monostate, int, std::string> &v) -> const char * {
return py::detail::visit_helper<variant>::call(visitor(), v);
});
m.def("cast_monostate_variant", []() {
using V = variant<std::monostate, int, std::string>;
return py::make_tuple(V{}, V(5), V("Hello"));
});
# endif
#endif

// #528: templated constructor
Expand Down
16 changes: 16 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ def test_variant(doc):
)


@pytest.mark.skipif(
not hasattr(m, "load_monostate_variant"), reason="no std::monostate"
)
def test_variant_monostate(doc):
assert m.load_monostate_variant(None) == "std::monostate"
assert m.load_monostate_variant(1) == "int"
assert m.load_monostate_variant("1") == "std::string"

assert m.cast_monostate_variant() == (None, 5, "Hello")

assert (
doc(m.load_monostate_variant)
== "load_monostate_variant(arg0: Union[None, int, str]) -> str"
)


def test_vec_of_reference_wrapper():
"""#171: Can't return reference wrappers (or STL structures containing them)"""
assert (
Expand Down