Add enum value to enum repr (#2126)
This changes enum reprs to look like `<Enum.name: value>` similarly to
the Python enum module.
This keeps the str of enums as `Enum.name`, like the Python enum module.
diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h
index 7c5286a..c1d17f7 100644
--- a/include/pybind11/pybind11.h
+++ b/include/pybind11/pybind11.h
@@ -1486,6 +1486,16 @@
}
PYBIND11_NAMESPACE_BEGIN(detail)
+
+inline str enum_name(handle arg) {
+ dict entries = arg.get_type().attr("__entries");
+ for (const auto &kv : entries) {
+ if (handle(kv.second[int_(0)]).equal(arg))
+ return pybind11::str(kv.first);
+ }
+ return "???";
+}
+
struct enum_base {
enum_base(handle base, handle parent) : m_base(base), m_parent(parent) { }
@@ -1495,29 +1505,21 @@
auto static_property = handle((PyObject *) get_internals().static_property_type);
m_base.attr("__repr__") = cpp_function(
- [](handle arg) -> str {
+ [](object arg) -> str {
handle type = type::handle_of(arg);
object type_name = type.attr("__name__");
- dict entries = type.attr("__entries");
- for (auto kv : entries) {
- object other = kv.second[int_(0)];
- if (other.equal(arg))
- return pybind11::str("{}.{}").format(type_name, kv.first);
- }
- return pybind11::str("{}.???").format(type_name);
+ return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg));
}, name("__repr__"), is_method(m_base)
);
- m_base.attr("name") = property(cpp_function(
+ m_base.attr("name") = property(cpp_function(&enum_name, name("name"), is_method(m_base)));
+
+ m_base.attr("__str__") = cpp_function(
[](handle arg) -> str {
- dict entries = type::handle_of(arg).attr("__entries");
- for (auto kv : entries) {
- if (handle(kv.second[int_(0)]).equal(arg))
- return pybind11::str(kv.first);
- }
- return "???";
+ object type_name = type::handle_of(arg).attr("__name__");
+ return pybind11::str("{}.{}").format(type_name, enum_name(arg));
}, name("name"), is_method(m_base)
- ));
+ );
m_base.attr("__doc__") = static_property(cpp_function(
[](handle arg) -> std::string {
diff --git a/tests/test_enum.py b/tests/test_enum.py
index bfaa193..46cb379 100644
--- a/tests/test_enum.py
+++ b/tests/test_enum.py
@@ -7,6 +7,9 @@
assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
assert str(m.EOne) == "UnscopedEnum.EOne"
+ assert repr(m.UnscopedEnum.EOne) == "<UnscopedEnum.EOne: 1>"
+ assert repr(m.UnscopedEnum.ETwo) == "<UnscopedEnum.ETwo: 2>"
+ assert repr(m.EOne) == "<UnscopedEnum.EOne: 1>"
# name property
assert m.UnscopedEnum.EOne.name == "EOne"
@@ -143,6 +146,8 @@
def test_implicit_conversion():
assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
+ assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "<EMode.EFirstMode: 1>"
+ assert repr(m.ClassWithUnscopedEnum.EFirstMode) == "<EMode.EFirstMode: 1>"
f = m.ClassWithUnscopedEnum.test_function
first = m.ClassWithUnscopedEnum.EFirstMode
@@ -167,7 +172,7 @@
x[f(first)] = 3
x[f(second)] = 4
# Hashing test
- assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"
+ assert repr(x) == "{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}"
def test_binary_operators():