Skip to content

false-positive: E0401 import-error for relative imports inside __init__.py (but only sometimes) #5319

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

Closed
Kwasniok opened this issue Nov 15, 2021 · 5 comments
Labels
Bug 🪲 Duplicate 🐫 Duplicate of an already existing issue Import system

Comments

@Kwasniok
Copy link

Bug description

Description

Relative imports inside a __init__.pyfile cause random E0401.

E.g.:
Say one has a module A.B.C.D inside a folder src such that

  • All the required __init__.py files exist for modules A, B, C and D
  • src/A/B/C/D/some_moduleX.py exists and defines SomeClassX where X=1,2,3
  • Content of src/A/B/C/D/__init__.py contains
from .some_module1 import SomeClass1
from .some_module2 import SomeClass2
from .some_module3 import SomeClass3

pylint will issue E0401 (pseudo-)randomly for some of the imports, even if all of them are similar. For which one the warnings are issued depends on the version/machine used - e.g. GitHub actions uses pylint v.2.11.1 and gives me different locations and a different amount of E0401.
If there are only a few relative imports across the entire project, pylint might not issue E0401 at all.

Python code using this module works flawlessly. E.g. via from A.B.C.D import SomeClass2

Workaround

Adding a .pylintrc

[MASTER]
init-hook='import sys; sys.path.append(".")'

fixes the issue.

Sad Note

I tried to come up with a minimalistic example with nested modules all containing relative imports in the __init__.py files importing dummy classes from the same level but for some reason this bug could not be provoked that way. I tried for quite a while but this is the best I can do.

Related

Configuration

No response

Command used

pylint src

Pylint output

************* Module src.A.B.C.D
src/A/B/C/D/__init__.py:2:0: E0401: Unable to import 'src.A.B.C.D.some_module2' (import-error)

# NOTE: The exact output is inconsistent across versions/machines.

Expected behavior

no warnings

Pylint version

pylint 2.10.2
astroid 2.7.3
Python 3.9.7 (default, Oct 10 2021, 15:13:22)
[GCC 11.1.0]

OS / Environment

ArchLinux 5.15.2-arch1-1 (Fri, 12 Nov 2021)

Additional dependencies

No response

@Kwasniok Kwasniok added Bug 🪲 Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 15, 2021
@Pierre-Sassoulas Pierre-Sassoulas added Import system and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 16, 2021
@Kwasniok
Copy link
Author

I noticed if I change the order of relative imports n a __init__.py, the exact same lines as before cause an issue.

@Kwasniok
Copy link
Author

I wrote a script which generates a minimal example with N nested submodules each including M files each. Unfortunately, the issue does not occur in the minimal example.
I also checked for cyclic import in the original code but there is none and the critical sections of the code are also successfully interpreted and executed by python.

make_minimal_example_E0401.txt (note: change the extension to .py since GitHub does not allow to upload script files)

@jacobtylerwalls
Copy link
Member

Thanks for the report. The indeterminacy is described in #3651, which appears to be fixed in recent pylint versions (anything shipping with astroid > 2.9.1). Feel free to reopen if still an issue.

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Jul 3, 2022
@jacobtylerwalls jacobtylerwalls added the Duplicate 🐫 Duplicate of an already existing issue label Jul 3, 2022
@jacwalte
Copy link

It looks like this issue is still present - I have tried on 2.14.4 and 2.14.5 with the same issue ... sometimes happening

We are currently migrating from a Windows pipeline to a Linux pipeline in ADO when this issue first occured. I am not sure if it has to do with Linux or not, but we are only able to reproduce it there.

We have several repos where we have made this change recently with no issue. Also, we still have the Windows pipeline running and not seeing this issue.

astroid-2.11.7 pylama-8.3.8 isort-5.10.1 mccabe-0.7.0 pycodestyle-2.8.0 pydocstyle-6.1.1 pyflakes-2.4.0

I will continue to go down in versions to see if one is stable

@jacobtylerwalls for visibility

@jacobtylerwalls
Copy link
Member

Thanks for testing, it's much appreciated! I'm going to take a note on #3651 that it may not be solved. (I declared victory too soon, it seems, since the regression test at #7117 isn't passing.) Still, unless there's a reason to treat these as separate issues, I'm thinking we can just keep #3651 open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Duplicate 🐫 Duplicate of an already existing issue Import system
Projects
None yet
Development

No branches or pull requests

4 participants