Skip to content

lib.enum: rename EnumMeta to EnumType. #1317

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

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
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
29 changes: 17 additions & 12 deletions amaranth/lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
del _member


class EnumMeta(ShapeCastable, py_enum.EnumMeta):
"""Subclass of the standard :class:`enum.EnumMeta` that implements the :class:`ShapeCastable`
class EnumType(ShapeCastable, py_enum.EnumMeta):
"""Subclass of the standard :class:`enum.EnumType` that implements the :class:`ShapeCastable`
protocol.

This metaclass provides the :meth:`as_shape` method, making its instances
:ref:`shape-like <lang-shapelike>`, and accepts a ``shape=`` keyword argument
to specify a shape explicitly. Other than this, it acts the same as the standard
:class:`enum.EnumMeta` class; if the ``shape=`` argument is not specified and
:class:`enum.EnumType` class; if the ``shape=`` argument is not specified and
:meth:`as_shape` is never called, it places no restrictions on the enumeration class
or the values of its members.

Expand Down Expand Up @@ -180,32 +180,37 @@ def _value_repr(cls, value):
yield Repr(FormatEnum(cls), value)


# In 3.11, Python renamed EnumMeta to EnumType. Like Python itself, we support both for
# compatibility.
EnumMeta = EnumType


class Enum(py_enum.Enum):
"""Subclass of the standard :class:`enum.Enum` that has :class:`EnumMeta` as
"""Subclass of the standard :class:`enum.Enum` that has :class:`EnumType` as
its metaclass and :class:`EnumView` as its view class."""


class IntEnum(py_enum.IntEnum):
"""Subclass of the standard :class:`enum.IntEnum` that has :class:`EnumMeta` as
"""Subclass of the standard :class:`enum.IntEnum` that has :class:`EnumType` as
its metaclass."""


class Flag(py_enum.Flag):
"""Subclass of the standard :class:`enum.Flag` that has :class:`EnumMeta` as
"""Subclass of the standard :class:`enum.Flag` that has :class:`EnumType` as
its metaclass and :class:`FlagView` as its view class."""


class IntFlag(py_enum.IntFlag):
"""Subclass of the standard :class:`enum.IntFlag` that has :class:`EnumMeta` as
"""Subclass of the standard :class:`enum.IntFlag` that has :class:`EnumType` as
its metaclass."""


# Fix up the metaclass after the fact: the metaclass __new__ requires these classes
# to already be present, and also would not install itself on them due to lack of shape.
Enum.__class__ = EnumMeta
IntEnum.__class__ = EnumMeta
Flag.__class__ = EnumMeta
IntFlag.__class__ = EnumMeta
Enum.__class__ = EnumType
IntEnum.__class__ = EnumType
Flag.__class__ = EnumType
IntFlag.__class__ = EnumType


class EnumView(ValueCastable):
Expand All @@ -219,7 +224,7 @@ def __init__(self, enum, target):
"""Constructs a view with the given enum type and target
(a :ref:`value-like <lang-valuelike>`).
"""
if not isinstance(enum, EnumMeta) or not hasattr(enum, "_amaranth_shape_"):
if not isinstance(enum, EnumType) or not hasattr(enum, "_amaranth_shape_"):
raise TypeError(f"EnumView type must be an enum with shape, not {enum!r}")
try:
cast_target = Value.cast(target)
Expand Down
2 changes: 1 addition & 1 deletion docs/stdlib/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ It is also possible to define a custom view class for a given enum:
Metaclass
=========

.. autoclass:: EnumMeta()
.. autoclass:: EnumType()


Base classes
Expand Down
4 changes: 2 additions & 2 deletions tests/test_lib_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from amaranth import *
from amaranth.hdl import *
from amaranth.lib.enum import Enum, EnumMeta, Flag, IntEnum, EnumView, FlagView
from amaranth.lib.enum import Enum, EnumType, Flag, IntEnum, EnumView, FlagView

from .utils import *

Expand Down Expand Up @@ -100,7 +100,7 @@ class EnumA(Enum):
Z = 0
A = 10
B = 20
self.assertNotIsInstance(EnumA, EnumMeta)
self.assertNotIsInstance(EnumA, EnumType)
self.assertIsInstance(EnumA, py_enum.EnumMeta)

def test_const_shape(self):
Expand Down