Pylint 一直报错(R: 73,0:MyLogging: Too many public methods (22/20)
)对于以下代码:
class MyLogging(logging.Logger):
def foo(self):
pass
def bar(self):
pass
起初我以为这是 Pylint 中的一个错误,因为MyLogging
类正好有 22 行代码,但后来我意识到,它包含基类中的所有公共方法logging.Logger
统计数据也增加了 20。
是否可以从 Pylint 统计中排除基类的公共方法?
PS:我知道我可以改变max-public-methods
到一个更高的数字,或者添加一个一次性例外# pylint: disable=R0904
办法是有的,但没有一个是好的。
这是不可配置的:您可以检查 Pylint 中的代码设计分析.MisdesignChecker, 之内def leave_class
:
for method in node.methods():
if not method.name.startswith('_'):
nb_public_methods += 1
上面的代码只是迭代所有不以“_”开头的方法,并将它们算作公共方法。
因此,我看到两种方法可以做你想做的事:
-
fork Pylint 并修改此方法:
for method in node.methods():
if not method.name.startswith('_') and method.parent == node:
nb_public_methods += 1
method.parent
- 定义该函数的类节点;也在你的leave_class
函数有一个参数node
- 这是类节点。
比较它们就可以了解是否是当前班级。
-
在 Pylint 配置中禁用此规则并创建您自己的插件:
MAX_NUMBER_PUBLIC_METHODS = 3
class PublicMethodsChecker(BaseChecker):
__implements__ = (IASTNGChecker,)
name = 'custom-public-methods-checker'
msgs = {
"C1002": ('Too many public methods (%s/%s)',
'Used when class has too many public methods, try to reduce \
this to get a more simple (and so easier to use) class.'),
}
def leave_class(self, node):
"""check number of public methods"""
nb_public_methods = 0
print type(node)
for method in node.methods():
if not method.name.startswith('_') and method.parent == node:
nb_public_methods += 1
if nb_public_methods > MAX_NUMBER_PUBLIC_METHODS:
self.add_message('C1002',
node=node,
args=(nb_public_methods, MAX_NUMBER_PUBLIC_METHODS))
基本上,这个实现是稍微修改的摘录设计分析.MisdesignChecker来自 Pylint 源代码。
有关插件的更多信息,请参阅帮助 pylint 理解它不理解的事情 http://www.logilab.org/blogentry/78354,以及 Pylint 源代码中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)