parent
84dff93786
commit
49a8a942e8
3 changed files with 20 additions and 15 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue