Home > OS >  How to add custom pylint warning?
How to add custom pylint warning?

Time:02-08

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.

CodePudding user response:

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

CodePudding user response:

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

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):

    __implements__ = (IAstroidChecker,)
    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,
  •  Tags:  
  • Related