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

per-line pylint suppression does not work inside multiline-imports like black formats things #6484

Open vapier opened 2 years ago

vapier commented 2 years ago

Bug description

# pylint: disable=missing-module-docstring, pointless-statement

from typing import (
    Any,
    AnyStr,
    Callable,
    Collection,
    Dict,
    Generator,
    Iterator,  # pylint: disable=unused-import
    List,
    Optional,
    Tuple,
    Union,
)

Any
AnyStr
Callable
Collection
Dict
Generator
List
Optional
Tuple
Union

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:3:0: W0611: Unused Iterator imported from typing (unused-import)

Expected behavior

warning should have been suppressed

Pylint version

pylint 2.11.1
astroid 2.8.4
Python 3.9.10 (main, Feb 22 2022, 13:54:07) 
[GCC 11.2.0]

OS / Environment

No response

Additional dependencies

No response

Pierre-Sassoulas commented 2 years ago

Thank you for opening the issue, I can reproduce with 2.14.0-dev0:

************* Module a
a.py:3:0: W0611: Unused Iterator imported from typing (unused-import)
a.py:10:0: I0021: Useless suppression of 'unused-import' (useless-suppression)

You should disable on line 3 instead but being able to disable on the specific line would be a nice enhancement.

vapier commented 2 years ago

there are a couple of workarounds, but none are ideal

putting it before the import line turns it off for the entire file after that point (since these comments operate on scope) and requires manual turning off with # pylint: enable=unused-import after the import line. that also disables it for all the imports on that line (when there are multiple ones like this typing example). trying to split the one import out to a dedicated line works only if the whole thing fits on one line, otherwise black/isort wraps it with parens, and it hits this bug again. when the comment is at the start of the line, black/isort insert blank lines around the section making it more jarring.

jacobtylerwalls commented 2 years ago

putting it before the import line turns it off for the entire file after that point

I think the suggestion was to do this:

from typing import (  # pylint: disable=unused-import
...
vapier commented 2 years ago

gotcha, thanks for the clarification. unfortunately, isort will reformat that when the first line is longer than 80 cols. i could put in a comment to disable the isort logic, but then i'm back in the same position of adding lots of suppressions :/.

jacobtylerwalls commented 2 years ago

I see! I think disable-next can be used to still prevent an all-block disable:

# pylint: disable-next=unused-import
from typing import ...
vapier commented 2 years ago

ah, i guess disable-next is a new thing. time for me to upgrade.