- Notifications
You must be signed in to change notification settings - Fork118
Pylint plugin for improving code analysis for when using Django
License
pylint-dev/pylint-django
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
pylint-django
is aPylint plugin for improving codeanalysis when analysing code using Django. It is also used by theProspector tool.
To install:
pip install pylint-django
WARNING:pylint-django
will not installDjango
by default becausethis causes more trouble than good,see discussion. If you wishto automatically install the latest version ofDjango
then:
pip install pylint-django[with-django]
otherwise sort out your testing environment and pleaseDO NOT report issuesabout missing Django!
Ensurepylint-django
is installed and on your path. In order to access someof the internal Django features to improve pylint inspections, you should alsoprovide a Django settings module appropriate to your project. This can be doneeither with an environment variable:
DJANGO_SETTINGS_MODULE=your.app.settings pylint --load-plugins pylint_django [..other options..] <path_to_your_sources>
Alternatively, this can be passed in as a commandline flag:
pylint --load-plugins pylint_django --django-settings-module=your.app.settings [..other options..] <path_to_your_sources>
If you do not configure Django, default settings will be used but this will not include, forexample, which applications to include in INSTALLED_APPS and so the linting and type inferencewill be less accurate. It is recommended to specify a settings module.
If you haveprospector
installed, thenpylint-django
will already beinstalled as a dependency, and will be activated automatically if Django isdetected:
prospector [..other options..]
- Prevents warnings about Django-generated attributes such as
Model.objects
orViews.request
. - Prevents warnings when using
ForeignKey
attributes ("Instance ofForeignKey has no <x> member"). - Fixes pylint's knowledge of the types of Model and Form field attributes
- Validates
Model.__unicode__
methods. Meta
informational classes on forms and models do not generate errors.- Flags dangerous use of the exclude attribute in ModelForm.Meta.
- Uses Django's internal machinery to try and resolve models referenced asstrings in ForeignKey fields. That relies on
django.setup()
which needsthe appropriate project settings defined!
pylint_django.checkers.migrations
looks for migrations which:
- add new model fields and these fields have a default value. According toDjango docsthis may have performance penalties especially on large tables. The preferred wayis to add a new DB column with
null=True
because it will be created instantlyand then possibly populate the table with the desired default values.Only the last migration from a sub-directory will be examined; - are
migrations.RunPython()
without a reverse callable - these will result innon reversible data migrations;
This plugin is disabled by default! To enable it:
pylint --load-plugins pylint_django --load-plugins pylint_django.checkers.migrations
Please feel free to add your name to theCONTRIBUTORS.rst
file if you want tobe credited when pull requests get merged. You can also add to theCHANGELOG.rst
file if you wish, although we'll also do that when merging.
The structure of the test package follows that from pylint itself.
It is fairly simple: create a module starting withfunc_
followed bya test name, and insert into it some code. The tests will run pylintagainst these modules. If the idea is that no messages now occur, thenthat is fine, just check to see if it works by runningscripts/test.sh
.
Any command line argument passed toscripts/test.sh
will be passed to the internal invocation ofpytest
.For example if you want to debug the tests you can executescripts/test.sh --capture=no
.A specific test case can be run by filtering based on the file name of the test case./scripts/test.sh -k 'func_noerror_views'
.
Ideally, add some pylint error suppression messages to the file to preventspurious warnings, since these are all tiny little modules not designed todo anything so there's no need to be perfect.
It is possible to make tests with expected error output, for example, ifadding a new message or simply accepting that pylint is supposed to warn.Atest_file_name.txt
file contains a list of expected error messages in theformaterror-type:line number:class name or empty:1st line of detailed error text:confidence or empty
.
pylint-django
is available under the GPLv2 license.
About
Pylint plugin for improving code analysis for when using Django
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.