下面是一个简单的教程,它为Pylint创建一个自定义检查器/模块。代码看起来是这样的。
bad_code.py
def func():
print("Bad code")
returnuseless_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。
帮助是非常感谢的。
发布于 2022-04-22 14:59:16
我无法确定这是测试自定义检查器的最佳方法。但是,我发现的唯一方法是将自定义模块复制到pylint/checkers中并运行命令。只测试自定义检查器
$pylint --load-plugins=useless_return --disable=all --enable=useless-return bad_code.py
发布于 2022-08-05 13:13:06
编辑:与Pylint维护人员的讨论进行得很好,这似乎是一个非常有响应性的项目。他们的建议肯定是将您的自定义检查器作为模块的一部分来安装,就像使用pip安装的任何其他库一样。期待看到一些变化,这将带来更多的清晰度很快!
我也有类似的问题,我想我已经找出了根本原因。我用pylint打开了一个问题:PyCQA/pylint#7264
当您使用命令行指定模块时,它尝试加载并注册,而不需要,即添加到sys路径的当前目录(即使init-钩子这样做),因此找不到要导入的文件。
解决办法
在有一个修复程序之前,或者在文档中调用它之前,您有几个选项:
--load_plugins使用命令行参数,而是使用与代码相同的目录中的pylintrc文件,该文件至少指定以下两点:
主# Python代码要执行,通常用于sys.path操作,例如# pygtk.require()。init-钩子=“从pylint.config导入find_pylintrc;导入sys;要加载插件的pylint.config#列表(作为逗号分隔的python模块名的值),#通常用于注册其他检查程序。load-plugins=您的自定义检查器https://stackoverflow.com/questions/71590677
复制相似问题