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

Add an option to disable a message on the next line so a line does not become too long because of a disable #1682

Closed
lafrech opened this issue Sep 27, 2017 · 8 comments · Fixed by #4797
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors

Comments

@lafrech
Copy link

lafrech commented Sep 27, 2017

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 is 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)
            [...]
@PCManticore
Copy link
Contributor

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?

@lafrech
Copy link
Author

lafrech commented Sep 30, 2017

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.

@andy-maier
Copy link

andy-maier commented Nov 9, 2017

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

@altaurog
Copy link

altaurog commented May 13, 2018

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.

@fabiencelier
Copy link

Hello,
Any plan to support this in the future ?

ESLint has disable-next-line for instance

@dave-labscubed
Copy link

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

@hippo91
Copy link
Contributor

hippo91 commented Oct 17, 2020

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

@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component and removed Question labels Jun 17, 2021
@Pierre-Sassoulas Pierre-Sassoulas changed the title Disable one rule for only one line: trailing pragma makes line too long Add an option to disable a message on the next line so a line does not become too long because of a disable Jun 17, 2021
@Pierre-Sassoulas Pierre-Sassoulas added Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors labels Jun 18, 2021
DanielNoord added a commit to DanielNoord/pylint that referenced this issue Aug 4, 2021
Adding `# pylint: disable-next=msgid` to your file will disable the
message for the next line.
This closes pylint-dev#1682
Pierre-Sassoulas added a commit that referenced this issue Aug 5, 2021
* Add ``disable-next`` option
Adding `# pylint: disable-next=msgid` to your file will disable the
message for the next line.
This closes #1682

* Add documentation, rorganize the FAQ for disable/enable and add ref to the full doc

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
@Pierre-Sassoulas
Copy link
Member

Follow up for taking disable-param in the line length into account is here: #4802

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants