标签:google-style-guide lint python
适用于python的Google样式指南指出,应该:
“仅将导入用于程序包和模块.”
https://google.github.io/styleguide/pyguide.html#Imports
有没有标记违反此建议的工具?
Pylint不会这样做.例如,以下内容:
Is there a tool to lint Python based on the Google style guide?
创建一个test.py违反了准则(存在是一个函数,而不是一个模块):
"""Test file for pylint"""
from os.path import exists
exists('/home')
然后,使用rc文件运行pylint就可以了:
$pylint --rcfile=googlecl-pylint.rc -r n -s n test.py
$echo $?
0
搜索可能的代码:http://pylint-messages.wikidot.com/all-codes,我看不到任何警告它的内容.
我也没有在pep8或pyflakes中看到任何能抓住这一点的东西.
解决方法:
为此,我制作了以下pylint插件:
import astroid
from pylint import checkers, interfaces
from pylint.checkers import utils
class ImportOnlyModulesChecked(checkers.BaseChecker):
__implements__ = interfaces.IAstroidChecker
name = 'import-only-modules'
priority = -1
msgs = {
'W5521': (
"Import \"%s\" from \"%s\" is not a module.",
'import-only-modules',
"Only modules should be imported.",
),
}
@utils.check_messages('import-only-modules')
def visit_importfrom(self, node):
try:
imported_module = node.do_import_module(node.modname)
except astroid.AstroidBuildingException:
# Import errors should be checked elsewhere.
return
if node.level is None:
modname = node.modname
else:
modname = '.' * node.level + node.modname
for (name, alias) in node.names:
# Wildcard imports should be checked elsewhere.
if name == '*':
continue
try:
imported_module.import_module(name, True)
# Good, we could import "name" as a module relative to the "imported_module".
except astroid.AstroidImportError:
self.add_message(
'import-only-modules',
node=node,
args=(name, modname),
)
except astroid.AstroidBuildingException:
# Some other error.
pass
def register(linter):
linter.register_checker(ImportOnlyModulesChecked(linter))
标签:google-style-guide,lint,python 来源: https://codeday.me/bug/20191111/2018025.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。