I want to add a warning message to my pylint for all print statements reminding myself I have them in case I print sensitive information. Is this possible? I can only find answers about disabling pylint errors, not enabling new ones.
Asked
Active
Viewed 1,547 times
1
-
I know you are asking for pylint, but pylint does not seem to have a [built-in checker/rule for `print` statements](http://pylint-messages.wikidot.com/all-codes). In case you can switch to flake8, there's already a custom [flake8-print](https://pypi.org/project/flake8-print/) plugin that you can use. – Gino Mempin Feb 07 '22 at 23:56
-
Already have too much going on with pylint to make a switch but thanks! – bnykpp Feb 08 '22 at 01:17
2 Answers
2
You can add print to the bad builtin in your pylintrc
:
[DEPRECATED_BUILTINS]
# List of builtins function names that should not be used, separated by a comma
bad-functions=print
load-plugins=
pylint.extensions.bad_builtin,
Creating a custom checker like Code-Apprentice suggested should also be relatively easy, something like that:
from typing import TYPE_CHECKING
from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.checkers.utils import check_messages
from pylint.interfaces import IAstroidChecker
if TYPE_CHECKING:
from pylint.lint import PyLinter
class PrintUsedChecker(BaseChecker):
name = "no_print_allowed"
msgs = {
"W5001": (
"Used builtin function %s",
"print-used",
"a warning message reminding myself I have them in case I "
"print sensitive information",
)
}
@check_messages("print-used")
def visit_call(self, node: nodes.Call) -> None:
if isinstance(node.func, nodes.Name):
if node.func.name == "print":
self.add_message("print-used", node=node)
def register(linter: "PyLinter") -> None:
linter.register_checker(PrintUsedChecker(linter))
Then you add it in load-plugin in the pylintrc:
load-plugins=
your.code.namespace.print_used,

Pierre.Sassoulas
- 3,733
- 3
- 33
- 48
-
Thanks for the detailed explanation! I haven't been able to get it to work, so if you don't mind I have a few questions. What is pylint.extensions.bad_builtin ? Are you creating a plugin called bad_builtin with the custom checker? Why is it a nodes.Call instead of nodes.FunctionDef like in their example? – bnykpp Feb 08 '22 at 21:41
-
``pylint.extensions.bad_builtin`` is included in pylint but not by default, you need to load it explicitly, see https://pylint.pycqa.org/en/latest/technical_reference/extensions.html?highlight=bad_builtin#pylint-extensions-bad-builtin. Regarding the custom checker code, there's a visitor pattern used, depending on what matters to you (function def, function call, while loop...) you can choose to visit one or not. Here we want to do something for call to print so we visit the calls. Check this ``visit_while`` here : https://github.com/PyCQA/pylint/blob/main/pylint/extensions/while_used.py#L27 – Pierre.Sassoulas Feb 09 '22 at 08:23
-
(The comment length is limited.) To make it clearer you don't need the custom checker to solve the problem, I just added it FYI so there's two solutions in the answer. You should use bad_builtin it's simpler :) – Pierre.Sassoulas Feb 09 '22 at 08:26
-
-
1
I suggest writing a custom checker. I've never done this before myself, but the documentation for how to do it is here: https://pylint.pycqa.org/en/latest/how_tos/custom_checkers.html

Gino Mempin
- 25,369
- 29
- 96
- 135

Code-Apprentice
- 81,660
- 23
- 145
- 268