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

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

Open
bdrung opened this issue Jun 30, 2022 · 5 comments
Open

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

bdrung opened this issue Jun 30, 2022 · 5 comments
Labels
Astroid Related to astroid namespace-package Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@bdrung
Copy link
Contributor

bdrung commented Jun 30, 2022

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

@bdrung bdrung added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 30, 2022
bdrung added a commit to canonical/apport that referenced this issue Jun 30, 2022
pylint fails to import the `apport` module for `data/apport`, because it
has the same name. See bug pylint-dev/pylint#7093

Signed-off-by: Benjamin Drung <bdrung@ubuntu.com>
@Pierre-Sassoulas Pierre-Sassoulas added Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jun 30, 2022
@jacobtylerwalls
Copy link
Member

Thanks for the report. Fixed in pylint-dev/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.

@jacobtylerwalls jacobtylerwalls added tests Needs PR This issue is accepted, sufficiently specified and now needs an implementation Astroid Related to astroid namespace-package and removed Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine labels Jul 13, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.15.0 milestone Jul 13, 2022
bdrung added a commit to bdrung/apport that referenced this issue Jul 18, 2022
pylint incorrectly complains about import-self on importing `apport` in
`data/apport`.

See pylint-dev/pylint#7093
Signed-off-by: Benjamin Drung <bdrung@ubuntu.com>
@rostero1
Copy link

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
Copy link
Member

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
Copy link
Member

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 :)

@Pierre-Sassoulas Pierre-Sassoulas removed this from the 2.15.0 milestone Aug 25, 2022
@jacobtylerwalls
Copy link
Member

Still reproduces on pylint at 1baa10e and astroid at 39d9cafb8b39432957f4e640d5ef222b66a7096e, so #7093 (comment) was not correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astroid Related to astroid namespace-package Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

4 participants