From 70eaf5818b083a564597ed0865f437488f777b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Mon, 22 Jan 2024 20:04:28 -0800 Subject: [PATCH] Append methods from the inherited class to the new enum class --- src/lvmopstools/actor.py | 13 ++++++++++++- tests/test_actor.py | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lvmopstools/actor.py b/src/lvmopstools/actor.py index 52b4b84..7ab52c0 100644 --- a/src/lvmopstools/actor.py +++ b/src/lvmopstools/actor.py @@ -79,7 +79,18 @@ def wrapper(added_enum): joined[item.name] = item.value for item in added_enum: joined[item.name] = item.value - return enum.Enum(added_enum.__name__, joined) + + new_enum_class = enum.Enum(added_enum.__name__, joined) + + # Add methods from the inherited class. + for attr in inherited_enum.__dict__: + if attr.startswith("__"): + continue + if hasattr(new_enum_class, attr): + continue + setattr(new_enum_class, attr, getattr(inherited_enum, attr)) + + return new_enum_class return wrapper diff --git a/tests/test_actor.py b/tests/test_actor.py index 281b8d8..7652ffa 100644 --- a/tests/test_actor.py +++ b/tests/test_actor.py @@ -37,6 +37,8 @@ class ExtraErrorCodes(enum.Enum): assert hasattr(ExtraErrorCodes, "SOME_FAILURE_MODE") assert hasattr(ExtraErrorCodes, "UNKNOWN") + assert hasattr(ExtraErrorCodes, "get_error_code") + def test_verify_error_codes_fails(): with pytest.raises(ValueError):