You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug report] Name '...' is not defined when wildcard importing local module #8497
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.
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'
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.
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 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.
Activity
ilevkivskyi commentedon Mar 6, 2020
Most likely you are using
--ignore-missing imports, and since you don't have an__init__.pyfile there mypy doesn't find that file.Also I have no idea how do you run Python, but without that
__init__.pyfile I getErwanDL commentedon Mar 6, 2020
There is actually an
__init__.pyfile, 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 importsflag.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 :The import succeeds, and as you can see the Python extension in VSCode even recognizes the type of
WORK_DIR(i.e. an instance ofPath). However, mypy displays the following 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.
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 commentedon Mar 7, 2020
Yes, we can't really proceed without this.
ErwanDL commentedon Mar 8, 2020
@ilevkivskyi I have found that my original issue originates from the fact that there was no
__init__.pyfile in the top level package (thefeedlypackage).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__.pyfiles to be present ? I think that they are technically not necessary anymore in Python 3.