You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a string-enum converter that works using v2.2.4. This breaks starting from v2.3.0 due to the enum_ rewrite.
In v2.2.4, it was possible to support this conversion via implicitly_convertible. It looks like other people are taking advantage of this feature as well: #483 and #1122.
From v2.3.0+, it appears that this codepath is not being covered by the new PYBIND11_ENUM_OP_CONV and PYBIND11_ENUM_OP_CONV_LHS implementations. These macros appear to only support integer conversion and fails to invoke the existing pathways enabled through implicitly_convertible.
I am wondering if this is a regression or if only integer conversions will be allowed going forward?
Tested using v2.2.4, v2.3.0, v.2.4.3.
Reproducible example code
// example.cc
#include<pybind11/pybind11.h>namespacepy= pybind11;
template <typename T>
T pyStringToEnum(const py::enum_<T>& enm, const std::string& value) {
auto values = enm.attr("__members__").templatecast<py::dict>();
auto strVal = py::str(value);
if (values.contains(strVal)) {
returnT(values[strVal].templatecast<T>());
}
throw"Invalid string value " + value + " for enum " + std::string(typeid(T).name());
}
template <typename T>
py::str enumToPyString(const py::enum_<T>& enm, const T& value) {
auto values = enm.attr("__members__").templatecast<py::dict>();
for (auto val : values) {
if (T(val.second.templatecast<T>()) == value) {
returnpy::str(val.first);
}
}
throw"Invalid value for enum " + std::string(typeid(T).name());
}
enumclassKind { X = 0, Y = 1, Z = 2 };
structA {
A() : kind(Kind::X) {}
Kind kind;
};
PYBIND11_MODULE(example, m) {
py::enum_<Kind> enm(m, "Kind");
enm
.value("X", Kind::X)
.value("Y", Kind::Y)
.value("Z", Kind::Z)
.def(py::init([enm](const std::string& value) -> Kind {
returnpyStringToEnum(enm, py::str(value));
}))
.def("__str__", [enm](Kind e) { returnenumToPyString(enm, e); });
py::implicitly_convertible<std::string, Kind>();
py::implicitly_convertible<int, Kind>();
py::class_<A>(m, "A")
.def(py::init<>())
.def_readwrite("kind", &A::kind);
}
Issue description
I have a string-enum converter that works using v2.2.4. This breaks starting from v2.3.0 due to the enum_ rewrite.
In v2.2.4, it was possible to support this conversion via
implicitly_convertible
. It looks like other people are taking advantage of this feature as well: #483 and #1122.From v2.3.0+, it appears that this codepath is not being covered by the new
PYBIND11_ENUM_OP_CONV
andPYBIND11_ENUM_OP_CONV_LHS
implementations. These macros appear to only support integer conversion and fails to invoke the existing pathways enabled throughimplicitly_convertible
.I am wondering if this is a regression or if only integer conversions will be allowed going forward?
Tested using v2.2.4, v2.3.0, v.2.4.3.
Reproducible example code
The text was updated successfully, but these errors were encountered: