Skip to content

Commit 6956753

Browse files
committed
len(TypedList) cannot be symbolic
1 parent 7ccde64 commit 6956753

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

pytensor/typed_list/basic.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313

1414
class _typed_list_py_operators:
15+
def __len__(self):
16+
raise TypeError(
17+
"Cannot call len(TypedList). Use `.length()` method instead for the symbolic equivalent."
18+
)
19+
1520
def __getitem__(self, index):
1621
return getitem(self, index)
1722

18-
def __len__(self):
19-
return length(self)
20-
2123
def append(self, toAppend):
2224
return append(self, toAppend)
2325

@@ -36,10 +38,13 @@ def reverse(self):
3638
def count(self, elem):
3739
return count(self, elem)
3840

39-
# name "index" is already used by an attribute
41+
# name "index" is claimed as an attribute of PyTensor Variable(s)
4042
def ind(self, elem):
4143
return index_(self, elem)
4244

45+
def length(self):
46+
return length(self)
47+
4348
ttype = property(lambda self: self.type.ttype)
4449
dtype = property(lambda self: self.type.ttype.dtype)
4550
ndim = property(lambda self: self.type.ttype.ndim + 1)

tests/typed_list/test_basic.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35
import scipy
@@ -551,8 +553,11 @@ def test_interface(self):
551553
mySymbolicMatricesList = TypedListType(
552554
TensorType(pytensor.config.floatX, shape=(None, None))
553555
)()
554-
z = mySymbolicMatricesList.__len__()
555-
556+
with pytest.raises(
557+
TypeError, match=re.escape("Use `.length()` method instead")
558+
):
559+
len(mySymbolicMatricesList)
560+
z = mySymbolicMatricesList.length()
556561
f = pytensor.function([mySymbolicMatricesList], z)
557562

558563
x = rand_ranged_matrix(-1000, 1000, [100, 101])

0 commit comments

Comments
 (0)