mirror of
https://github.com/pybind/pybind11.git
synced 2026-06-11 16:58:08 +00:00
Fix missing pythonic type hints for native_enum (#5619)
* fix missing pythonic type hints for native_enum * Fix __qualname__ in native_enum * Rename `enum class native` → `func_sig_rendering`. Add to `ENUM_TYPES_AND_MEMBERS`. Move new code around to fit in more organically. "native" is used in so many places here, it would be very difficult to pin-point where the specific enum type is used. Similarly, "value" is difficult to pin-point. Changed to "nested_value". --------- Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com> Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
This commit is contained in:
@@ -29,6 +29,8 @@ enum class member_doc { mem0, mem1, mem2 };
|
||||
|
||||
struct class_with_enum {
|
||||
enum class in_class { one, two };
|
||||
|
||||
in_class nested_value = in_class::one;
|
||||
};
|
||||
|
||||
// https://github.com/protocolbuffers/protobuf/blob/d70b5c5156858132decfdbae0a1103e6a5cb1345/src/google/protobuf/generated_enum_util.h#L52-L53
|
||||
@@ -40,6 +42,8 @@ enum some_proto_enum : int { Zero, One };
|
||||
template <>
|
||||
struct is_proto_enum<some_proto_enum> : std::true_type {};
|
||||
|
||||
enum class func_sig_rendering {};
|
||||
|
||||
} // namespace test_native_enum
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
||||
@@ -124,11 +128,20 @@ TEST_SUBMODULE(native_enum, m) {
|
||||
.value("two", class_with_enum::in_class::two)
|
||||
.finalize();
|
||||
|
||||
py_class_with_enum.def(py::init())
|
||||
.def_readwrite("nested_value", &class_with_enum::nested_value);
|
||||
|
||||
m.def("isinstance_color", [](const py::object &obj) { return py::isinstance<color>(obj); });
|
||||
|
||||
m.def("pass_color", [](color e) { return static_cast<int>(e); });
|
||||
m.def("return_color", [](int i) { return static_cast<color>(i); });
|
||||
|
||||
py::native_enum<func_sig_rendering>(m, "func_sig_rendering", "enum.Enum").finalize();
|
||||
m.def(
|
||||
"pass_and_return_func_sig_rendering",
|
||||
[](func_sig_rendering e) { return e; },
|
||||
py::arg("e"));
|
||||
|
||||
m.def("pass_some_proto_enum", [](some_proto_enum) { return py::none(); });
|
||||
m.def("return_some_proto_enum", []() { return some_proto_enum::Zero; });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user