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

Conversation

whitequark
Copy link
Member

@whitequark whitequark commented Jun 27, 2024

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 whitequark marked this pull request as draft June 27, 2024 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant