lib.data: add a diagnostic for slicing data.View.

This is meaningless for a view but meaningful for the underlying value.

Fixes #1375.
This commit is contained in:
Catherine 2024-06-10 13:28:43 +01:00
parent 0140fe27e2
commit c649045f35
2 changed files with 9 additions and 0 deletions

View file

@ -792,6 +792,9 @@ class View(ValueCastable):
:exc:`TypeError`
If :meth:`.ShapeCastable.__call__` does not return a value or a value-castable object.
"""
if isinstance(key, slice):
raise TypeError(
"View cannot be indexed with a slice; did you mean to call `.as_value()` first?")
if isinstance(self.__layout, ArrayLayout):
if not isinstance(key, (int, Value, ValueCastable)):
raise TypeError(

View file

@ -670,6 +670,12 @@ class ViewTestCase(FHDLTestCase):
r"with a value$"):
Signal(data.StructLayout({}))[Signal(1)]
def test_index_wrong_slice(self):
with self.assertRaisesRegex(TypeError,
r"^View cannot be indexed with a slice; did you mean to call `.as_value\(\)` "
r"first\?$"):
Signal(data.StructLayout({}))[0:1]
def test_getattr(self):
v = Signal(data.UnionLayout({
"a": unsigned(2),