Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding "Too many ancestors" from pylint

Tags:

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

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?

like image 956
jon Avatar asked Apr 03 '14 10:04

jon


4 Answers

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
like image 135
sthenault Avatar answered Oct 01 '22 01:10

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.

like image 26
Hari Mahadevan Avatar answered Oct 01 '22 01:10

Hari Mahadevan


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.

like image 20
jon Avatar answered Oct 01 '22 02:10

jon


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

like image 45
Joehoe Avatar answered Oct 01 '22 01:10

Joehoe