diff --git a/pytensor/typed_list/basic.py b/pytensor/typed_list/basic.py index 1b836c0bfa..f458779064 100644 --- a/pytensor/typed_list/basic.py +++ b/pytensor/typed_list/basic.py @@ -19,6 +19,11 @@ def __getitem__(self, index): def __len__(self): return length(self) + def __bool__(self): + # Truthiness of typedLists cannot depend on length, + # just like truthiness of TensorVariables does not depend on size or contents + return True + def append(self, toAppend): return append(self, toAppend) @@ -677,3 +682,18 @@ def perform(self, node, inputs, outputs): All PyTensor variables must have the same type. """ + + +class MakeEmptyList(Op): + __props__ = () + + def make_node(self, ttype): + tl = TypedListType(ttype)() + return Apply(self, [], [tl]) + + def perform(self, node, inputs, outputs): + (out,) = outputs + out[0] = [] + + +make_empty_list = MakeEmptyList() diff --git a/tests/typed_list/test_type.py b/tests/typed_list/test_type.py index 41c1a8326b..a5281e21d1 100644 --- a/tests/typed_list/test_type.py +++ b/tests/typed_list/test_type.py @@ -150,3 +150,7 @@ def test_variable_is_Typed_List_variable(self): )() assert isinstance(mySymbolicVariable, TypedListVariable) + + def test_any(self): + tlist = TypedListType(TensorType(dtype="int64", shape=(None,)))() + assert any([tlist])