151

I would very much like to integrate pylint into the build process for my python projects, but I have run into one show-stopper: One of the error types that I find extremely useful--:E1101: *%s %r has no %r member*--constantly reports errors when using common django fields, for example:

E1101:125:get_user_tags: Class 'Tag' has no 'objects' member

which is caused by this code:

def get_user_tags(username):
   """
   Gets all the tags that username has used.

   Returns a query set.
   """
   return Tag.objects.filter(  ## This line triggers the error.
       tagownership__users__username__exact=username).distinct()

# Here is the Tag class, models.Model is provided by Django:
class Tag(models.Model):
   """
   Model for user-defined strings that help categorize Events on
   on a per-user basis.
   """
   name = models.CharField(max_length=500, null=False, unique=True)

   def __unicode__(self):
       return self.name

How can I tune Pylint to properly take fields such as objects into account? (I've also looked into the Django source, and I have been unable to find the implementation of objects, so I suspect it is not "just" a class field. On the other hand, I'm fairly new to python, so I may very well have overlooked something.)

Edit: The only way I've found to tell pylint to not warn about these warnings is by blocking all errors of the type (E1101) which is not an acceptable solution, since that is (in my opinion) an extremely useful error. If there is another way, without augmenting the pylint source, please point me to specifics :)

See here for a summary of the problems I've had with pychecker and pyflakes -- they've proven to be far to unstable for general use. (In pychecker's case, the crashes originated in the pychecker code -- not source it was loading/invoking.)

3

13 Answers 13

171

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.

11
  • 4
    The link to the blog post is dead (so soon). Here are some archived links from the Internet Archive and from archive.is Sep 23, 2015 at 16:06
  • 3
    To make it work with Sublime Text's SublimeLinter plugin, I had to add --load-plugins=pylint_django to linters/pylint/args setting. Note the '=' sign, it didn't work without it. Nov 19, 2015 at 13:32
  • it does not work. I get this error: E: 8, 0: No name 'models' in module 'django.db' (no-name-in-module)
    – max
    Dec 7, 2015 at 17:02
  • 11
    You can also add this in your pylintrc: [MASTER] load-plugins=pylint_django
    – azmeuk
    Jun 14, 2016 at 15:13
  • 3
    In vs code it dose not work for me until I Put in the following in the user settings: {"python.linting.pylintArgs": [ "--load-plugins=pylint_django" ],} tieuminh2510's answer Jul 14, 2019 at 6:16
64

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

4
  • 1
    man pylint(1) under TYPECHECK --generated-members=<members names> List of members which are set dynamically and missed by pylint inference system, and so shouldn't trigger E0201 and E1101 when accessed. [current: REQUEST,acl_users,aq_parent] May 2, 2012 at 6:14
  • I add this in PyDev in eclipse under preferences in the PyDev/PyLint section. May 2, 2012 at 6:21
  • 3
    Using generated-members just hides these errors from you, there still can be errors when trying to access objects field on the wrong object. Use pylint-django plugin instead. Feb 12, 2015 at 13:16
  • 6
    This is the wrong way to fix Pylint - by disabling some of its functionality. All you need to do is install a Pylint plugin that understands Django. See stackoverflow.com/a/31000713/78234
    – Tal Weiss
    Jun 23, 2015 at 10:50
38

If you use Visual Studio Code do this:

pip install pylint-django

And add to VSC config:

"python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
],
1
  • 4
    Best response by far :D
    – serfer2
    Sep 10, 2019 at 12:19
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!

5
  • You should submit your patch to pylint back to the maintainers.
    – slacy
    Jun 17, 2011 at 18:51
  • actually they've included this patch in 0.24, but they've started using the shlex package, and broken something else now. I had to add gen.wordchars += "[]-+" at line 135 to get it to work...
    – simon
    Oct 1, 2011 at 21:01
  • 4
    Using generated-members just hides these errors from you, there still can be errors when trying to access 'objects' field on the wrong object. Use pylint-django plugin instead. Feb 12, 2015 at 13:17
  • 4
    This is the wrong way to fix Pylint - by disabling some of its functionality. All you need to do is install a Pylint plugin that understands Django. See stackoverflow.com/a/31000713/78234
    – Tal Weiss
    Jun 23, 2015 at 10:50
  • 3
    @TalWeiss -- in fairness, this answer is three years older than pylint-django, so the downvote is a bit harsh...
    – simon
    Jun 24, 2015 at 20:08
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

7
  • 1
    I like the idea of a Django-specific pylint, but it seems a big buggy last time I tried it.
    – Wernight
    Jul 20, 2010 at 22:23
  • 3
    Also it's not available via PyPI and the website doesn't seem to give enough info like: What's the current version?
    – Wernight
    Jul 20, 2010 at 22:32
  • 1
    I like the concept, but this implementation is only half-baked, and breaks on any moderately sized codebase. It has a long way to go before it's actual useful.
    – Cerin
    Aug 17, 2011 at 14:59
  • 1
    @gurney alex, Link is dead.
    – shuttle87
    Jan 19, 2015 at 1:33
  • 2
    Looks like pylint-django is more active now, that should be the advised solution. Feb 12, 2015 at 13:18
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.

2
  • 3
    It is not easy to teach Pylint about Django, but it has been done: All you need to do is install a Pylint plugin that understands Django. See stackoverflow.com/a/31000713/78234
    – Tal Weiss
    Jun 23, 2015 at 11:18
  • Well I installed it but it still compains about thins like QuerySet has no remove... Jun 18, 2018 at 14:26
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.

3
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).

5
  • Ah... thanks for the tip. I may try just adding that to Model.models in the local copy of the django source, and see if that does it.
    – rcreswick
    Sep 23, 2008 at 17:19
  • I think this a great solution because it doesn't compromise on warnings.
    – Tom Leys
    Mar 22, 2009 at 0:20
  • 1
    This is a bad solution. Repeating yourself and replacing something that it is feasible will change later (thus introducing a QA problem), just to fix an incomplete QA tool? Jun 11, 2012 at 13:20
  • 2
    I wouldn't call this a bad solution: explicit is better than implicit. Perhaps objects shouldn't be magically added anyway.
    – Will Hardy
    Apr 30, 2013 at 13:18
  • 1
    I think that this is the wrong way to fix Pylint - by patching Django in a sense. All you need to do is install a Pylint plugin that understands Django. See stackoverflow.com/a/31000713/78234
    – Tal Weiss
    Jun 23, 2015 at 11:20
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 :P

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.

2
  • It only works if I never make any errors in those classes ;). I want to avoid ignoring code if at all possible -- I think it is a very bad idea to have different parts of the codebase analyzed an different degrees of scrutiny. I will forget which is which, and make false assumptions when debugging
    – rcreswick
    Sep 22, 2008 at 16:55
  • 1
    This is the wrong way to fix Pylint - by disabling some of its functionality. All you need to do is install a Pylint plugin that understands Django. See stackoverflow.com/a/31000713/78234
    – Tal Weiss
    Jun 23, 2015 at 11:22
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'
1
  • 1
    did work perfect for me, on bought vim and neovim, thank you. Aug 19, 2020 at 8:59
3

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

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.
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"])'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.