0

我想在我的 pylint 中为所有打印语句添加一条警告消息,提醒自己我有它们,以防我打印敏感信息。这可能吗?我只能找到有关禁用 pylint 错误的答案,而不是启用新错误。

4

2 回答 2

1

您可以将 print 添加到您的内置错误中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,

创建像 Code-Apprentice 建议的自定义检查器也应该相对容易,如下所示:


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

然后将其添加到 pylintrc 的 load-plugin 中:

load-plugins=
    your.code.namespace.print_used,
于 2022-02-08T15:10:13.957 回答
0

我建议编写一个自定义检查器。我自己之前从来没有这样做过,但是如何做到这一点的文档在这里:https ://pylint.pycqa.org/en/latest/how_tos/custom_checkers.html

于 2022-02-07T22:36:46.800 回答