Skip to content

Commit 6ccefb3

Browse files
committed
lib.enum: rename EnumMeta to EnumType.
Fixes #1073.
1 parent c59447c commit 6ccefb3

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

amaranth/lib/enum.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
del _member
1515

1616

17-
class EnumMeta(ShapeCastable, py_enum.EnumMeta):
18-
"""Subclass of the standard :class:`enum.EnumMeta` that implements the :class:`ShapeCastable`
17+
class EnumType(ShapeCastable, py_enum.EnumMeta):
18+
"""Subclass of the standard :class:`enum.EnumType` that implements the :class:`ShapeCastable`
1919
protocol.
2020
2121
This metaclass provides the :meth:`as_shape` method, making its instances
2222
:ref:`shape-like <lang-shapelike>`, and accepts a ``shape=`` keyword argument
2323
to specify a shape explicitly. Other than this, it acts the same as the standard
24-
:class:`enum.EnumMeta` class; if the ``shape=`` argument is not specified and
24+
:class:`enum.EnumType` class; if the ``shape=`` argument is not specified and
2525
:meth:`as_shape` is never called, it places no restrictions on the enumeration class
2626
or the values of its members.
2727
@@ -180,32 +180,37 @@ def _value_repr(cls, value):
180180
yield Repr(FormatEnum(cls), value)
181181

182182

183+
# In 3.11, Python renamed EnumMeta to EnumType. Like Python itself, we support both for
184+
# compatibility.
185+
EnumMeta = EnumType
186+
187+
183188
class Enum(py_enum.Enum):
184-
"""Subclass of the standard :class:`enum.Enum` that has :class:`EnumMeta` as
189+
"""Subclass of the standard :class:`enum.Enum` that has :class:`EnumType` as
185190
its metaclass and :class:`EnumView` as its view class."""
186191

187192

188193
class IntEnum(py_enum.IntEnum):
189-
"""Subclass of the standard :class:`enum.IntEnum` that has :class:`EnumMeta` as
194+
"""Subclass of the standard :class:`enum.IntEnum` that has :class:`EnumType` as
190195
its metaclass."""
191196

192197

193198
class Flag(py_enum.Flag):
194-
"""Subclass of the standard :class:`enum.Flag` that has :class:`EnumMeta` as
199+
"""Subclass of the standard :class:`enum.Flag` that has :class:`EnumType` as
195200
its metaclass and :class:`FlagView` as its view class."""
196201

197202

198203
class IntFlag(py_enum.IntFlag):
199-
"""Subclass of the standard :class:`enum.IntFlag` that has :class:`EnumMeta` as
204+
"""Subclass of the standard :class:`enum.IntFlag` that has :class:`EnumType` as
200205
its metaclass."""
201206

202207

203208
# Fix up the metaclass after the fact: the metaclass __new__ requires these classes
204209
# to already be present, and also would not install itself on them due to lack of shape.
205-
Enum.__class__ = EnumMeta
206-
IntEnum.__class__ = EnumMeta
207-
Flag.__class__ = EnumMeta
208-
IntFlag.__class__ = EnumMeta
210+
Enum.__class__ = EnumType
211+
IntEnum.__class__ = EnumType
212+
Flag.__class__ = EnumType
213+
IntFlag.__class__ = EnumType
209214

210215

211216
class EnumView(ValueCastable):
@@ -219,7 +224,7 @@ def __init__(self, enum, target):
219224
"""Constructs a view with the given enum type and target
220225
(a :ref:`value-like <lang-valuelike>`).
221226
"""
222-
if not isinstance(enum, EnumMeta) or not hasattr(enum, "_amaranth_shape_"):
227+
if not isinstance(enum, EnumType) or not hasattr(enum, "_amaranth_shape_"):
223228
raise TypeError(f"EnumView type must be an enum with shape, not {enum!r}")
224229
try:
225230
cast_target = Value.cast(target)

docs/stdlib/enum.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ It is also possible to define a custom view class for a given enum:
114114
Metaclass
115115
=========
116116

117-
.. autoclass:: EnumMeta()
117+
.. autoclass:: EnumType()
118118

119119

120120
Base classes

tests/test_lib_enum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from amaranth import *
77
from amaranth.hdl import *
8-
from amaranth.lib.enum import Enum, EnumMeta, Flag, IntEnum, EnumView, FlagView
8+
from amaranth.lib.enum import Enum, EnumType, Flag, IntEnum, EnumView, FlagView
99

1010
from .utils import *
1111

@@ -100,7 +100,7 @@ class EnumA(Enum):
100100
Z = 0
101101
A = 10
102102
B = 20
103-
self.assertNotIsInstance(EnumA, EnumMeta)
103+
self.assertNotIsInstance(EnumA, EnumType)
104104
self.assertIsInstance(EnumA, py_enum.EnumMeta)
105105

106106
def test_const_shape(self):

0 commit comments

Comments
 (0)