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.
- [Django]-Getting Django admin url for an object
- [Django]-Get user profile in django
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
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"
],
- [Django]-What's the best way to extend the User model in Django?
- [Django]-Gunicorn autoreload on source change
- [Django]-Determine variable type within django template
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!
- [Django]-How to get username from Django Rest Framework JWT token
- [Django]-Allowing only super user login
- [Django]-Django ignores router when running tests?
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
- [Django]-Is this the right way to do dependency injection in Django?
- [Django]-How to test Django's UpdateView?
- [Django]-Django TemplateDoesNotExist?
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.
- [Django]-OneToOneField() vs ForeignKey() in Django
- [Django]-Multiple Database Config in Django 1.2
- [Django]-Django: Get model from string?
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.
- [Django]-Celery. Decrease number of processes
- [Django]-How can I list urlpatterns (endpoints) on Django?
- [Django]-How to iterate through dictionary in a dictionary in django template?
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).
- [Django]-How do I integrate Ajax with Django applications?
- [Django]-How about having a SingletonModel in Django?
- [Django]-Django order_by query set, ascending and descending
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.
- [Django]-What's the recommended approach to resetting migration history using Django South?
- [Django]-Django manage.py runserver invalid syntax
- [Django]-Django Template Language: Using a for loop with else
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'
- [Django]-Django Rest Framework model serializer with out unique together validation
- [Django]-Django aggregate or annotate
- [Django]-Gunicorn Connection in Use: ('0.0.0.0', 5000)
3👍
The solution proposed in this other question it to simply add get_attr to your Tag class. Ugly, but works.
- [Django]-Django return file over HttpResponse – file is not served correctly
- [Django]-Mixin common fields between serializers in Django Rest Framework
- [Django]-Pagination in Django-Rest-Framework using API-View
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.
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
- [Django]-Select distinct values from a table field
- [Django]-Querying django migrations table
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"])'
- [Django]-How to get an ImageField URL within a template?
- [Django]-<Django object > is not JSON serializable
- [Django]-Django 2.0 – Not a valid view function or pattern name (Customizing Auth views)