gpt4 book ai didi

python-3.x - 无法加载自定义 pylint 模块

转载 作者:行者123 更新时间:2023-12-04 19:25:26 32 4
gpt4 key购买 nike

我正在关注一个为 Pylint 创建 custom checker/module 的简单教程。代码看起来像这样。
bad_code.py

def func():
print("Bad code")

return
useless_return.py (这是检查器)
import astroid

from pylint import checkers
from pylint import interfaces
from pylint.checkers import utils


class UselessReturnChecker(checkers.BaseChecker):
__implements__ = interfaces.IAstroidChecker

name = 'useless-return'

msgs = {
'R2119': ("Useless return at end of function or method",
'useless-return',
'Emitted when a bare return statement is found at the end of '
'function or method definition'
),
}

@utils.check_messages('useless-return')
def visit_functiondef(self, node):
"""
Checks for presence of return statement at the end of a function
"return" or "return None" are useless because None is the default
return type if they are missing
"""
# if the function has empty body then return
if not node.body:
return

last = node.body[-1]
if isinstance(last, astroid.Return):
# e.g. "return"
if last.value is None:
self.add_message('useless-return', node=node)
# e.g. "return None"
elif isinstance(last.value, astroid.Const) and (last.value.value is None):
self.add_message('useless-return', node=node)


def register(linter):
"""required method to auto register this checker"""
linter.register_checker(UselessReturnChecker(linter))
请注意,这两个文件都在项目根目录中。
然后我执行了以下命令来测试它。 $ pylint --load-plugins=useless_return bad_code.py它抛出错误:
Traceback (most recent call last):
File "/path/to/my/project/venv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 609, in load_plugin_configuration
module = astroid.modutils.load_module_from_name(modname)
File "path/to/my/project/venv/lib/python3.8/site-packages/astroid/modutils.py", line 228, in load_module_from_name
return importlib.import_module(dotted_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'useless_return'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "path/to/my/project/venv/bin/pylint", line 8, in <module>
sys.exit(run_pylint())
File "path/to/my/project/venv/lib/python3.8/site-packages/pylint/__init__.py", line 24, in run_pylint
PylintRun(sys.argv[1:])
File "path/to/my/project/venv/lib/python3.8/site-packages/pylint/lint/run.py", line 383, in __init__
linter.load_plugin_configuration()
File "path/to/my/project/venv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 613, in load_plugin_configuration
self.add_message("bad-plugin-value", args=(modname, e), line=0)
File "path/to/my/project/venv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1519, in add_message
self._add_one_message(
File "path/to/my/project/venv/lib/python3.8/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 "path/to/my/project/venv/lib/python3.8/site-packages/pylint/utils/linterstats.py", line 296, in increase_single_module_message_count
self.by_module[modname][type_name] += increase
KeyError: None
请注意,我没有全局安装 pylint ,仅在我的 venv 中安装。
命令 which pylint 返回 path/to/my/project/venv/bin/pylint
非常感谢您的帮助。

最佳答案

我无法确认这是测试自定义检查器的最佳方式。但我发现的唯一方法是将自定义模块复制到 pylint/checkers并运行命令。仅测试自定义检查器$pylint --load-plugins=useless_return --disable=all --enable=useless-return bad_code.py

关于python-3.x - 无法加载自定义 pylint 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71590677/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com