Skip to content

mypy rejects unpacking into same variable #14786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
PeterJCLaw opened this issue Feb 26, 2023 · 3 comments
Open

mypy rejects unpacking into same variable #14786

PeterJCLaw opened this issue Feb 26, 2023 · 3 comments
Labels
bug mypy got something wrong topic-usability

Comments

@PeterJCLaw
Copy link
Contributor

Bug Report

I believe the following code should be fine, however mypy rejects the case where we're unpacking into the variable which was expanded:

foo: tuple[int, ...] = ()

# Ok
bar, *quox = foo

# Incompatible types in assignment (expression has type "List[int]", variable has type "Tuple[int, ...]")  [assignment]
bar, *foo = foo

There are a number of similar issues reported, the closest being #9706, however I believe this is distinct as it reproduces when the generic type is int (though my original code was using str). This suggests a different root cause.

Your Environment

  • Mypy version used: 1.0.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10
@PeterJCLaw PeterJCLaw added the bug mypy got something wrong label Feb 26, 2023
@sterliakov
Copy link
Collaborator

I'd say it's not a bug: you really have foo defined as a tuple and assign a list to it later, why should this be allowed? This code passes with --allow-redefinition, as expected.

@PeterJCLaw
Copy link
Contributor Author

PeterJCLaw commented Feb 26, 2023

Oh, huh. For some reason I thought that *foo would capture the values into a tuple not a list.

Happy to close, though I wonder if the error could be clearer about this?
The error position is on the RHS expression, not the LHS assignment, yet in this case the RHS expression has no control over the error and it's the LHS which will need updating to be correct.

@JelleZijlstra
Copy link
Member

Agree that the error could be improved here. The wording indicates the RHS is a list, but it's not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-usability
Projects
None yet
Development

No branches or pull requests

3 participants