[Django]-Using Pylint with Django

172👍

Do not disable or weaken Pylint functionality by adding ignores or generated-members.
Use an actively developed Pylint plugin that understands Django.
This Pylint plugin for Django works quite well:

pip install pylint-django

and when running pylint add the following flag to the command:

--load-plugins pylint_django

Detailed blog post here.

64👍

I use the following: pylint --generated-members=objects

39👍

If you use Visual Studio Code do this:

pip install pylint-django

And add to VSC config:

"python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
],

31👍

My ~/.pylintrc contains

[TYPECHECK]
generated-members=REQUEST,acl_users,aq_parent,objects,_meta,id

the last two are specifically for Django.

Note that there is a bug in PyLint 0.21.1 which needs patching to make this work.

Edit: After messing around with this a little more, I decided to hack PyLint just a tiny bit to allow me to expand the above into:

[TYPECHECK]
generated-members=REQUEST,acl_users,aq_parent,objects,_meta,id,[a-zA-Z]+_set

I simply added:

    import re
    for pattern in self.config.generated_members:
        if re.match(pattern, node.attrname):
            return

after the fix mentioned in the bug report (i.e., at line 129).

Happy days!

👤simon

19👍

django-lint is a nice tool which wraps pylint with django specific settings : http://chris-lamb.co.uk/projects/django-lint/

github project: https://github.com/lamby/django-lint

16👍

Because of how pylint works (it examines the source itself, without letting Python actually execute it) it’s very hard for pylint to figure out how metaclasses and complex baseclasses actually affect a class and its instances. The ‘pychecker’ tool is a bit better in this regard, because it does actually let Python execute the code; it imports the modules and examines the resulting objects. However, that approach has other problems, because it does actually let Python execute the code 🙂

You could extend pylint to teach it about the magic Django uses, or to make it understand metaclasses or complex baseclasses better, or to just ignore such cases after detecting one or more features it doesn’t quite understand. I don’t think it would be particularly easy. You can also just tell pylint to not warn about these things, through special comments in the source, command-line options or a .pylintrc file.

7👍

I resigned from using pylint/pychecker in favor of using pyflakes with Django code – it just tries to import module and reports any problem it finds, like unused imports or uninitialized local names.

👤zgoda

7👍

This is not a solution, but you can add objects = models.Manager() to your Django models without changing any behavior.

I myself only use pyflakes, primarily due to some dumb defaults in pylint and laziness on my part (not wanting to look up how to change the defaults).

👤AdamKG

5👍

Try running pylint with

pylint --ignored-classes=Tags

If that works, add all the other Django classes – possibly using a script, in say, python 😛

The documentation for --ignore-classes is:

--ignored-classes=<members names>
List of classes names for which member
attributes should not be checked
(useful for classes with attributes
dynamicaly set). [current: %default]

I should add this is not a particular elegant solution in my view, but it should work.

4👍

For neovim & vim8 use w0rp's ale plugin. If you have installed everything correctly including w0rp's ale, pylint & pylint-django. In your vimrc add the following line & have fun developing web apps using django.
Thanks.

let g:ale_python_pylint_options = '--load-plugins pylint_django'
👤Ganesh

3👍

The solution proposed in this other question it to simply add get_attr to your Tag class. Ugly, but works.

👤eric

1👍

So far I have found no real solution to that but work around:

  • In our company we require a pylint
    score > 8. This allows coding
    practices pylint doesn’t understand
    while ensuring that the code isn’t
    too “unusual”. So far we havn’t seen
    any instance where E1101 kept us
    from reaching a score of 8 or
    higher.
  • Our ‘make check’ targets
    filter out “for has no ‘objects’
    member” messages to remove most of
    the distraction caused by pylint not
    understanding Django.
👤max

0👍

For heroku users, you can also use Tal Weiss’s answer to this question using the following syntax to run pylint with the pylint-django plugin (replace timekeeping with your app/package):

# run on the entire timekeeping app/package
heroku local:run pylint --load-plugins pylint_django timekeeping

# run on the module timekeeping/report.py
heroku local:run pylint --load-plugins pylint_django timekeeping/report.py

# With temporary command line disables
heroku local:run pylint --disable=invalid-name,missing-function-docstring --load-plugins pylint_django timekeeping/report.py

Note: I was unable to run without specifying project/package directories.

If you have issues with E5110: Django was not configured., you can also invoke as follows to try to work around that (again, change timekeeping to your app/package):

heroku local:run python manage.py shell -c 'from pylint import lint; lint.Run(args=["--load-plugins", "pylint_django", "timekeeping"])'

# With temporary command line disables, specific module
heroku local:run python manage.py shell -c 'from pylint import lint; lint.Run(args=["--load-plugins", "pylint_django", "--disable=invalid-name,missing-function-docstring", "timekeeping/report.py"])'
👤sage

Leave a comment