Skip to content

Enum attribute

enum_attribute

EnumType = TypeVar('EnumType', bound=StrEnum) module-attribute

EnumAttribute dataclass

Bases: Data[EnumType]

Helper for Enum Attributes. Takes a StrEnum type parameter, and defines parsing/printing automatically from its values, restricted to be parsable as identifiers.

example:

class MyEnum(StrEnum):
    First = auto()
    Second = auto()

class MyEnumAttribute(EnumAttribute[MyEnum], SpacedOpaqueSyntaxAttribute):
    name = "example.my_enum"

To use this attribute suffices to have a textual representation of example<my_enum first> and example<my_enum second>

Source code in xdsl/dialects/utils/enum_attribute.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class EnumAttribute(Data[EnumType]):
    """
    Helper for Enum Attributes. Takes a StrEnum type parameter, and defines
    parsing/printing automatically from its values, restricted to be parsable as
    identifiers.

    example:
    ```python
    class MyEnum(StrEnum):
        First = auto()
        Second = auto()

    class MyEnumAttribute(EnumAttribute[MyEnum], SpacedOpaqueSyntaxAttribute):
        name = "example.my_enum"
    ```
    To use this attribute suffices to have a textual representation
    of `example<my_enum first>` and ``example<my_enum second>``

    """

    enum_type: ClassVar[type[StrEnum]]

    def __init_subclass__(cls) -> None:
        """
        Extract and store the Enum type used by the subclass for use in
        parsing/printing.

        Subclass implementations are also constrained to keep implementations
        reasonable, unless more complex use cases appear.

        The constraint(s) are:
        - Only direct, specialized inheritance is allowed. That is, using a
        subclass of EnumAttribute as a base class is *not supported*.
        This simplifies type-hacking code and I don't see it being too
        restrictive anytime soon.
        """
        super().__init_subclass__()

        orig_bases = getattr(cls, "__orig_bases__")
        enumattr = next(b for b in orig_bases if get_origin(b) is EnumAttribute)
        enum_type = get_args(enumattr)[0]
        if isinstance(enum_type, TypeVar):
            raise TypeError("Only direct inheritance from EnumAttribute is allowed.")

        cls.enum_type = enum_type

    def print_parameter(self, printer: Printer) -> None:
        printer.print_identifier_or_string_literal(self.data.value)

    @classmethod
    def parse_parameter(cls, parser: AttrParser) -> EnumType:
        return cast(EnumType, parser.parse_str_enum(cls.enum_type))

enum_type: type[StrEnum] class-attribute

__init_subclass__() -> None

Extract and store the Enum type used by the subclass for use in parsing/printing.

Subclass implementations are also constrained to keep implementations reasonable, unless more complex use cases appear.

The constraint(s) are: - Only direct, specialized inheritance is allowed. That is, using a subclass of EnumAttribute as a base class is not supported. This simplifies type-hacking code and I don't see it being too restrictive anytime soon.

Source code in xdsl/dialects/utils/enum_attribute.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def __init_subclass__(cls) -> None:
    """
    Extract and store the Enum type used by the subclass for use in
    parsing/printing.

    Subclass implementations are also constrained to keep implementations
    reasonable, unless more complex use cases appear.

    The constraint(s) are:
    - Only direct, specialized inheritance is allowed. That is, using a
    subclass of EnumAttribute as a base class is *not supported*.
    This simplifies type-hacking code and I don't see it being too
    restrictive anytime soon.
    """
    super().__init_subclass__()

    orig_bases = getattr(cls, "__orig_bases__")
    enumattr = next(b for b in orig_bases if get_origin(b) is EnumAttribute)
    enum_type = get_args(enumattr)[0]
    if isinstance(enum_type, TypeVar):
        raise TypeError("Only direct inheritance from EnumAttribute is allowed.")

    cls.enum_type = enum_type

print_parameter(printer: Printer) -> None

Source code in xdsl/dialects/utils/enum_attribute.py
61
62
def print_parameter(self, printer: Printer) -> None:
    printer.print_identifier_or_string_literal(self.data.value)

parse_parameter(parser: AttrParser) -> EnumType classmethod

Source code in xdsl/dialects/utils/enum_attribute.py
64
65
66
@classmethod
def parse_parameter(cls, parser: AttrParser) -> EnumType:
    return cast(EnumType, parser.parse_str_enum(cls.enum_type))