Understanding "Too many ancestors" from pylint

Question:

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', [])

When I run pylint:

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

What does this mean and how can I fix it?

Asked By: jon

||

Answers:

From documentation here: https://pylint.readthedocs.io/en/stable/technical_reference/features.html

too-many-ancestors (R0901): Too many ancestors (%s/%s) Used when
class has too many parent classes, try to reduce this to get a simpler
(and so easier to use) class.

Answered By: jon

The problem is that you inherit from a class which has itself (too) many ancestors: RegisterForm. In your case, you can’t do a lot about this, beside stopping using it which is probably not an option. So you may want to disable this message for this class, eg:

class ExtendedRegisterForm(RegisterForm): # pylint: disable=too-many-ancestors
Answered By: sthenault

In addition to the disabling directives in the source code, you can configure this through the –max-parents= commandline option. You can also specify this in the config file (.pylintrc):

[DESIGN]
max-parents=15

As you can see I set it to 15 as many classes in Django (my project), particularly its view classes, will have hierarchies that are deeper than the default 7.

Answered By: Hari Mahadevan

basically Add to json or .pylintrc --max-parents='yourrequirednumber'

Note: --max-parents=7 is default

If using Visual Studio Code (VSC) you can easily override default config with adding
below code to settings.json

Go to –> UserSettings ctrl+, (default shortcut) –> click … (more actions) –> open settings.json –> Add to overwrite Default settings this code:

"python.linting.pydocstyleArgs": ["--max-parents=25"]

NOTE2:
if pylint_django is not installed:
$ pip install pylint-django

Answered By: Joehoe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.