Skip to content

Commit

Permalink
lib.enum: allow using functional syntax for enum creation.
Browse files Browse the repository at this point in the history
Fixes #910.
  • Loading branch information
wanda-phi authored and whitequark committed Oct 21, 2023
1 parent dacbd9d commit 00699f7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions amaranth/lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_lib_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

0 comments on commit 00699f7

Please sign in to comment.