python / python/mypy

multiple discarded variables with undescore (_) cause Incompatible types in assignment

Open
#9,962 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

Here is a silly example:

x = ((1, 'a', 1.4),)

for _, _, y in x:
    print(y)

The current output (mypy 0.800)

$ mypy t.py 
t.py:3: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 1 error in 1 file (checked 1 source file)

Here's a real world example:

import pkgutil

from somemod import _plugins

# trigger an import of all of the plugins
# https://github.com/python/mypy/issues/1422
plugins_path: str = _plugins.__path__  # type: ignore
mod_infos = pkgutil.walk_packages(plugins_path, f'{_plugins.__name__}.')
for _, name, _ in mod_infos:
    __import__(name, fromlist=['_trash'])
pyupgrade/_data.py:101: error: Incompatible types in assignment (expression has type "bool", variable has type "Union[MetaPathFinder, PathEntryFinder]")

Pitch

The discarded _ variable here is immediately clobbered before it could possibly be read, I'd like this to pass without error

(I couldn't find this searching, though I suspect there's a duplicate -- sorry in advance).

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 reproducing the two examples with mypy 0.800 and inspect the type-checking path for tuple unpacking in for-loop targets. Done means repeated discarded _ targets no longer cause incompatible-assignment errors while the examples type-check as intended.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.