1

我正在尝试编写简单的 pylint 插件,但任何输入都会出现以下错误。

__init__py

"""
Register all plugin
"""
#!/usr/bin/env python


    from .check_os_walk import check_os_walk
    
    def register(linter):
        """
        entry point for pylint
        :param linter: linter obj
        :return:
        """
        linter.register_checker(CheckOswalk(linter))

check_os_walk.py

#!/usr/bin/env python
import astroid
from pylint.checkers import BaseChecker, utils
from pylint.interfaces import IAstroidChecker


class CheckOswalk(BaseChecker):
    __implements__ = (IAstroidChecker,)

    name = 'no-need-to-call-os-walk'

    msg = {'R100': ('please avoid using os.walk',
                       'os.walk_used',
                       'os.walk use unless it is needed and prefer os.scandir in python3')}

    def visit_name(self, node):
        print(node.parent)
        self.add_message('os.walk_used', node)

dir_traversal.py """ 遍历目录 """

#!/usr/bin/env python
import os


def main():
    """
    print directory
    :return:
    """
    cwd = os.getcwd()
    for root, dirs, files in os.walk(cwd, topdown=False):
        for name in files:
            print(os.path.join(root, name))
        for name in dirs:
            print(os.path.join(root, name))


if __name__ == "__main__":
    main()

命令 venv/bin/pylint --load-plugins=$PWD/' init .py' dir_traversal.py

错误:

venv/lib/python3.6/site-packages/pylint/lint/pylinter.py", line 613, in load_plugin_configuration
    self.add_message("bad-plugin-value", args=(modname, e), line=0)
  File "venv/lib/python3.6/site-packages/pylint/lint/pylinter.py", line 1527, in add_message
    end_col_offset,
  File "venv/lib/python3.6/site-packages/pylint/lint/pylinter.py", line 1456, in _add_one_message
    self.stats.increase_single_module_message_count(self.current_name, msg_cat, 1)
  File "venv/lib/python3.6/site-packages/pylint/utils/linterstats.py", line 296, in increase_single_module_message_count
    self.by_module[modname][type_name] += increase
KeyError: None
4

1 回答 1

0

在您的__init__.py,CheckOswalk中没有定义。它不会崩溃,因为似乎该register函数也从未被调用过。

于 2021-12-07T10:33:26.110 回答