Skip to content

Commit f0244ad

Browse files
ricardoV94lucianopaz
authored andcommitted
Raise NotImplementedError for boolean scalar indexing
This is a special-edge case that behaves differently than other boolean indexing. It adds a new dimension (either empty or with one entry). The logic in `make_node` and `infer_shape` don't handle this.
1 parent 03e5f77 commit f0244ad

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

pytensor/tensor/subtensor.py

+5
Original file line numberDiff line numberDiff line change
@@ -2627,6 +2627,11 @@ def as_index_variable(idx):
26272627
idx = as_tensor_variable(idx)
26282628
if idx.type.dtype not in discrete_dtypes:
26292629
raise TypeError("index must be integers or a boolean mask")
2630+
if idx.type.dtype == "bool" and idx.type.ndim == 0:
2631+
raise NotImplementedError(
2632+
"Boolean scalar indexing not implemented. "
2633+
"Open an issue in https://github.com/pymc-devs/pytensor/issues if you need this behavior."
2634+
)
26302635
return idx
26312636

26322637

tests/tensor/test_subtensor.py

+5
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,11 @@ def fun(x, y):
22282228
mode=self.mode,
22292229
)
22302230

2231+
def test_boolean_scalar_raises(self):
2232+
x = vector("x")
2233+
with pytest.raises(NotImplementedError):
2234+
x[np.array(True)]
2235+
22312236

22322237
class TestInferShape(utt.InferShapeTester):
22332238
@staticmethod

0 commit comments

Comments
 (0)