Coder Perfect

How can I turn off a Pylint warning?


In Pylint 0.21.1 (if it matters: astng 0.20.1, common 0.50.3, and Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56), I’m trying to disable warning C0321 (“more than one statement on a single line” — I frequently put if statements with short single-line results on the same line), I’m trying to disable warning C0321 (“more than one statement on a single line” — I often put if statements

I tried including disable=C0321 in the Pylint configuration file, but Pylint still reports it. Variations on that line (such as disable=0321 or disable=C321) are noted as errors, indicating that Pylint understands the option. It’s merely a case of ignoring it.

Is this a Pylint problem, or am I making a mistake? Is there a way to get out of this?

I’d like to get rid of some of this background noise.

Asked by Head Geek

Solution #1

The output of pylint —generate-rcfile is as follows:


# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time.

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).

So it appears that the disable= line/s should be inside a section [MESSAGES CONTROL] in your /.pylintrc.

Answered by Chris Morgan

Solution #2

I ran into this issue when using Eclipse and came up with the following solution:

Hold Shift, right-click, and choose to open the windows command in the pylint folder (e.g. C:Python26Libsite-packagespylint). Type: --generate-rcfile > standard.rc

The standard.rc configuration file is created as a result of this. Open it in Notepad and uncomment disable= and add the message IDs you want to disable under [MESSAGES CONTROL], for example:

disable=W0511, C0321

Save the file, and in the parameters box in Eclipse Window Preferences PyDev *pylint, type:


It should now work…

You can also include a note at the top of your code that Pylint will read:

# pylint: disable=C0321

Pylint message codes.

—disable-ids=C0321 in the arguments box, for example, does not function.

The dictionary _messages, a property of an instance of the pylint.utils.MessagesHandlerMixIn class, stores all accessible Pylint messages. This dictionary is initially empty when Pylint is launched with the argument —disable-ids=… (at least without a configuration file), resulting in a KeyError exception in Pylint (pylint.utils.MessagesHandlerMixIn.check message id) ().

In Eclipse, you can see this error-message in the Pylint Console (windows* → show view → Console, select Pylint console from the console options besides the console icon.)

Answered by Remi

Solution #3

Instead of remembering all those code numbers, you can use symbolic names to disable warnings starting with Pylint v. 0.25.3. E.g.:

# pylint: disable=locally-disabled, multiple-statements, fixme, line-too-long

This style is more informative than cryptic error codes, and it’s also more practical, because recent Pylint versions just print the symbolic name, not the problem code.

This is where you’ll find the relationship between symbolic names and codes.

A disable comment can be placed on its own line, causing everything after it in the same block to be disabled. It can also be added at the end of the line to which it is intended to apply.

If Pylint generates “Locally disabling” messages, use the disable locally-disabled command first, as shown in the example above.

Answered by imolit

Solution #4

To disable a warning in a block locally, add

# pylint: disable=C0321

to that block.

Answered by thakis

Solution #5

Pylint’s warnings and faults can be turned off in a number of ways. Which one to use depends on whether you want the disablement to be applied worldwide or locally, which is an essential design decision.

Multiple Approaches

This goes beyond Chris Morgan’s description of the /.pylintrc file (in your $HOME directory). Pylint will look for rc files, with a priority system that prioritizes “closer” files:

The majority of these files are named pylintrc, with the exception of the file in having a leading dot.

Add lines to your pylintrc file to disable specific pylint messages. Consider the following scenario:


The # pylint syntax is used to disable a single line, whereas the # pragma pylint syntax is used to disable this line and subsequent lines. It’s easy to mix these up, especially when copying and pasting.

Putting Everything Together

I normally use a combination of these methods.

Like everything else in Python, you can act at different levels of indirection. My advice is to think about what belongs at what level so you don’t end up with a too-lenient approach to Pylint.

Answered by Chris Johnson

Post is based on