diff --git a/amaranth/lib/enum.py b/amaranth/lib/enum.py index 6a2fce7..f957222 100644 --- a/amaranth/lib/enum.py +++ b/amaranth/lib/enum.py @@ -14,14 +14,14 @@ for _member in py_enum.__all__: 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 `, 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. @@ -180,32 +180,37 @@ class EnumMeta(ShapeCastable, py_enum.EnumMeta): 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): @@ -219,7 +224,7 @@ class EnumView(ValueCastable): """Constructs a view with the given enum type and target (a :ref:`value-like `). """ - 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) diff --git a/docs/stdlib/enum.rst b/docs/stdlib/enum.rst index 9887158..52d507e 100644 --- a/docs/stdlib/enum.rst +++ b/docs/stdlib/enum.rst @@ -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 diff --git a/tests/test_lib_enum.py b/tests/test_lib_enum.py index af4b006..806c31e 100644 --- a/tests/test_lib_enum.py +++ b/tests/test_lib_enum.py @@ -5,7 +5,7 @@ import unittest 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 * @@ -100,7 +100,7 @@ class EnumTestCase(FHDLTestCase): 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):