Last updated: Apr 10, 2024
Reading timeยท6 min
To disable specific Pylint warnings:
pylint
module installed by running the following
command.pip install pylint --upgrade # Or with pip3 pip3 install pylint --upgrade
pylintrc
file if you don't already have one.pylintrc
file in the root directory of your project. Otherwise, it will override your existing pylintrc
file.pylint --generate-rcfile > pylintrc
The command generates a pylintrc
configuration file in the root directory of
your project.
pylintrc
file and search for the [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.
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:
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.
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.
If you use Visual Studio Code as your IDE, you can disable Pylint warnings in your settings.json file.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.Type user settings json.
Click on Preferences: Open User Settings (JSON)
python.linting.pylintArgs
property in your settings.json
file to
an array of the warnings you want to disable.{ "python.linting.pylintArgs": [ "--disable=undefined-variable", "--disable=missing-module-docstring", "--disable=no-member" ], }
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.
In the root directory of your project, create a .vscode
folder.
Create a settings.json
file in the .vscode
folder.
Add the following code to your settings.json
file.
{ "python.linting.pylintArgs": [ "--disable=undefined-variable", "--disable=missing-module-docstring", "--disable=no-member" ] }
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.
If you need to disable a warning or multiple warnings on a single line, use the following comment.
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.
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.
If you need to disable all Pylint warnings in a file, set the
# pylint: skip-file
comment at the top of the module.
# pylint: skip-file
For example, Pylint won't report any of the warnings in the following file.
# pylint: skip-file global abc def example(): try: print('hello world') except: print('bobbyhadz.com')
You can also use the --disable
option to disable all warnings in a file when
issuing a command.
pylint --disable=W main.py
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.
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.
# 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.
# pylint: disable=global-variable-not-assigned,global-at-module-level def example(): global abc global xyz
If you need to disable Pylint warnings in a single scope, set the pragma control comment at the top of the scope.
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.
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.
Here is a more complex example that disables specific Pylint warnings in a single block.
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.
You can also set the --disable
option when issuing the pylint
command.
pylint --disable=no-member main.py
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.
pylint --disable=no-member,bare-except main.py
You can also set the --disable
option multiple times when issuing the command.
pylint --disable=no-member --disable=bare-except main.py
If you want to disable all warnings and then enable only specific warnings, set
the --disable
option to all
.
pylint --disable=all --enable=no-member,bare-except main.py
The command disables all warnings except for no-member
and bare-except
.
You can also disable or enable checks by a category:
C
- convention-related checksR
- refactoring-related checksW
- various warningsE
- errors caused by probable bugsF
- fatal errors that prevent pylint
from checking your codeHere is an example command that only enables the E
and F
categories.
pylint --disable=all --enable=E,F main.py
The same approach can be used to disable specific categories.
pylint --disable=C,R,W main.py
You can view the identifiers of all of the available warnings in this section in the docs.
You can learn more about the related topics by checking out the following tutorials: