Skip to content

Commit 8542252

Browse files
committed
lib.enum: privatize internal attributes in EnumView/FlagView.
Before this commit, public names `enum` and `target` were not usable as enumeration member or method names. This was unintentional and our policy is to not reserve public names where they may clash with names defined in downstream code. This commit makes them a private name (`__enum`, etc). This was chosen instead of a sunder name (`_amaranth_enum_`, etc) because there is a 1:1 correspondence between the attribute and a `ValueCastable` method: * `shape()` -> `__enum` * `as_value()` -> `__target`
1 parent b9dc4d3 commit 8542252

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

amaranth/lib/enum.py

+29-23
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,16 @@ def __init__(self, enum, target):
234234
.format(target)) from e
235235
if cast_target.shape() != enum.as_shape():
236236
raise TypeError("EnumView target must have the same shape as the enum")
237-
self.enum = enum
238-
self.target = cast_target
237+
self.__enum = enum
238+
self.__target = cast_target
239239

240240
def shape(self):
241241
"""Returns the underlying enum type."""
242-
return self.enum
242+
return self.__enum
243243

244244
def as_value(self):
245245
"""Returns the underlying value."""
246-
return self.target
246+
return self.__target
247247

248248
def eq(self, other):
249249
"""Assign to the underlying value.
@@ -296,21 +296,25 @@ def __eq__(self, other):
296296
:class:`Value`
297297
The result of the equality comparison, as a single-bit value.
298298
"""
299-
if isinstance(other, self.enum):
300-
other = self.enum(Value.cast(other))
301-
if not isinstance(other, EnumView) or other.enum is not self.enum:
302-
raise TypeError("an EnumView can only be compared to value or other EnumView of the same enum type")
303-
return self.target == other.target
299+
enum_cls = self.shape()
300+
if isinstance(other, enum_cls):
301+
other = enum_cls(Value.cast(other))
302+
if not isinstance(other, EnumView) or other.shape() is not enum_cls:
303+
raise TypeError("an EnumView can only be compared to value or other EnumView of "
304+
"the same enum type")
305+
return self.as_value() == other.as_value()
304306

305307
def __ne__(self, other):
306-
if isinstance(other, self.enum):
307-
other = self.enum(Value.cast(other))
308-
if not isinstance(other, EnumView) or other.enum is not self.enum:
309-
raise TypeError("an EnumView can only be compared to value or other EnumView of the same enum type")
310-
return self.target != other.target
308+
enum_cls = self.shape()
309+
if isinstance(other, enum_cls):
310+
other = enum_cls(Value.cast(other))
311+
if not isinstance(other, EnumView) or other.shape() is not enum_cls:
312+
raise TypeError("an EnumView can only be compared to value or other EnumView of "
313+
"the same enum type")
314+
return self.as_value() != other.as_value()
311315

312316
def __repr__(self):
313-
return f"{type(self).__qualname__}({self.enum.__qualname__}, {self.target!r})"
317+
return f"{type(self).__qualname__}({self.__enum.__qualname__}, {self.__target!r})"
314318

315319

316320
class FlagView(EnumView):
@@ -330,21 +334,23 @@ def __invert__(self):
330334
-------
331335
:class:`FlagView`
332336
"""
333-
if hasattr(self.enum, "_boundary_") and self.enum._boundary_ in (EJECT, KEEP):
334-
return self.enum._amaranth_view_class_(self.enum, ~self.target)
337+
enum_cls = self.shape()
338+
if hasattr(enum_cls, "_boundary_") and enum_cls._boundary_ in (EJECT, KEEP):
339+
return enum_cls._amaranth_view_class_(enum_cls, ~self.as_value())
335340
else:
336341
singles_mask = 0
337-
for flag in self.enum:
342+
for flag in enum_cls:
338343
if (flag.value & (flag.value - 1)) == 0:
339344
singles_mask |= flag.value
340-
return self.enum._amaranth_view_class_(self.enum, ~self.target & singles_mask)
345+
return enum_cls._amaranth_view_class_(enum_cls, ~self.as_value() & singles_mask)
341346

342347
def __bitop(self, other, op):
343-
if isinstance(other, self.enum):
344-
other = self.enum(Value.cast(other))
345-
if not isinstance(other, FlagView) or other.enum is not self.enum:
348+
enum_cls = self.shape()
349+
if isinstance(other, enum_cls):
350+
other = enum_cls(Value.cast(other))
351+
if not isinstance(other, FlagView) or other.shape() is not enum_cls:
346352
raise TypeError("a FlagView can only perform bitwise operation with a value or other FlagView of the same enum type")
347-
return self.enum._amaranth_view_class_(self.enum, op(self.target, other.target))
353+
return enum_cls._amaranth_view_class_(enum_cls, op(self.as_value(), other.as_value()))
348354

349355
def __and__(self, other):
350356
"""Performs a bitwise AND and returns another :class:`FlagView`.

0 commit comments

Comments
 (0)