astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
33.34k stars 1.11k forks source link

[red-knot] Follow-up from unpacked tuple assignment work #13773

Open dhruvmanila opened 1 month ago

dhruvmanila commented 1 month ago

As https://github.com/astral-sh/ruff/pull/13316 is merged, there are some follow-up work that needs to be done. The following is a list in the order of priority where the highest priority work is on the top:

dhruvmanila commented 2 weeks ago

Regarding "Use the unpacking logic in other places where it's relevant like" task, I think the challenge here would be to implement the logic to "combine" union types into a single type. For example, in a for loop like:

for (x, y) in ((1, 2), (3, 4), (5, 6)): ...

The inferred type for the iterable would be:

tuple[Literal[1], Literal[2]] | tuple[Literal[3], Literal[4]] | tuple[Literal[5], Literal[6]]

But, we should combine it to:

tuple[int, int]
# or
tuple[Literal[1, 3, 5], Literal[2, 4, 6]]

Or, another example where the types are "mixed":

for (x, y) in ((1, 2), ("a", "b")): ...

We should combine it to:

tuple[int | str, int | str]
# or
tuple[Literal[1, "a"], Literal[2, "b"]]

Another example:

for (x, y) in ((1, 2), ("a", 3)): ...

where,

tuple[int | str, int]
# or
tuple[Literal[1, "a"], Literal[2, 3]]