pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.31k stars 1.14k forks source link

pylint fails to import module if the script is named the same way #7093

Open bdrung opened 2 years ago

bdrung commented 2 years ago

Bug description

Apport provides a module name apport, but also a script named data/apport. Pylint throws a bunch of failures for data/apport. This problem can be reproduces with this Shell script:

#!/bin/sh
mkdir name
touch name/__init__.py
printf '# pylint: disable=missing-docstring\ndef inc(val):\n    return val + 1\n' > name/math.py
mkdir data
printf '#!/usr/bin/python3\n# pylint: disable=missing-docstring\n\nimport name\nimport name.math\n\nprint(name.math.inc(41))\n' > data/name
chmod +x data/name
PYTHONPATH=. data/name

It will create name/math.py with

# pylint: disable=missing-docstring
def inc(val):
    return val + 1

and data/name with

#!/usr/bin/python3
# pylint: disable=missing-docstring

import name
import name.math

print(name.math.inc(41))

After setting PYTHONPATH, data/name will execute successfully.

Configuration

No response

Command used

pylint name data/name

or

PYTHONPATH=. pylint name data/name

or

pylint --init-hook='import sys; sys.path.append(".")' name data/name

Pylint output

data/name:4:0: W0406: Module import itself (import-self)
data/name:5:0: E0611: No name 'math' in module 'name' (no-name-in-module)
data/name:7:6: I1101: Module 'name' has no 'math' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. (c-extension-no-member)

Expected behavior

pylint should produce no output.

Pylint version

pylint 2.14.4
astroid 2.11.6
Python 3.10.5 (main, Jun  8 2022, 09:26:22) [GCC 11.3.0]

OS / Environment

Ubuntu 22.04 (jammy)

Additional dependencies

No response

jacobtylerwalls commented 2 years ago

Thanks for the report. Fixed in PyCQA/astroid#1536, which will be part of pylint 2.15's upgrade to astroid 2.12. This may even be a duplicate of #2648 but I'm reluctant to declare that until we have distilled a mergeable test case from it.

This issue has a clear test case, which I would welcome adding to the suite. Would you like to prepare a PR? It will pass as soon as we merge #7153.

Because this issue requires a particular directory structure, and might not reproduce cleanly with the generic system for functional tests in tests/functional, you might follow the models at #7117 or #7113.

rostero1 commented 2 years ago

I spent most of my weekend debugging a similar issue. The project root (with requirements.txt) is named django. I kept getting import errors on django, but all the other modules were detected just fine.

Can you confirm this will be fixed in 2.15.0? Also, is there a date announced for 2.15.0 or a workaround until that time?

jacobtylerwalls commented 2 years ago

Can you confirm this will be fixed in 2.15.0?

The cases reported to this issue board relating to imports can be deceivingly different. It would be a great help if you could install pylint from the main branch with pip install git+https://github.com/PyCQA/pylint.git and let us know if it resolves your issue. That install should pull astroid==2.12.2.

Also, is there a date announced for 2.15.0 or a workaround until that time?

Unfortunately not, the workaround is to install from the main branch as above.

Pierre-Sassoulas commented 2 years ago

You can follow the release at https://github.com/PyCQA/pylint/milestone/56. I'm going to remove this from the 2.15.0 milestone as we want to release soon and as if seems like a real release would be convenient be helpful for testing on your side :)

jacobtylerwalls commented 1 year ago

Still reproduces on pylint at 1baa10e250406d740d0c83f2902e949d84c1ecd8 and astroid at 39d9cafb8b39432957f4e640d5ef222b66a7096e, so https://github.com/PyCQA/pylint/issues/7093#issuecomment-1183091900 was not correct.