-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open
Labels
C: undefined-variableIssues related to 'undefined-variable' checkIssues related to 'undefined-variable' checkFalse Positive 🦟A message is emitted but nothing is wrong with the codeA message is emitted but nothing is wrong with the codeImport systemNeeds investigation 🔬A bug or crash where it's not immediately obvious what is happenningA bug or crash where it's not immediately obvious what is happenningnamespace-package
Description
Given:
> pylint --version
pylint 2.6.0
astroid 2.4.2
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
> tree .
.
├──bug
│ ├── __init__.py
│ └── module.py
└──run_bug.py
with bug/__init__.py
:
from .module import *
__all__ = module.__all__
and bug/module.py
:
__all__ = ['func']
def func() -> None:
print('works')
and run_bug.py
:
from bug import func
func()
code works:
> python -c "import bug ; bug.func()"
works
and this also works:
> python run_bug.py
works
and bug is here:
> pylint bug run_bug --disable=C
************* Module bug
bug\__init__.py:3:10: E1101: Class 'module' has no '__all__' member (no-member)
bug\__init__.py:3:10: E0602: Undefined variable 'module' (undefined-variable)
Problem is that when *
is imported from bug.module
, module
becomes imported to bug
, but PyLint thinks that it doesn't.
All I wanted is to delegate filling of __all__
to submodules, like in asyncio.
Explanation is here.
Metadata
Metadata
Assignees
Labels
C: undefined-variableIssues related to 'undefined-variable' checkIssues related to 'undefined-variable' checkFalse Positive 🦟A message is emitted but nothing is wrong with the codeA message is emitted but nothing is wrong with the codeImport systemNeeds investigation 🔬A bug or crash where it's not immediately obvious what is happenningA bug or crash where it's not immediately obvious what is happenningnamespace-package
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
jamesbraza commentedon Nov 6, 2020
I believe I'm hitting this same bug. Seems like the problem lies with some combination of:
__all__
in one fileMinimal Repro
other_file
:main_file
:This results in the following error message:
Output of
pylint --version
:hippo91 commentedon Nov 19, 2020
@jamesbraza i'm trying to reproduce your case but i end up with
ImportError: attempted relative import with no known parent package
when runningpython3 main.py
. The file structure is:jamesbraza commentedon Nov 19, 2020
Hi @hippo91 sorry it looks like I was invoking my minimal repro differently. The error you're getting lies in the concepts from this stack overflow question: Relative imports in Python 3.
There are multiple options at invocations for directly with
python3
, including:.
fromfrom .other_file import *
inmain_file.py
and usepython3 main_file.py
undef_var_bug
and changeimport
to include parent folderUpdate import in
from undef_var_bug.other_file import *
, and invoke viapython3 -m undef_var_bug.main_file
.Let me know if neither of those work for you.
hippo91 commentedon Nov 26, 2020
@jamesbraza , i tested with the first solution. So i have the following code structure:
with:
and
Then linting is ok:
The false positive
undefined-variable
message emission doesn't occur. Do you agree?arquolo commentedon Nov 26, 2020
@hippo91
No,
undefined-variable
message does occur, you invoke it wrong. I have updated issue[-]E0602 False positives[/-][+]E1101 False positive[/+]jamesbraza commentedon Nov 27, 2020
@hippo91 so I copied what you'd set up, and I figured out the problem, I don't believe your code actually runs. I know because when I ran it via
python -m bug_pylint_3298.main_file
, the result was:ModuleNotFoundError: No module named 'other_file'
Your
main_file.py
begins withfrom other_file
:However, it should be:
And now the module executes as intended. And then when you invoke
pylint
, the bug surfaces:To fix it in this case, one can add an
__init__.py
to thebug_pylint_3298
directory, and it seems the error disappears.Seems like
pylint
:import-error
undefined-variable
, since it can't seem to parse the wildcard import (hence the precedingimport-error
)jamesbraza commentedon Nov 27, 2020
However, @arquolo seems to have changed the issue to be from
E0602 (undefined-variable)
toE1101 (no-member)
w.r.t.module.__all__
, which now makes my contributions here being sort of off-topic.@arquolo I think your problem may be in the fact that
python -c
is different from just invokingpylint
.Invoking via
python -c
runs the input as if it's a__main__
, which is why this works. However, if you were to try and invoke in a plain way, you can see Python can't actually run:This may be why
pylint
can't properly parse the wildcard import... somewhere behind the scenes the real problem may be that the module can't be run. Just my guess, but I am not sure.[-]E1101 False positive[/-][+]E0602 False positive[/+]arquolo commentedon Nov 27, 2020
@jamesbraza
I have updated issue, and yet it's still E0602 (it was 2 am here, I misread the log).
It doesn't depend whether module is imported in
python -c
, or in a plain way in python script, it still works.I'm not trying to run a package, but import it.
Problem is not with star import, but with star import from subpackage, what imports subpackage too, and Pylint fails with last.
[-]E0602 False positive[/-][+]E0602 (undefined-variable) - False positive[/+]hippo91 commentedon Nov 28, 2020
@arquolo and @jamesbraza thanks for your remarks. I'm now able to reproduce the bug.
Here is attached a reproducer. I can confirm that adding an empty
__init__.py
file makes the bug disappear. That's why this issue may be linked to #3944.bug_pylint_3298.tar.gz
NeilGirdhar commentedon Aug 31, 2021
I've been running into the same problem. It is as simple as
in any file inside a package. Pylint just needs to do what Python does, which is to add an implicit
import x
.stdedos commentedon Sep 19, 2023
Is this similar to
and https://github.com/hoel-bagard/matplotlib-stubs/blob/f24bcb7808881bf5a1dd7271ce50b0ad0fb0051f/src/matplotlib-stubs/__init__.pyi#L36-L38 ?