要求 python 导入为模块
Require python imports to be modules
Google python 的风格指南指出应该:
"Use imports for packages and modules only."
https://google.github.io/styleguide/pyguide.html#Imports
是否有工具可以标记违反此建议的行为?
Pylint 不会这样做。例如,以下:
Is there a tool to lint Python based on the Google style guide?
创建 test.py
违反了准则(exists
是函数,不是模块):
"""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 python 的风格指南指出应该: "Use imports for packages and modules only."
https://google.github.io/styleguide/pyguide.html#Imports
是否有工具可以标记违反此建议的行为?
Pylint 不会这样做。例如,以下: Is there a tool to lint Python based on the Google style guide?
创建 test.py
违反了准则(exists
是函数,不是模块):
"""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))