Pylint disable all warnings for a file

2019-01-22 05:16发布

问题:

We are using pylint within our build system. We have a python package within our code base that has throwaway code, and I'd like to disable all warnings for a module temporarily so I can stop bugging the other devs with these superfluous messages. Is there an easy way to pylint: disable all warnings for a module?

回答1:

From the PyLint FAQ

With Pylint < 0.25, add

# pylint: disable-all

at the beginning of the module.

Pylint 0.26.1 and up have renamed that directive to

# pylint: skip-file

(but the first version will be kept for backward compatibility).

In order to ease finding which modules are ignored a Information-level message I0013 is emited. With recent versions of Pylint, if you use the old syntax, an additional I0014 message is emited.



回答2:

PyLint has five "categories" for messages (of which I'm aware).

These categories were very obvious in the past, but the numbered Pylint messages have now been replaced by names. For example, C0302 is now too-many-lines. But the 'C' tells us that too-many-lines is a Convention message. This is confusing, because Convention messages frequently just show up as a warning, since many systems (such as Syntastic) seem to classify everything as either a warning or an error. However, the PyLint report still breaks things down into these categories, so it's still definitely supported.

Your question specifically refers to Warnings, and all PyLint Warning message names start with 'W'.

It was a bit difficult for me to track this down, but this answer eventually led me to the answer. PyLint still supports disabling entire categories of messages. So, to disable all Warnings, you would do:

disable=W

This can be used at the command-line:

$ pylint --disable=W myfile.py

Or, you can put it in your pylintrc file:

[MESSAGES CONTROL]
disable=W

Note: you may already have the disable option in your rc file, in which case you should append the 'W' to this list.

Or, you can put it inline in your code, where it will work for the scope into which it is placed:

# pylint: disable=W

To disable it for an entire file, it's best to put it at the very top of the file. However, even at the very top of the file, I found I was still getting the trailing-newlines warning message (technically a convention warning, but I'm getting to that).

In my case, I had a library written by someone from long ago. It worked well, so there was really no need to worry about modern Python convention, etc. All I really cared about were the errors that would likely break my code.

My solution was to disable all the Warning, Convention, and Refactoring messages for this one file only by placing the following PyLint command on the first line:

# pylint: disable=W,C,R

Aside from the aforementioned message for trailing newlines, this did exactly what I needed.



回答3:

Yes, you can specify # pylint: skip-file, but it is bad practice to disable all warnings for a file. Throwaway code should not exist in a branch that is analyzed by pylint.

If you want to disable specific warnings only, this can be done by adding a comment such as # pylint: disable=message-name to disable the specified message for the remainder of the file, or at least until # pylint: enable=message-name.

Example:

# pylint: disable=no-member
class C123:
    def __init__(self):
        self.foo_x = self.bar_x
# pylint: enable=no-member

class C456:
    def __init__(self):
        self.foo_x = self.bar_x


回答4:

Another option is to use the --ignore command line option to skip analysis for some files.



回答5:

My use case is to run pylint *.py to process all files in a directory, except that I want to skip one particular file.

Adding #pylint: skip-file caused pylint to fail with I: 8, 0: Ignoring entire file (file-ignored). Adding #pylint: disable=file-ignored does not fix that. Presumably, it's a global error rather than a file-specific one.

The solution was to include --disable=file-ignored in the pylint command options. It took way too long to figure this out; there shouldn't be a file-ignored error when you explicitly ignore a file.



标签: python pylint