python / python/mypy

mypy rejects unpacking into same variable

Open
#14,786 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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 https://github.com/python/mypy/issues/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reported Python 3.10 snippet with mypy 1.0.1 and tracing how unpacking assignment is checked when the expanded variable is also the source. Add a regression test for this case, ensuring the same-variable unpacking is accepted while incompatible assignments remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.