从pylint中理解"太多的祖先"

jon*_*jon 21 python pylint

example.py:

'''
demo too many ancestors 
'''
from flask_security.forms import RegisterForm
from wtforms.fields import TextField

class ExtendedRegisterForm(RegisterForm):
    '''An extended register form'''
    name = TextField('Name', [])
Run Code Online (Sandbox Code Playgroud)

当我运行pylint时:

$ pylint -r n example.py
************* Module example
R:  7, 0: Too many ancestors (10/7) (too-many-ancestors)
Run Code Online (Sandbox Code Playgroud)

这是什么意思,我该如何解决?

sth*_*ult 30

问题是你从一个拥有许多祖先的类继承:RegisterForm.在你的情况下,你不能做很多事情,除了停止使用它可能不是一个选项.因此,您可能希望为此类禁用此消息,例如:

class ExtendedRegisterForm(RegisterForm): # pylint: disable=too-many-ancestors
Run Code Online (Sandbox Code Playgroud)

  • 然后,如果你运行 Pep 8 'E261 / inline 之前至少有两个空格 ' 哈哈 (2认同)

Har*_*van 12

除了源代码中的禁用指令外,您还可以通过--max-parents = commandline选项对其进行配置.您也可以在配置文件(.pylintrc)中指定它:

[DESIGN]
max-parents=15
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我将它设置为15,因为Django(我的项目)中的许多类,特别是其视图类,将具有比默认7更深的层次结构.


jon*_*jon 8

来自此处的文档:http://docs.pylint.org/features.html

太多的祖先(R0901):太多的祖先(%s /%s)当类有太多的父类时使用,尝试减少它以获得更简单(更容易使用)的类.