Skip to content
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

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

Open
asottile opened this issue Jan 25, 2021 · 2 comments
Labels

Comments

@asottile
Copy link
Contributor

asottile commented Jan 25, 2021

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).

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 25, 2021

We already support some forms of this, so seems reasonable to get it to work here as well (#465). As a workaround, you can put _: Any on the line before

@asottile
Copy link
Contributor Author

ah, I see from that issue that another workaround is to sequester the loop into a function instead of executing it at the module scope

asottile added a commit to anthonywritescode/twitch-chat-bot that referenced this issue Jan 25, 2021
asottile added a commit to asottile/add-trailing-comma that referenced this issue Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants