TL;DR: use # pylint: disable=error-type
inline comments to disable error types or edit the .pylintrc
file generated via pylint --generate-rcfile > .pylintrc
.
If you are using pylint to run checks on the quality of your Python code, you might want to ignore some of the checks the tool is running on your codebase for you.
You can silence errors with inline comments (e.g. if you still want this check to be performed on your overall codebase but not for this particular snippet):
def f():
pass
class NotAuthorized(Exception):
def __init__(self, message=""):
self.message = message
super().__init__(self.message)
Running pylint on the above code gives you the following output:
1:0: C0116: Missing function or method docstring (missing-function-docstring)
1:0: C0103: Function name "f" doesn't conform to snake_case naming style (invalid-name)
4:0: C0115: Missing class docstring (missing-class-docstring)
On the opposite, the following snippets is rated 10/10 by pylint:
def f(): # pylint: disable=invalid-name, missing-function-docstring
pass
class NotAuthorized(Exception): # pylint: disable=missing-class-docstring
def __init__(self, message=""):
self.message = message
super().__init__(self.message)
Your code has been rated at 10.00/10 (previous run: 5.00/10, +5.00)
Note: you can disable multiple pylint errors with one single inline comment, using a comma as separator.
If you want to disable a specific error check for the whole codebase, you can create a .pylintrc
at the root of your code:
zsh> poetry run pylint --generate-rcfile > .pylintrc
Then, navigate to the [MESSAGES CONTROL]
section, editing the following lines with the error types you want to append:
disable=raw-checker-failed,
bad-inline-option,
locally-disabled,
file-ignored,
Notes:
-
It is never a good practice to deactivating the error messages pylint is raising. Always try to work on it. For instance,
too-man-arguments
on a method probably means that you are missing one intermediary method and should refactor it. -
pylint
checks usually come in pair withblack
,mypy
andunitests
. You can group them to one target via aMakefile
:
black:
poetry run black --exclude=<excluded-folder> .
pylint:
poetry run pylint .
mypy:
poetry run mypy
test:
poetry run pytest -vvs tests/
checks: black pylint mypy test
- To keep my code DRY I avoid repeating information both in the code and in function/module docstrings. As explained in The Pragmatic Programmer by David Thomas & Andrew Hunt and Clean Code by Robert Martin, it makes the code less maintainable and enhance the risk of having the docstrings no longer aligned with the code (because not correctly updated). The code should be self-explanatory, well structured and sticking to good naming convention. The docstrings are only there to explain the why and not the how. Thus, why I often decide to silence the
missing-function-docstring
andmissing-module-docstring
since I will be force to add dummy docstrings otherwise.