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

No warning for unused import in __init__.py #10162

Closed
rrthomas opened this issue Dec 31, 2024 · 3 comments · Fixed by #10179
Closed

No warning for unused import in __init__.py #10162

rrthomas opened this issue Dec 31, 2024 · 3 comments · Fixed by #10179
Labels
Documentation 📗 Good first issue Friendly and approachable by new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@rrthomas
Copy link

rrthomas commented Dec 31, 2024

Bug description

In a directory tmp I put the following code as __init__.py:

"""Test module"""
import sys

Pylint 3.3.3 reports no problems with this code:

$ pylint __init__.py 

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

This is odd, as I have an unused import.

If I copy the same code to test.py and rerun pylint, I get the expected error:

$ pylint test.py
************* Module tmp.test
test.py:2:0: W0611: Unused import sys (unused-import)

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

Configuration

Command used

pylint test.py

Pylint output

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

Expected behavior

__init___.py:2:0: W0611: Unused import sys (unused-import)

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

Pylint version

pylint 3.3.3
astroid 3.3.8
Python 3.12.3 (main, Nov  6 2024, 18:32:19) [GCC 13.2.0]

OS / Environment

Ubuntu 24.04

Additional dependencies

@rrthomas rrthomas added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Dec 31, 2024
@zenlyj
Copy link
Contributor

zenlyj commented Jan 2, 2025

by default unused-import checks are skipped for __init__.py files, but you may enable the check using the init-import configuration.

pylint --init-import yes __init__.py

alternatively specify in your pylintrc file:

[VARIABLES]
init-import=yes  # this is "no" by default

@zenlyj zenlyj added Question and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jan 2, 2025
@rrthomas
Copy link
Author

rrthomas commented Jan 2, 2025

Thanks, it never occurred to me that this would be deliberate behaviour! I found the documentation for the flag, but for those of us whose primary interaction with Pylint is via errors in our IDE (in my case, VS Codium), it would be nice if it could be documented in the warning's page (I checked https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/unused-import.html and found no mention), along with a rationale (I don't understand why this happens, it has merely confused and then annoyed me!).

Also, particular thanks for your quick and helpful reply @zenlyj.

@Pierre-Sassoulas Pierre-Sassoulas added Good first issue Friendly and approachable by new contributors Documentation 📗 Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Question labels Jan 3, 2025
amelenty pushed a commit to amelenty/pylint that referenced this issue Jan 12, 2025
- Explained why __init__.py files may have unused imports by design.
- Linked to the config option that overrides this behavior

Fixes pylint-dev#10162
@rrthomas
Copy link
Author

Much appreciated, many thanks! I'm sure future me, having forgotten all of this, will benefit from the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation 📗 Good first issue Friendly and approachable by new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants