amaranth icon indicating copy to clipboard operation
amaranth copied to clipboard

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

Open whitequark opened this issue 1 year ago • 0 comments

Proof of concept PR for #1419.

Can be tested with:

from amaranth import *
from amaranth.lib import enum


class MyEnum(enum.Enum, shape=3):
    GetX1 = 0
    GetX2 = 1
    GetX4 = 2
    PutX1 = 3
    PutX2 = 4
    PutX4 = 5

    @property
    def is_get(self):
        return self in [MyEnum.GetX1, MyEnum.GetX2, MyEnum.GetX4]

    @property
    def is_put(self):
        return self in [MyEnum.PutX1, MyEnum.PutX2, MyEnum.PutX4]


sig = Signal(MyEnum)
print(sig.is_get)
print(sig.is_put)
print(sig.x)

This returns:

(proxy (array [True, True, True, False, False, False]) (sig sig))
(proxy (array [False, False, False, True, True, True]) (sig sig))
Traceback (most recent call last):
  File "/home/whitequark/Projects/amaranth/xenum.py", line 25, in <module>
    print(sig.x)
          ^^^^^
  File "/home/whitequark/Projects/amaranth/amaranth/lib/enum.py", line 317, in __getattr__
    raise AttributeError(f"{self!r} has no attribute {name!r}")
AttributeError: EnumView(MyEnum, (sig sig)) has no attribute 'x'

whitequark avatar Jun 27 '24 15:06 whitequark