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

[Bug report] Name '...' is not defined when wildcard importing local module #8497

Closed
ErwanDL opened this issue Mar 5, 2020 · 4 comments
Closed

Comments

@ErwanDL
Copy link

ErwanDL commented Mar 5, 2020

Mypy version : 0.761

Mypy outputs an error Name 'MY_CONSTANT' is not defined when MY_CONSTANT has been imported through a wildcard import of a local module. The error doesn't appear when I wildcard import a standard library module for instance (such as from math import *.

This is definitely a false positive, since I can run main.py without any error.

Example code :

# main.py
from mypackage.my_test_module import *

print(MY_CONSTANT)

The module in question :

# my_test_module.py
MY_CONSTANT = "Hello world"

Simplified project architecture :

mypackage
┣ my_test_module.py
┗ main.py
@ilevkivskyi
Copy link
Member

Most likely you are using --ignore-missing imports, and since you don't have an __init__.py file there mypy doesn't find that file.

Also I have no idea how do you run Python, but without that __init__.py file I get

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from mypackage.my_test_module import *
ModuleNotFoundError: No module named 'mypackage'

@ErwanDL
Copy link
Author

ErwanDL commented Mar 6, 2020

There is actually an __init__.py file, I tried to give a minimal reproducible example (the real life project on which this bug happens to me is much more complex) and didn't think it was necessary, my bad. I'll try to be more comprehensive.

Note : I do not run mypy with the --ignore-missing imports flag.

As you can see on the image below, I import a bunch of constants (one of them is called WORK_DIR) with the first line :

from feedly.ml_ner_business.globals import *

The import succeeds, and as you can see the Python extension in VSCode even recognizes the type of WORK_DIR (i.e. an instance of Path). However, mypy displays the following error :

Name 'WORK_DIR' is not defined mypy(error)

Pylint doesn't detect any error here, neither does the Python extension, and this module works without a problem at runtime, so I can assure you that this seems to be a false positive on mypy's side.

Screen Shot 2020-03-06 at 3 42 54 PM

I am wondering if this is linked to the fact that I use Poetry to handle the dependencies (including the local packages) on this project. I can try and create an actual minimal reproducible project this weekend so that it is clearer where the bug may originate from, if you want.

@ilevkivskyi
Copy link
Member

I can try and create an actual minimal reproducible project this weekend so that it is clearer where the bug may originate from, if you want.

Yes, we can't really proceed without this.

@ErwanDL
Copy link
Author

ErwanDL commented Mar 8, 2020

@ilevkivskyi I have found that my original issue originates from the fact that there was no __init__.py file in the top level package (the feedly package).

However, I am wondering if this is really expected behavior, since when using named imports (as opposed to wildcard imports), mypy apparently has no problem recognizing the imported variables even if __init__.py files are missing. This behavior seems a little inconsistent.

On top of that, is there a reason why mypy would sometimes require __init__.py files to be present ? I think that they are technically not necessary anymore in Python 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants