Disable specific Pylint warnings or all warnings in a File

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

  1. Disable specific Pylint warnings
  2. Disabling Pylint warnings in Visual Studio Code
  3. Disable Pylint warnings on a single line
  4. Disable all Pylint warnings in a File
  5. Disable specific Pylint warnings for an entire file
  6. Disable Pylint warnings in a single scope
  7. Disable Pylint warnings in a single block
  8. Disabling specific warnings when using Pylint from the command line
  9. Only enable specific Pylint warnings when using the command line
  10. Disable or enable Pylint warnings by category

# Disable specific Pylint warnings

To disable specific Pylint warnings:

  1. Make sure you have the pylint module installed by running the following command.
shell
pip install pylint --upgrade # Or with pip3 pip3 install pylint --upgrade

make sure pylint is installed

  1. Generate a pylintrc file if you don't already have one.
Only run this command if you don't have a pylintrc file in the root directory of your project. Otherwise, it will override your existing pylintrc file.
shell
pylint --generate-rcfile > pylintrc

generate pylintrc file

The command generates a pylintrc configuration file in the root directory of your project.

  1. Open your pylintrc file and search for the [MESSAGES CONTROL] section.

search for messages control section

You can press Ctrl + F (or Cmd + F on macOS) and search for [MESSAGES CONTROL] if your pylintrc file is large.

Under MESSAGES CONTROL, you will see the disable= and enable= blocks.

The disable= block is used to disable one or more specific warnings based on their identifiers.

You can separate the identifiers by commas to disable multiple warnings.

pylintrc
disable=bare-except, no-member, undefined-variable

The disable block in the example disables 3 warnings.

Two warnings you will often have to disable are:

pylintrc
disable=missing-function-docstring, missing-module-docstring, no-member, undefined-variable

The warnings should be disabled when you don't write docstrings (documentation strings) in your functions and modules.

There is also an enable block under [MESSAGES CONTROL].

The enable block is the opposite of disable and allows you to enable warnings.

The syntax for the enable block is the same.

pylintrc
enable=c-extension-no-member, non-ascii-module-import, non-ascii-name

You can view the identifiers of all of the available warnings in this section in the docs.

# Disabling Pylint warnings in Visual Studio Code

If you use Visual Studio Code as your IDE, you can disable Pylint warnings in your settings.json file.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Set the python.linting.pylintArgs property in your settings.json file to an array of the warnings you want to disable.
settings.json
{ "python.linting.pylintArgs": [ "--disable=undefined-variable", "--disable=missing-module-docstring", "--disable=no-member" ], }

disable pylint warnings in vscode settings json

Each entry in the array has the form of "--disable=YOUR_PYLINT_WARNING".

If you only want to disable the specific Pylint warnings for your current project, use the local .vscode/settings.json file instead.

  1. In the root directory of your project, create a .vscode folder.

  2. Create a settings.json file in the .vscode folder.

  3. Add the following code to your settings.json file.

.vscode/settings.json
{ "python.linting.pylintArgs": [ "--disable=undefined-variable", "--disable=missing-module-docstring", "--disable=no-member" ] }

disable specific pylint warnings only for current vscode project

Note that the settings you've specified in your local .vscode/settings.json file only apply to your current project.

The local settings also override any global configuration.

# Disable Pylint warnings on a single line

If you need to disable a warning or multiple warnings on a single line, use the following comment.

main.py
try: print('hello world') except: # pylint: disable=bare-except print('bobbyhadz.com')

The # pylint: disable=warning-name message has to be on the same one on which the warning appears.

You can specify multiple warnings by separating them with a comma, e.g.:

  • # pylint: disable=first,second

The same approach can be used to ignore all warnings on the following line.

main.py
try: print('hello world') # pylint: disable-next=bare-except except: print('bobbyhadz.com')

Notice that we used disable-next instead of disable.

The code sample ignores the warning that occurs on the next line.

Using the disable-next comment is more convenient because when the comment is written on the same line, you might exceed the maximum number of characters per line of your linter.

# Disable all Pylint warnings in a File

If you need to disable all Pylint warnings in a file, set the # pylint: skip-file comment at the top of the module.

main.py
# pylint: skip-file

For example, Pylint won't report any of the warnings in the following file.

main.py
# pylint: skip-file global abc def example(): try: print('hello world') except: print('bobbyhadz.com')

pylint disable all warnings for file

You can also use the --disable option to disable all warnings in a file when issuing a command.

shell
pylint --disable=W main.py

disable all warnings for file command line

Make sure to replace main.py with the actual name of your file.

The W category in the example stands for warnings.

All warnings in the file can be disabled by simply disabling the W category when issuing the command.

# Disable specific Pylint warnings for an entire file

You can also use a comment to disable specific Pylint warnings for an entire file.

Simply place the comment at the top of the module.

main.py
# pylint: disable=global-variable-not-assigned def example(): global abc global xyz

To disable multiple, specific Pylint warnings for an entire file, separate them by commas.

main.py
# pylint: disable=global-variable-not-assigned,global-at-module-level def example(): global abc global xyz

# Disable Pylint warnings in a single scope

If you need to disable Pylint warnings in a single scope, set the pragma control comment at the top of the scope.

main.py
def example(): # pylint: disable=bare-except try: print('hello world') except: print('bobbyhadz.com') try: print('hello world') except: print('bobbyhadz.com')

If you only need to disable the warning for a couple of lines, use the pylint: enable comment.

main.py
def example(): # pylint: disable=bare-except try: print('hello world') except: print('bobbyhadz.com') # pylint: enable=bare-except try: print('hello world') except: print('bobbyhadz.com')

The warning is enabled after the pylint: enable comment.

# Disable Pylint warnings in a single block

Here is a more complex example that disables specific Pylint warnings in a single block.

main.py
def example(employee): # ๐Ÿ‘‡๏ธ Scope disable comment # pylint: disable=pointless-statement employee.salary if 10 > 0: # ๐Ÿ‘‡๏ธ Block enable comment # pylint: enable=pointless-statement # enables all pointless-statements in the block employee.salary else: # ๐Ÿ‘‡๏ธ Scope disable in effect here employee.salary

Each separate branch of an if statement is its own block.

The first comment disables the pointless-statements warning in the entire scope (function).

The comment in the if block enables the comment for the if block.

The scope disable comment is still in effect in the else block.

block and scope disable warnings

# Disabling specific warnings when using Pylint from the command line

You can also set the --disable option when issuing the pylint command.

shell
pylint --disable=no-member main.py

disable warning from command line

Make sure to replace main.py with the actual name of your file.

You can also specify multiple, comma-separated identifiers to disable multiple warnings.

shell
pylint --disable=no-member,bare-except main.py

disable multiple warnings command line

You can also set the --disable option multiple times when issuing the command.

shell
pylint --disable=no-member --disable=bare-except main.py

set disable option multiple times

# Only enable specific Pylint warnings when using the command line

If you want to disable all warnings and then enable only specific warnings, set the --disable option to all.

shell
pylint --disable=all --enable=no-member,bare-except main.py

The command disables all warnings except for no-member and bare-except.

# Disable or enable Pylint warnings by category

You can also disable or enable checks by a category:

  • C - convention-related checks
  • R - refactoring-related checks
  • W - various warnings
  • E - errors caused by probable bugs
  • F - fatal errors that prevent pylint from checking your code

Here is an example command that only enables the E and F categories.

shell
pylint --disable=all --enable=E,F main.py

only enable specific categories

The same approach can be used to disable specific categories.

main.py
pylint --disable=C,R,W main.py

disable warnings in specific categories

You can view the identifiers of all of the available warnings in this section in the docs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev