Skip to content

Combine starred element types in unpacking #191

Description

@dhruvmanila

Combine starred element types. For example, in (a, *b, c) = (1, 2, 3, 4), the Literal[2] and Literal[3] type should be combined into list[int] to be assigned to *b. (Requires generic support over list) https://github.com/astral-sh/ruff/blob/c6b311c54643c039d793fb836caf94d22f03cbb4/crates/red_knot_python_semantic/src/types/infer.rs#L1248-L1251

Activity

  1. changed the title [-]Combine starred element types. For example, in `(a, *b, c) = (1, 2, 3, 4)`, the `Literal[2]` and `Literal[3]` type should be combined into `list[int]` to be assigned to `*b`. **(Requires generic support over list)** https://github.com/astral-sh/ruff/blob/c6b311c54643c039d793fb836caf94d22f03cbb4/crates/red_knot_python_semantic/src/types/infer.rs#L1248-L1251[/-] [+][red-knot] Combine starred element type in unpacking[/+] on Feb 25, 2025
  2. changed the title [-][red-knot] Combine starred element type in unpacking[/-] [+][red-knot] Combine starred element types in unpacking[/+] on Feb 25, 2025
  3. dhruvmanila commented on Feb 25, 2025

    @dhruvmanila
    MemberAuthor

    (Moving this to Backlog as it requires generic support over list)

  4. transferred this issue fromastral-sh/ruffon May 7, 2025
  5. changed the title [-][red-knot] Combine starred element types in unpacking[/-] [+]Combine starred element types in unpacking[/+] on May 7, 2025
  6. added
    genericsBugs or features relating to ty's generics implementation
    on May 11, 2025
  7. carljm commented on May 30, 2025

    @carljm
    Contributor

    I think now that we have generic list, this should be pretty easy?

  8. carljm commented on May 30, 2025

    @carljm
    Contributor

    #247 (comment) gave this example:

    error[unresolved-attribute]: Type `str` has no attribute `pop`
       --> src/eulertools/lib/utils.py:383:18
        |
    381 |     prefix, response_key, *answers = line.split(maxsplit=2)
    382 |     try:
    383 |         answer = answers.pop()
        |                  ^^^^^^^^^^^
    384 |     except IndexError:
    385 |         answer = ""
        |
    info: rule `unresolved-attribute` is enabled by default
    

    It seems to be this issue, though I'm surprised we wrongly infer str here rather than just inferring Unknown or Todo.

  9. dhruvmanila commented on May 30, 2025

    @dhruvmanila
    MemberAuthor

    Yes, this should be easy now. I do have a couple of questions:

    1. What should we infer in this error case? Should it be Unknown or list[Unknown? I think the latter makes sense

      # error: [invalid-assignment] "Not enough values to unpack: Expected 3 or more"
      [a, *b, c, d] = (1, 2)
      reveal_type(a)  # revealed: Unknown
      # TODO: Should be list[Any] once support for assigning to starred expression is added
      reveal_type(b)  # revealed: Unknown
      reveal_type(c)  # revealed: Unknown
      reveal_type(d)  # revealed: Unknown
    2. Should the literal types be promoted? In this example, should the inferred type be list[Literal[2]] or list[int]?

      [a, *b, c] = (1, 2, 3)
      reveal_type(a)  # revealed: Literal[1]
      # TODO: Should be list[int] once support for assigning to starred expression is added
      reveal_type(b)  # revealed: @Todo(starred unpacking)
      reveal_type(c)  # revealed: Literal[3]
    3. If we do promote the literal types, it would have a side effect in this example that we infer list[str] instead of list[LiteralString]

      (a, *b, c) = "ab"
      reveal_type(a)  # revealed: LiteralString
      # TODO: Should be list[LiteralString] once support for assigning to starred expression is added
      reveal_type(b)  # revealed: @Todo(starred unpacking)
      reveal_type(c)  # revealed: LiteralString
  10. carljm commented on May 30, 2025

    @carljm
    Contributor

    Re (1), I don't think it's hugely consequential either way, but I agree that if all else is equal, list[Unknown] is preferable.

    Re (2), I would be inclined to initially not promote literals and see if it causes issues in practice. I suspect it's unusual to mutate an unpacked list, but I could be wrong.

  11. added a commit that references this issue on Jun 3, 2025
    2289187
  12. added this to the Beta milestone on Jun 11, 2025
  13. added a commit that references this issue on May 1, 2026
    b25b6a4
  14. added a commit that references this issue on Aug 28, 2026
    8796a6e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    genericsBugs or features relating to ty's generics implementationhelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions