Skip to content

Commit c1bceb9

Browse files
committed
Add more informative message for item assignment error
1 parent 573e0f7 commit c1bceb9

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pytensor/tensor/variable.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ def is_empty_array(val):
594594
),
595595
)
596596

597+
def __setitem__(self, key, value):
598+
raise TypeError(
599+
"TensorVariable does not support item assignment. Use the output of `set` or `add` instead."
600+
)
601+
597602
def take(self, indices, axis=None, mode="raise"):
598603
return at.subtensor.take(self, indices, axis, mode)
599604

tests/tensor/test_variable.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,15 @@ def test_set_add(self):
447447
with pytest.raises(TypeError, match=msg):
448448
x.add(y)
449449

450+
def test_set_item_error(self):
451+
x = matrix("x")
452+
453+
msg = "Use the output of `set` or `add` instead."
454+
with pytest.raises(TypeError, match=msg):
455+
x[0] = 5
456+
with pytest.raises(TypeError, match=msg):
457+
x[0] += 5
458+
450459

451460
def test_deprecated_import():
452461
with pytest.warns(

0 commit comments

Comments
 (0)