Skip to content

Commit

Permalink
[Symforce] Better assertion message when mixed types are in a list
Browse files Browse the repository at this point in the history
Can hit this when trying to codegen a list with matrices of different
sizes, making it hard to realize what was going on. Updating the
error message to make the issue clearer.

GitOrigin-RevId: 1533317a7fde9bd5ec9f63975bb639dcac9c2ed2
  • Loading branch information
Skydio authored and bradley-solliday-skydio committed Apr 23, 2024
1 parent 9932e20 commit 75d13cb
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions symforce/values/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,17 @@ def get_index_from_items(items: T.Iterable[T.Tuple[str, T.Any]]) -> T.Dict[str,
elif isinstance(value, (sf.Expr, sf.Symbol, int, float)):
entry = entry_helper(datatype=sf.Scalar)
elif isinstance(value, (list, tuple)):
assert all(
type(v) == type(value[0]) # pylint: disable=unidiomatic-typecheck
for v in value
) or all(typing_util.scalar_like(v) for v in value)
if not (
all(
type(v) == type(value[0]) # pylint: disable=unidiomatic-typecheck
for v in value
)
or all(typing_util.scalar_like(v) for v in value)
):
raise TypeError(
"A list/tuple in a Values object should not contain different types. "
f'Types in list "{name}" are {set(type(v) for v in value)}'
)
name_list = [f"{name}_{i}" for i in range(len(value))]
item_index = Values.get_index_from_items(zip(name_list, value))
entry = entry_helper(item_index=item_index)
Expand Down

0 comments on commit 75d13cb

Please sign in to comment.