Skip to content

[READY] Implement an enum_ property "name" #1345

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

Merged
merged 1 commit into from
Apr 7, 2018
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
18 changes: 18 additions & 0 deletions docs/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,24 @@ The entries defined by the enumeration type are exposed in the ``__members__`` p
>>> Pet.Kind.__members__
{'Dog': Kind.Dog, 'Cat': Kind.Cat}

The ``name`` property returns the name of the enum value as a unicode string.

.. note::

It is also possible to use ``str(enum)``, however these accomplish different
goals. The following shows how these two approaches differ.

.. code-block:: pycon

>>> p = Pet( "Lucy", Pet.Cat )
>>> pet_type = p.type
>>> pet_type
Pet.Cat
>>> str(pet_type)
'Pet.Cat'
>>> pet_type.name
'Cat'

.. note::

When the special tag ``py::arithmetic()`` is specified to the ``enum_``
Expand Down
8 changes: 8 additions & 0 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,7 @@ detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetSta
template <typename Type> class enum_ : public class_<Type> {
public:
using class_<Type>::def;
using class_<Type>::def_property_readonly;
using class_<Type>::def_property_readonly_static;
using Scalar = typename std::underlying_type<Type>::type;

Expand All @@ -1381,6 +1382,13 @@ template <typename Type> class enum_ : public class_<Type> {
}
return pybind11::str("{}.???").format(name);
});
def_property_readonly("name", [m_entries_ptr](Type value) -> pybind11::str {
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
if (pybind11::cast<Type>(kv.second[int_(0)]) == value)
return pybind11::str(kv.first);
}
return pybind11::str("???");
});
def_property_readonly_static("__doc__", [m_entries_ptr](handle self) {
std::string docstring;
const char *tp_doc = ((PyTypeObject *) self.ptr())->tp_doc;
Expand Down
13 changes: 13 additions & 0 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ def test_unscoped_enum():
assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
assert str(m.EOne) == "UnscopedEnum.EOne"

# name property
assert m.UnscopedEnum.EOne.name == "EOne"
assert m.UnscopedEnum.ETwo.name == "ETwo"
assert m.EOne.name == "EOne"
# name readonly
with pytest.raises(AttributeError):
m.UnscopedEnum.EOne.name = ""
# name returns a copy
foo = m.UnscopedEnum.EOne.name
foo = "bar"
assert m.UnscopedEnum.EOne.name == "EOne"

# __members__ property
assert m.UnscopedEnum.__members__ == \
{"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo}
Expand Down