Skip to content

Commit 66fa506

Browse files
wanda-phiwhitequark
authored andcommitted
hdl: make ValueCastable.from_bits override mandatory.
1 parent c9de103 commit 66fa506

File tree

4 files changed

+12
-29
lines changed

4 files changed

+12
-29
lines changed

amaranth/hdl/_ast.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,8 @@ def __init_subclass__(cls, **kwargs):
253253
raise TypeError(f"Class '{cls.__name__}' deriving from 'ShapeCastable' must override "
254254
f"the '__call__' method")
255255
if cls.from_bits is ShapeCastable.from_bits:
256-
warnings.warn(f"Class '{cls.__name__}' deriving from 'ShapeCastable' does not override "
257-
f"the 'from_bits' method, which will be required in Amaranth 0.6",
258-
DeprecationWarning, stacklevel=2)
256+
raise TypeError(f"Class '{cls.__name__}' deriving from 'ShapeCastable' must override "
257+
f"the 'from_bits' method")
259258

260259
# The signatures and definitions of these methods are weird because they are present here for
261260
# documentation (and error checking above) purpose only and should not affect control flow.
@@ -321,8 +320,10 @@ def const(self, *args, **kwargs):
321320
"""
322321
return super().const(*args, **kwargs) # :nocov:
323322

324-
def from_bits(self, raw):
325-
"""Lift a bit pattern to a higher-level representation.
323+
def from_bits(self, *args, **kwargs):
324+
"""from_bits(raw)
325+
326+
Lift a bit pattern to a higher-level representation.
326327
327328
This method is called by the Amaranth language to lift :py:`raw`, which is an :class:`int`,
328329
to a higher-level representation, which may be any object accepted by :meth:`const`.
@@ -353,7 +354,7 @@ def from_bits(self, raw):
353354
either directly or as a cause of another exception. While not constrained here,
354355
usually the exception class will be :exc:`ValueError`.
355356
"""
356-
return raw
357+
return super().from_bits(*args, **kwargs) # :nocov:
357358

358359
def __call__(self, *args, **kwargs):
359360
"""__call__(obj)

amaranth/lib/data.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,8 @@ class Const(ValueCastable):
922922
Slicing a constant or accessing its attributes returns a part of the underlying value
923923
corresponding to the field with that index or name. If the shape of the field is
924924
a :class:`Layout`, the returned value is a :class:`Const`; if it is a different
925-
:ref:`shape-like <lang-shapelike>` object implementing :meth:`~.ShapeCastable.from_bits`,
926-
it will be the result of calling that method; otherwise, it is an :class:`int`.
925+
:ref:`shape-like <lang-shapelike>` object, it will be the result of calling
926+
:meth:`~.ShapeCastable.from_bits`; otherwise, it is an :class:`int`.
927927
928928
Slicing a constant whose layout is an :class:`ArrayLayout` can be done with an index that is
929929
an Amaranth value rather than a constant integer. The returned element is chosen dynamically
@@ -990,8 +990,8 @@ def __getitem__(self, key):
990990
"""Slice the underlying value.
991991
992992
A field corresponding to :py:`key` is looked up in the layout. If the field's shape is
993-
a shape-castable object that has a :meth:`~.ShapeCastable.from_bits` method, returns
994-
the result of calling that method. Otherwise, returns an :class:`int`.
993+
a shape-castable object, returns the result of calling :meth:`~.ShapeCastable.from_bits`.
994+
Otherwise, returns an :class:`int`.
995995
996996
Arguments
997997
---------

docs/changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Language changes
2828

2929
.. currentmodule:: amaranth.hdl
3030

31+
* Changed: overriding :meth:`ValueCastable.from_bits` is now mandatory. (`RFC 51`_)
3132
* Removed: (deprecated in 0.4) :class:`Record`.
3233
* Removed: (deprecated in 0.5) public submodules of :mod:`amaranth.hdl`.
3334
* Removed: (deprecated in 0.5) :meth:`Value.implies`.

tests/test_hdl_ast.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,6 @@ def test_abstract(self):
211211
r"^Can't instantiate abstract class ShapeCastable$"):
212212
ShapeCastable()
213213

214-
def test_no_from_bits(self):
215-
with self.assertWarnsRegex(DeprecationWarning,
216-
r"^Class 'MockShapeCastableNoFromBits' deriving from 'ShapeCastable' does "
217-
r"not override the 'from_bits' method, which will be required in Amaranth 0.6$"):
218-
class MockShapeCastableNoFromBits(ShapeCastable):
219-
def __init__(self, dest):
220-
self.dest = dest
221-
222-
def as_shape(self):
223-
return self.dest
224-
225-
def __call__(self, value):
226-
return value
227-
228-
def const(self, init):
229-
return Const(init, self.dest)
230-
231-
self.assertEqual(MockShapeCastableNoFromBits(unsigned(2)).from_bits(123), 123)
232-
233214

234215
class ShapeLikeTestCase(FHDLTestCase):
235216
def test_construct(self):

0 commit comments

Comments
 (0)