[ty] Infer list[T] when unpacking non-tuple type (#18438)

## Summary

Follow-up from #18401, I was looking at whether that would fix the issue
at https://github.com/astral-sh/ty/issues/247#issuecomment-2917656676
and it didn't, which made me realize that the PR only inferred `list[T]`
when the value type was tuple but it could be other types as well.

This PR fixes the actual issue by inferring `list[T]` for the non-tuple
type case.

## Test Plan

Add test cases for starred expression involved with non-tuple type. I
also added a few test cases for list type and list literal.

I also verified that the example in the linked issue comment works:
```py
def _(line: str):
    a, b, *c = line.split(maxsplit=2)
    c.pop()
```
This commit is contained in:
Dhruv Manilawala 2025-06-03 19:17:47 +05:30 committed by GitHub
parent 0986edf427
commit 8d98c601d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 75 additions and 5 deletions

View file

@ -192,8 +192,15 @@ impl<'db> Unpacker<'db> {
err.fallback_element_type(self.db())
})
};
for target_type in &mut target_types {
target_type.push(ty);
// Both `elts` and `target_types` are guaranteed to have the same length.
for (element, target_type) in elts.iter().zip(&mut target_types) {
if element.is_starred_expr() {
target_type.push(
KnownClass::List.to_specialized_instance(self.db(), [ty]),
);
} else {
target_type.push(ty);
}
}
}
}