Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [PoC] lib.enum: forward property accesses from enum view to base enum #1426

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion amaranth/lib/enum.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import builtins
import enum as py_enum
import warnings
import operator

from ..hdl import Value, ValueCastable, Shape, ShapeCastable, Const, SyntaxWarning, Format
from ..hdl import Value, ValueCastable, Shape, ShapeCastable, Const, SyntaxWarning, Format, Array


__all__ = py_enum.__all__ + ["EnumView", "FlagView"]
Expand Down Expand Up @@ -309,6 +310,12 @@ def __ne__(self, other):
raise TypeError("an EnumView can only be compared to value or other EnumView of the same enum type")
return self.target != other.target

def __getattr__(self, name):
if (attr := getattr(self.enum, name, None)) and isinstance(attr, builtins.property):
return Array(attr.fget(self.enum(n))
for n in range(max(e.value for e in self.enum) + 1))[self.target]
raise AttributeError(f"{self!r} has no attribute {name!r}")

def __repr__(self):
return f"{type(self).__qualname__}({self.enum.__qualname__}, {self.target!r})"

Expand Down