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

Undefined name error when using late wildcard imports #5908

Closed
IwoHerka opened this issue Nov 17, 2018 · 4 comments
Closed

Undefined name error when using late wildcard imports #5908

IwoHerka opened this issue Nov 17, 2018 · 4 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@IwoHerka
Copy link

IwoHerka commented Nov 17, 2018

Hello,

I've encountered a bug (or I think so) when using late wildcard imports. It can be reproduced as follows:

file1.py

def foo() -> int:
    return bar()

from file2 import *
assert foo() == 1

file2.py

def bar() -> int:
    return 1

This yields:

file1.py:2: error: Name 'bar' is not defined

Version with

from file2 import bar

works fine (i.e. no errors are thrown).

This version works too:

from file2 import *

def foo() -> int:
    return bar()

assert foo() == 1

I'm using version 0.641 installed from PyPI. I encounter the same behaviour when installed from Git master (as of 2680c3).

I'm not using any options (just mypy file1.py).

Python version is 3.6.6.

@ilevkivskyi
Copy link
Member

We are planning a major redesign of our semantic analyzer in next months that will likely solve this kind of issues. For now you can use the workarounds you found.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal false-positive mypy gave an error on correct code labels Nov 17, 2018
@JukkaL
Copy link
Collaborator

JukkaL commented Nov 19, 2018

The underlying problem is that mypy currently doesn't support forward references to a star import. from x import * is processed at a later stage of analysis than other imports, which breaks forward references. Forward references to imported names in general are problematic at the moment though; #4687 is a related issue.

@arquolo
Copy link

arquolo commented Dec 9, 2019

Another bug with wildcard imports (typical code from asyncio.__init__):

from .submodule import *
__all__ = submodule.__all__

This yields:
file.py:2: error: Name `submodule` is not defined
Using mypy 0.750 from pip.

@msullivan
Copy link
Collaborator

Looks like this was fixed by the new semantic analyzer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests

5 participants