lib.enum: rename EnumMeta to EnumType.

Fixes #1073.
This commit is contained in:
Wanda 2024-04-11 09:21:14 +02:00 committed by Catherine
parent 84dff93786
commit 49a8a942e8
3 changed files with 20 additions and 15 deletions

View file

@ -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 <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.
@ -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 <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)

View file

@ -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

View file

@ -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):