enum: add missing Enum.value property (#2739)

* enum: Add Enum.value property

* simplify

* address review
This commit is contained in:
Eric Cousineau 2020-12-31 10:08:15 -06:00 committed by GitHub
parent b7dfe5cc84
commit 2110d2d8ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View File

@ -1731,6 +1731,7 @@ public:
m_base.init(is_arithmetic, is_convertible); m_base.init(is_arithmetic, is_convertible);
def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value")); 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; }); def("__int__", [](Type value) { return (Scalar) value; });
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
def("__long__", [](Type value) { return (Scalar) value; }); def("__long__", [](Type value) { return (Scalar) value; });

View File

@ -13,15 +13,24 @@ def test_unscoped_enum():
# name property # name property
assert m.UnscopedEnum.EOne.name == "EOne" assert m.UnscopedEnum.EOne.name == "EOne"
assert m.UnscopedEnum.EOne.value == 1
assert m.UnscopedEnum.ETwo.name == "ETwo" assert m.UnscopedEnum.ETwo.name == "ETwo"
assert m.EOne.name == "EOne" assert m.UnscopedEnum.ETwo.value == 2
# name readonly assert m.EOne is m.UnscopedEnum.EOne
# name, value readonly
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
m.UnscopedEnum.EOne.name = "" m.UnscopedEnum.EOne.name = ""
# name returns a copy with pytest.raises(AttributeError):
foo = m.UnscopedEnum.EOne.name m.UnscopedEnum.EOne.value = 10
foo = "bar" # 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" 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 # __members__ property
assert m.UnscopedEnum.__members__ == { assert m.UnscopedEnum.__members__ == {
@ -33,8 +42,8 @@ def test_unscoped_enum():
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
m.UnscopedEnum.__members__ = {} m.UnscopedEnum.__members__ = {}
# __members__ returns a copy # __members__ returns a copy
foo = m.UnscopedEnum.__members__ nonaliased_members = m.UnscopedEnum.__members__
foo["bar"] = "baz" nonaliased_members["bar"] = "baz"
assert m.UnscopedEnum.__members__ == { assert m.UnscopedEnum.__members__ == {
"EOne": m.UnscopedEnum.EOne, "EOne": m.UnscopedEnum.EOne,
"ETwo": m.UnscopedEnum.ETwo, "ETwo": m.UnscopedEnum.ETwo,