diff options
author | Eric Cousineau <eric.cousineau@tri.global> | 2020-12-31 10:08:15 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-31 11:08:15 -0500 |
commit | 2110d2d8bac7c5db240d4d0ccea5079224885ffd (patch) | |
tree | 0703be900c8ce0280bc1837a59f3bfd77dfecf8d | |
parent | b7dfe5cc84ca1891b50e728e83c9b5b393cf5272 (diff) | |
download | platform_external_python_pybind11-2110d2d8bac7c5db240d4d0ccea5079224885ffd.tar.gz platform_external_python_pybind11-2110d2d8bac7c5db240d4d0ccea5079224885ffd.tar.bz2 platform_external_python_pybind11-2110d2d8bac7c5db240d4d0ccea5079224885ffd.zip |
enum: add missing Enum.value property (#2739)
* enum: Add Enum.value property
* simplify
* address review
-rw-r--r-- | include/pybind11/pybind11.h | 1 | ||||
-rw-r--r-- | tests/test_enum.py | 23 |
2 files changed, 17 insertions, 7 deletions
diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 70ba635..f2d3c5d 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1731,6 +1731,7 @@ public: m_base.init(is_arithmetic, is_convertible); def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value")); + def_property_readonly("value", [](Type value) { return (Scalar) value; }); def("__int__", [](Type value) { return (Scalar) value; }); #if PY_MAJOR_VERSION < 3 def("__long__", [](Type value) { return (Scalar) value; }); diff --git a/tests/test_enum.py b/tests/test_enum.py index f3cce8b..e9732fa 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -13,15 +13,24 @@ def test_unscoped_enum(): # name property assert m.UnscopedEnum.EOne.name == "EOne" + assert m.UnscopedEnum.EOne.value == 1 assert m.UnscopedEnum.ETwo.name == "ETwo" - assert m.EOne.name == "EOne" - # name readonly + assert m.UnscopedEnum.ETwo.value == 2 + assert m.EOne is m.UnscopedEnum.EOne + # name, value readonly with pytest.raises(AttributeError): m.UnscopedEnum.EOne.name = "" - # name returns a copy - foo = m.UnscopedEnum.EOne.name - foo = "bar" + with pytest.raises(AttributeError): + m.UnscopedEnum.EOne.value = 10 + # name, value returns a copy + # TODO: Neither the name nor value tests actually check against aliasing. + # Use a mutable type that has reference semantics. + nonaliased_name = m.UnscopedEnum.EOne.name + nonaliased_name = "bar" # noqa: F841 assert m.UnscopedEnum.EOne.name == "EOne" + nonaliased_value = m.UnscopedEnum.EOne.value + nonaliased_value = 10 # noqa: F841 + assert m.UnscopedEnum.EOne.value == 1 # __members__ property assert m.UnscopedEnum.__members__ == { @@ -33,8 +42,8 @@ def test_unscoped_enum(): with pytest.raises(AttributeError): m.UnscopedEnum.__members__ = {} # __members__ returns a copy - foo = m.UnscopedEnum.__members__ - foo["bar"] = "baz" + nonaliased_members = m.UnscopedEnum.__members__ + nonaliased_members["bar"] = "baz" assert m.UnscopedEnum.__members__ == { "EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo, |