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

False "import-self" when importing a package from stdlib with same name than local subpackage #3665

Closed
niander opened this issue Jun 3, 2020 · 13 comments

Comments

@niander
Copy link

niander commented Jun 3, 2020

I have a package named logging, which is inside a namespace package (python 3.0 style)

bar
├── logging
│   ├─ __init__.py

in __init__.py

from logging import DEBUG, INFO, WARN

Here, logging refers to the std library. But, pylint is giving W0406: import-self for the statement.

Steps to reproduce

  1. Run python -m pylint src/bar/logging/__init__.py

Current behavior

************* Module logging
src/bar/logging/__init__.py:3:0: W0406: Module import itself (import-self)
src/bar/logging/__init__.py:3:0: W0406: Module import itself (import-self)
src/bar/logging/__init__.py:3:0: W0406: Module import itself (import-self)

Expected behavior

No error because logging is not a reference to itself.

pylint --version output

pylint 2.4.4
astroid 2.3.3
Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)]
@Project-Magenta
Copy link

It is a reference to itself. Try it!

@niander
Copy link
Author

niander commented Jun 4, 2020

It is a reference to itself. Try it!

Why? It is an absolute import. It is referencing the standard library logging. If I wanted to reference itself it would be from .logging import ...
This is no implicit relative import, it has been deprecated since Python 3

@DanielNoord
Copy link
Collaborator

@Pierre-Sassoulas I cannot reproduce this. Might have been fixed!

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.10.0 milestone Aug 5, 2021
@GTcreyon
Copy link

GTcreyon commented Sep 3, 2024

I've experienced the same (or a very similar) issue.
pylint --version output:

pylint 3.2.6
astroid 3.3.2
Python 3.12.5 (main, Aug  9 2024, 08:20:41) [GCC 14.2.1 20240805]

My circumstances are more or less identical: I have a file named logging.py, with the first line from logging import Formatter, StreamHandler, getLogger, INFO.

Upon running pylint /path/to/file/logging.py, I get W0406: Module import itself (import-self) repeated 4 times.

As far as I can tell, the only way to resolve this is to either disable the warning or rename the file.

@davetapley
Copy link

davetapley commented Oct 30, 2024

I just got this too, not sure if regression:

pylint 3.2.2
astroid 3.2.4
Python 3.12.2 (main, Sep 12 2024, 02:29:09) [GCC 9.4.0]

In my case my file is called falcon.py and I e.g.:

from falcon.http_error import HTTPError

which causes it too, so not limited to stdlib.

@dubiousjim
Copy link

dubiousjim commented Oct 31, 2024

My circumstances are more or less identical: I have a file named logging.py, with the first line from logging import Formatter, StreamHandler, getLogger, INFO.

Upon running pylint /path/to/file/logging.py, I get W0406: Module import itself (import-self) repeated 4 times.

Confirmed for:

pylint 3.1.0
astroid 3.1.0
Python 3.12.7 (main, Oct  7 2024, 11:30:19) [GCC 13.2.1 20240309]

Running python ./logging.py executes fine.

I have PYTHONSAFEPATH set, so working directory isn't on sys.path. (Error persists even if the file being checked isn't in the working directory, and even if I run pylint with an init-hook that explicitly writes sys.path to ensure that file being checked isn't in module search path.)

@dubiousjim
Copy link

Related issues: #5151, #6535, #7289

@jacobtylerwalls
Copy link
Member

@dubiousjim is the situation any different with pylint-dev/astroid#2223?

@dubiousjim
Copy link

dubiousjim commented Oct 31, 2024

@jacobtylerwalls No that doesn't fix it. I checked out the branch linked in that pull request (specifically commit 849f68e0), built the package and installed in my sys.path. And moved my distro's astroid out of the sys.path. Then:

$ pylint --version
pylint 3.1.0
astroid 3.0.0a10-dev0
Python 3.12.7 (main, Oct  7 2024, 11:30:19) [GCC 13.2.1 20240309]

$ mkdir not_on_sys_path
$ echo 'import logging' > not_on_sys_path/logging.py
$ pylint -d unused-import not_on_sys_path/logging.py 
************* Module logging
not_on_sys_path/logging.py:1:0: W0406: Module import itself (import-self)

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

I left my existing pylint package in place; only changed the version astroid being used.

@dubiousjim
Copy link

Build steps:

$ git clone /~https://github.com/jacobtylerwalls/astroid.git
$ cd astroid/
$ git switch --detach origin/same-name-stdlib
$ mkdir .dist; gpep517 build-wheel --wheel-dir .dist
$ mkdir .dest; python3 -m installer -d .dest .dist/astroid-*.whl
$ sudo cp -R .dest/usr/lib/python3.12/site-packages/astroid /usr/local/lib/python3.12/

Then disable my distro's copy of astroid (in /usr/lib).

@jacobtylerwalls
Copy link
Member

Got it, appreciate you finding out and reporting back.

@dubiousjim
Copy link

I just now noticed that if a package (file or directory containing __init__.py) which displays this issue is on my module search path, and instead of providing pylint with a path to the package, I just say pylint $PACKAGENAME, then the error does not arise.

@dubiousjim
Copy link

Here's a more exhaustive detailing of when the issue does/doesn't arise:

$ PYTHONSAFEPATH=1 python -c 'import sys; print(sys.path)'
['/home/jim/dev/python/user-packages', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/usr/lib/python3.12/site-packages', '/usr/local/lib/python3.12']

$ mkdir -p /home/jim/dev/python/user-packages/foo

$ touch /home/jim/dev/python/user-packages/foo/__init__.py

$ echo 'import logging' > /home/jim/dev/python/user-packages/foo/logging.py

$ pylint -d unused-import /home/jim/dev/python/user-packages/foo/logging.py # regular package, supply by filesystem path: no error

------------------------------------
Your code has been rated at 10.00/10

$ pylint -d unused-import foo.logging # regular package, supply by module path: no error

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

$ rm /home/jim/dev/python/user-packages/foo/__init__.py # now make foo a namespace package

$ pylint -d unused-import foo.logging # namespace package, supply by module path: no error

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

$ pylint -d unused-import /home/jim/dev/python/user-packages/foo/logging.py # namespace package, supply by filesystem path: ERROR
************* Module logging
dev/python/user-packages/foo/logging.py:1:0: W0406: Module import itself (import-self)

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

$ mv /home/jim/dev/python/user-packages/foo /home/jim/dev/python/ # that is, no longer on module search path

$ pylint -d unused-import /home/jim/dev/python/foo/logging.py # not on search path, supply by filesystem path: ERROR
************* Module logging
dev/python/foo/logging.py:1:0: W0406: Module import itself (import-self)

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

$ pylint -d unused-import --recursive=y /home/jim/dev/python/foo # not on search path, supply by --recursive=y on parent dir: ERROR
************* Module logging
dev/python/foo/logging.py:1:0: W0406: Module import itself (import-self)

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

$ pylint --version
pylint 3.1.0
astroid 3.1.0
Python 3.12.7 (main, Oct  7 2024, 11:30:19) [GCC 13.2.1 20240309]

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

9 participants