Is it possible to ignore one single specific line with Pylint?
Yes, add the comment # pylint: disable=warning-name
to the end of the line you want Pylint to ignore. Replace warning-name
with the Pylint warning ID that you want ignored. Example:
Guide to Pylint messages
Knowing your warning name or code can help you. It comes in two handy formats:
- Symbolic names like
unused-variable
- Message codes like
W0612
For instance, disabling an unused import could look like this:
Or using the message code:
To find the right message name or code, you can check the Pylint's GitHub page or the extensive Pylint wiki.
Taking control of the scope of warning suppression
Using inline exemption offers the ability to control at a granular level. Yet, sometimes you want to re-enable a warning:
From Pylint 2.10, the disable-next
was introduced to ignore the next line only:
Want more control? Visit the Pylint message control documentation for more insights.
IDEs and the art of effortless suppression
Modern integrated development environments (IDEs) like Eclipse or PyCharm have inbuilt linting tools that automatically provide solutions for Pylint errors. Some even add the necessary comment with a click or shortcut. Make sure to check the PEP 8 guidelines on safer code with proper spacing for comments to avoid additional formatting warnings.
When suppression needs a different approach
While inline exemptions can be handy, a larger scope may be needed for project-wide false positives. Alter your .pylintrc configuration file to manage globally disabled warnings globally, keeping your code free from constant Pylint directives. A wider approach helps to maintain code readability.
Remember, refactoring your code based on Pylint's suggestions helps promote healthier, more maintainable code.
Was this article helpful?