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.
This commit is contained in:
David Vo 2020-09-20 09:12:19 +10:00 committed by GitHub
parent 5a8ec8e505
commit 5e6ec49652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 17 deletions

View File

@ -1486,6 +1486,16 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta
}
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 @@ struct enum_base {
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 {

View File

@ -7,6 +7,9 @@ def test_unscoped_enum():
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_scoped_enum():
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 @@ def test_implicit_conversion():
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():