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

Regression: explicit relative imports in modules with __init__.py trigger E0402 relative-beyond-top-level #3624

Closed
jnsnow opened this issue May 14, 2020 · 2 comments

Comments

@jnsnow
Copy link
Contributor

jnsnow commented May 14, 2020

Issue #3528 and associated PR #3569 appear to have caused a regression in how modules with __init__.py are perceived.

If I install a dev copy of 4756b3c (origin/master as of writing) and revert this change, the explicit relative import works again.

Steps to reproduce

layout:

qemu/
  lib/
    __init__.py
    qmp.py
    machine.py
    qtest.py

qtest.py has an import statement like this:

from .machine import QEMUMachine

If CWD is qemu/lib and I execute:
> pylint qtest.py

2.4.4:

Your code has been rated at 10.00/10 (previous run: 9.82/10, +0.18)

2.5.0:

Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

but 2.5.1:

qtest.py:24:0: E0402: Attempted relative import beyond top-level package (relative-beyond-top-level)

Current behavior

Relative imports trigger relative-beyond-top-level messages.

Expected behavior

As the relative imports function correctly during runtime, pylint should not produce an Error.

pylint --version output

Current setup:

pylint 2.5.1
astroid 2.4.1
Python 3.7.7 (default, Mar 13 2020, 21:39:43) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]

More info:

From what I can tell, it seems as if the fact that I have an __init__.py is getting ignored (?), and my file is being processed as if it wasn't in a package. I seem to recall that what 'self.name' resolved to inside relative_to_absolute_name was surprisingly different, but naturally I didn't write it down...

@jnsnow
Copy link
Contributor Author

jnsnow commented Oct 12, 2020

@PCManticore I have one more facet of this to report that differs slightly from my above report. It might be the same as in #3651 ?

The structure I am prototyping for QEMU upstream is now:

python/
  qemu/
    core/
      __init__.py
      accel.py
      qmp.py
      machine.py
      qtest.py

i.e. qemu is a PEP420 namespace, core is the package.

__init__.py contains these lines:

from .accel import kvm_available, list_accel, tcg_available
from .machine import QEMUMachine
from .qmp import QEMUMonitorProtocol
from .qtest import QEMUQtestMachine, QEMUQtestProtocol

Starting from the containing directory, i.e. python/ (so the qemu/ namespace folder is in the CWD) WITHOUT the package installed to the current environment:

> pylint qemu

2.4.4: OK

2.5.0:

************* Module qemu
qemu/__init__.py:1:0: F0010: error while code parsing: Unable to load file qemu/__init__.py:
[Errno 2] No such file or directory: 'qemu/__init__.py' (parse-error)

2.5.1, 2.5.2, 2.5.3, 2.6.0: (Tested on 3.6. 2.6.0 was tested in 3.6, 3.7, and 3.8)

************* Module qemu.core
qemu/core/__init__.py:30:0: E0401: Unable to import 'qemu.core.accel' (import-error)
qemu/core/__init__.py:31:0: E0401: Unable to import 'qemu.core.machine' (import-error)
qemu/core/__init__.py:32:0: E0401: Unable to import 'qemu.core.qmp' (import-error)
qemu/core/__init__.py:33:0: E0401: Unable to import 'qemu.core.qtest' (import-error)

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.7.0, 2.7.x Feb 20, 2021
bonzini pushed a commit to qemu/qemu that referenced this issue May 30, 2021
Pylint 2.5.x - 2.7.x have regressions that make import checking
inconsistent, see:

pylint-dev/pylint#3609
pylint-dev/pylint#3624
pylint-dev/pylint#3651

Pinning to 2.4.4 is worse, because it mandates versions of shared
dependencies that are too old for features we want in isort and mypy.
Oh well.

Signed-off-by: John Snow <[email protected]>
Reviewed-by: Cleber Rosa <[email protected]>
Message-id: [email protected]
Signed-off-by: John Snow <[email protected]>
bonzini pushed a commit to qemu/qemu that referenced this issue Jun 2, 2021
Pylint 2.5.x - 2.7.x have regressions that make import checking
inconsistent, see:

pylint-dev/pylint#3609
pylint-dev/pylint#3624
pylint-dev/pylint#3651

Pinning to 2.4.4 is worse, because it mandates versions of shared
dependencies that are too old for features we want in isort and mypy.
Oh well.

Signed-off-by: John Snow <[email protected]>
Reviewed-by: Cleber Rosa <[email protected]>
Message-id: [email protected]
Signed-off-by: John Snow <[email protected]>
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.x, 2.15.0 May 9, 2022
@DanielNoord DanielNoord removed this from the 2.15.0 milestone Aug 23, 2022
@clavedeluna
Copy link
Contributor

I've attempted to reproduce this issue and can confirm that with pylint 2.15.4 the relative-beyond-top-level is not raised.

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

No branches or pull requests

5 participants