From 00699f7c41a97cf8373a15e0b28e29361e017812 Mon Sep 17 00:00:00 2001 From: Wanda Date: Fri, 20 Oct 2023 18:10:51 +0200 Subject: [PATCH] lib.enum: allow using functional syntax for enum creation. Fixes #910. --- amaranth/lib/enum.py | 4 ++-- tests/test_lib_enum.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/amaranth/lib/enum.py b/amaranth/lib/enum.py index 10c3666b2..6dc70e72a 100644 --- a/amaranth/lib/enum.py +++ b/amaranth/lib/enum.py @@ -118,7 +118,7 @@ def as_shape(cls): # replacement for `enum.Enum`. return Shape._cast_plain_enum(cls) - def __call__(cls, value): + def __call__(cls, value, *args, **kwargs): # :class:`py_enum.Enum` uses ``__call__()`` for type casting: ``E(x)`` returns # the enumeration member whose value equals ``x``. In this case, ``x`` must be a concrete # value. @@ -130,7 +130,7 @@ def __call__(cls, value): # comparisons with enum members of the wrong type. if isinstance(value, Value): return value - return super().__call__(value) + return super().__call__(value, *args, **kwargs) def const(cls, init): # Same considerations apply as above. diff --git a/tests/test_lib_enum.py b/tests/test_lib_enum.py index 9d5a27ec4..5c86d609f 100644 --- a/tests/test_lib_enum.py +++ b/tests/test_lib_enum.py @@ -116,3 +116,6 @@ class EnumA(Enum): r"shape used in bit vector context; define the enumeration by inheriting from " r"the class in amaranth\.lib\.enum and specifying the 'shape=' keyword argument$"): Cat(EnumA.A) + + def test_functional(self): + Enum("FOO", ["BAR", "BAZ"])