How to suppress a warning in one line for pylint and Flake8 at the same time
Asked Answered
R

3

14

I would like to ignore a specific line in static code analysis.

For Flake8, I'd use the syntax # noqa: F401.

For pylint, I'd use the syntax # pylint: disable=unused-import.

As I am working on a code generation framework, I would like the code to support both linters. Is there a way to combine both directives such that both of them are correctly detected?

Ridglea answered 29/7, 2022 at 11:25 Comment(2)
Perhaps you need a space before the second #Lorenelorens
Thank you @MattClarke. That was a typo in my question. I did mean to include the space around itRidglea
W
20

both of these combinations work for me:

import os  # noqa: F401 # pylint:disable=unused-import
import sys  # pylint:disable=unused-import # noqa: F401
Weaver answered 29/7, 2022 at 12:45 Comment(0)
S
2

I was having similar issue. Having used pylint initially, I had that directive first which caused errors. Reversing the order so that flake8 is first worked for me.

# noqa: 501  pylint: disable=unused-argument
Sothena answered 9/10, 2022 at 13:56 Comment(0)
M
0

There are a couple options:

  1. Position the noqa: command first, followed by the Pylint directive:
    # noqa: 501 pylint: disable=unused-argument

  2. Position the directives in any order, but make sure they are both preceded by "# ":
    # pylint: disable=unused-argument # noqa: 501

  3. If you also want to have a comment on the same line before the directives you need to add another # after the comment:
    # comment # noqa: 501 pylint: disable=unused-argument

For me, it's easiest to think of the # as part of the directive and always include it with each one. Then I don't have to worry about whether or not I need to add an extra one.

Mensch answered 5/2 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.