Skip to content

Commit

Permalink
Allow TaskMetadata to handle empty lists
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Jan 30, 2025
1 parent f24ff2e commit 6b37143
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion python/lsst/pipe/base/_task_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,12 @@ def __getitem__(self, key: str) -> Any:
if key0 in self.metadata:
return self.metadata[key0]
if key0 in self.arrays:
return self.arrays[key0][-1]
arr = self.arrays[key0]
if not arr:
# If there are no elements then returning a scalar
# is an error.
raise KeyError(f"'{key}' not found")
return arr[-1]
raise KeyError(f"'{key}' not found")
# Hierarchical lookup so the top key can only be in the metadata
# property. Trap KeyError and reraise so that the correct key
Expand Down Expand Up @@ -613,6 +618,8 @@ def _validate_value(self, value: Any) -> tuple[str, Any]:
# For model consistency, need to check that every item in the
# list has the same type.
value = list(value)
if not value:
return "array", value

type0 = type(value[0])
for i in value:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_taskmetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def testTaskMetadata(self):
self.assertEqual(meta.getArray("new_array"), ["a", "b", "c"])
meta["new_array"] = [1, 2, 3]
self.assertEqual(meta.getArray("new_array"), [1, 2, 3])
meta["empty_array"] = []
self.assertEqual(meta.getArray("empty_array"), [])
with self.assertRaises(KeyError):
meta["empty_array"]

meta["meta"] = 5
meta["meta"] = TaskMetadata()
Expand Down

0 comments on commit 6b37143

Please sign in to comment.