Pylint: Disable one rule for only one line: trailing pragma makes line too long

Created on 27 Sep 2017  路  7Comments  路  Source: PyCQA/pylint

Hi. I'm trying to ignore a pylint error for a single line and I don't know how it should be done.

Example use case:

        if self._wrapper_class is not None:
            data = self._wrapper_class(data)
            [...]

The data = ... line triggers a not-callable error I would like to mask.

I'm not sure how to do that.

This triggers a line-too-long error, unless I also disable line-too-long in the line, which makes it even longer (in my real-life use case, data is a longer word and the line is more than 80 characters):

        if self._wrapper_class is not None:
            data = self._wrapper_class(data)  # pylint: disable=not-callable
            [...]

This disables the warning for the whole if context:

        if self._wrapper_class is not None:
            # pylint: disable=not-callable
            data = self._wrapper_class(data)
            [...]

This if functionally correct but a bit too verbose:

        if self._wrapper_class is not None:
            # pylint: disable=not-callable
            data = self._wrapper_class(data)
            # pylint: enable=not-callable
            [...]

This was discussed a while ago on a mailing-list thread.

I guess this is bound to happen as long as the only way to disable a warning for a single line is to add a trailing pragma, especially since pylint supports disable by (long) name rather than (short) code.

Wouldn't it be handy to have some sort of ignore-for-next-statement pragma?

        if self._wrapper_class is not None:
            # pylint: disable-next=not-callable
            data = self._wrapper_class(data)
            [...]
question

Most helpful comment

Hello,
Any plan to support this in the future ?

ESLint has disable-next-line for instance

All 7 comments

We are already stripping the pragma for each line before checking for line-too-long: https://github.com/PyCQA/pylint/blob/14da5ce258d3818037304a6e0f61aba1462e251d/pylint/checkers/format.py#L1004, so it shouldn't not be taken into account

Which pylint version are you using? Can you add a minimal test case for which I can reproduce this?

Just tested here on a new virtualenv.

pylint==1.7.4
flake8==3.4.1

The trailing pragma does trigger a "line too long" error, but from flake8, not from pylint. I guess I got confused, the other day. My apologies.

Trying to silence flake8, this

    data = self._wrapper_class(data)  # pylint: disable=not-callable  # noqa

triggers the not-callable error. Apparently, pylint loses its pragma in the process.

This:

    data = self._wrapper_class(data)  # pylint: disable=not-callable noqa

triggers line too long from flake8.

This yields no error:

    data = self._wrapper_class(data)  # noqa # pylint: disable=not-callable
    # or even shorter
    data = self._wrapper_class(data)  # noqa pylint: disable=not-callable

Is this the recommended approach?

I think the cons are that

  • (A bit) too verbose: I'd rather avoid the #聽noqa
  • It disables flake8 for the whole line, not only the line too long rule
  • I'd rather have #聽noqa at the end and keep more important stuff (not callable) closer to the code

I realize that this is not a pylint issue as I thought but rather a pylint/flake8 issue. Anyway, let alone flake8, it is a pity that pylint, while enforcing coding style, generates exceptions to its own rules. Even if it auto-ignores its pragmas while enforcing the line-too-long rule, the line is still too long for the editor. Maybe not as bad for a comment as it would be for actual code, but still.

From this perspective, I still think a pragma that would disable a rule for the next line would be nice.

I won't argue about it, though, because I don't know pylint's code architecture and this might be a huge breaking change for little benefit. Besides, I'm not an experienced pylint user, so my vision might be biased towards my use case.

I'm also interested in knowing which alternative has consensus among the community. Should I be writing it like this ?

    data = self._wrapper_class(data)  # noqa pylint: disable=not-callable

Feel free to redirect me to stackoverflow if I'm abusing the bugtracker for what looks more like a question.

I have run into the same issue and am interested in a solution, and I found how to improve the previously listed solutions by disabling just the line too long issue for flake8:

if type(test_dict) not in dict_types:  # noqa: E501 pylint: disable=unidiomatic-typecheck

IMO stripping the pragma before checking line length defeats the purpose of line-too-long. I for one arrange my editor window(s) on the premise of a certain code width. I want to see those pragmas too. Here pylint not only fails to enforce the maximum width, but requires an overflowing line! That is not too helpful.

Hello,
Any plan to support this in the future ?

ESLint has disable-next-line for instance

This seems like a basic feature and pretty integral to anybody using any kind of linter (eg. black for spacing and styling).

@dave-labscubed thanks for your pertinent remark. Do not hesitate to make a PR to see this basic feature implemented.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrginglymus picture mrginglymus  路  3Comments

sambarluc picture sambarluc  路  3Comments

DGalt picture DGalt  路  3Comments

thanatos picture thanatos  路  3Comments

adamtheturtle picture adamtheturtle  路  3Comments