Skip to main content

A Pylint plugin to help Pylint understand the Django web framework

Project description

pylint-django

https://travis-ci.org/PyCQA/pylint-django.svg?branch=master https://landscape.io/github/landscapeio/pylint-django/master/landscape.png https://coveralls.io/repos/PyCQA/pylint-django/badge.svg https://img.shields.io/pypi/v/pylint-django.svg

About

pylint-django is a Pylint plugin for improving code analysis when analysing code using Django. It is also used by the Prospector tool.

Installation

To install:

pip install pylint-django

WARNING: pylint-django will not install Django by default because this causes more trouble than good, see discussion. If you wish to automatically install the latest version of Django then:

pip install pylint-django[with_django]

otherwise sort out your testing environment and please DO NOT report issues about missing Django!

Usage

Ensure pylint-django is installed and on your path. In order to access some of the internal Django features to improve pylint inspections, you should also provide a Django settings module appropriate to your project. This can be done either 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, for example, which applications to include in INSTALLED_APPS and so the linting and type inference will be less accurate. It is recommended to specify a settings module.

Prospector

If you have prospector installed, then pylint-django will already be installed as a dependency, and will be activated automatically if Django is detected:

prospector [..other options..]

Features

  • Prevents warnings about Django-generated attributes such as Model.objects or Views.request.

  • Prevents warnings when using ForeignKey attributes (“Instance of ForeignKey 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 as strings in ForeignKey fields. That relies on django.setup() which needs the appropriate project settings defined!

Additional plugins

pylint_django.checkers.migrations looks for migrations which:

  • add new model fields and these fields have a default value. According to Django docs this may have performance penalties especially on large tables. The prefered way is to add a new DB column with null=True because it will be created instantly and 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 in non reversible data migrations;

This plugin is disabled by default! To enable it:

pylint --load-plugins pylint_django --load-plugins pylint_django.checkers.migrations

Contributing

Please feel free to add your name to the CONTRIBUTORS.rst file if you want to be credited when pull requests get merged. You can also add to the CHANGELOG.rst file if you wish, although we’ll also do that when merging.

Tests

The structure of the test package follows that from pylint itself.

It is fairly simple: create a module starting with func_ followed by a test name, and insert into it some code. The tests will run pylint against these modules. If the idea is that no messages now occur, then that is fine, just check to see if it works by running scripts/test.sh.

Any command line argument passed to scripts/test.sh will be passed to the internal invocation of pytest. For example if you want to debug the tests you can execute scripts/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 prevent spurious warnings, since these are all tiny little modules not designed to do anything so there’s no need to be perfect.

It is possible to make tests with expected error output, for example, if adding a new message or simply accepting that pylint is supposed to warn. A test_file_name.txt file contains a list of expected error messages in the format error-type:line number:class name or empty:1st line of detailed error text:confidence or empty.

License

pylint-django is available under the GPLv2 license.

Changelog

Version 2.4.0 (06 Jan 2021)

  • Allowed configuration of the Django settings module to be used via a commandline argument #286

  • If Django settings are not specified via a commandline argument or environment variable, an error is issued but defaults are loaded from Django, removing the fatal error behaviour. #277 and #243

  • Fixed tests to work with pylint>2.6

  • Fixed AttributeError: 'Subscript' object has no attribute 'name' error. Closes #284 (@uy-rrodriguez)

  • Pin Faker version to Prevent Asteroid Crash (James Pulec)

  • Remove Python 3.5 Support (EOL since Sept 2020 and Faker requires 3.6 anyway) (James Pulec)

  • Fixed reverse manager update_or_create calls (James Pulec)

Version 2.3.0 (05 Aug 2020)

  • Improvement in error message for missing-backwards-migration-callable (Bryan Mutai)

  • Start testing with Django 3.1 on Python 3.8

  • Better error message when Django is not configured. Closes #277

Version 2.2.0 (22 Jul 2020)

  • Rely on Django to resolve string references in ForeignKey fields. Refs #243 (Alejandro Angulo)

  • Suppress unused-argument for functions in migration modules. Fix #267

  • New checker for hard-coded auth.User. Fix #244

Version 2.1.0 (12 Jul 2020)

  • New checker to warn about missing backwards data migrations (Bryan Mutai)

  • Rename pylint_django.checkers.db_performance to pylint_django.checkers.migrations

  • Add URL to project Changelog for PyPI (Ville Skyttä)

  • Fix failing test suite b/c of missing CSV dialect. Fix #268 (Alejandro Angulo)

Version 2.0.15 (14 Apr 2020)

  • Do not report error for gettext_lazy (Antonin MOREL)

Version 2.0.14 (25 Feb 2020)

  • Add support for Django 3.0 and Python 3.8 (Wayne Lambert)

  • Support ASGI. Fix #258 (Sander Maijers)

Version 2.0.13 (23 Nov 2019), HackBulgaria edition

  • Suppress too-many-ancestors for class-based generic views

  • Add handler400, handler403, handler404 to good_names. Fix #248

Version 2.0.12 (04 Nov 2019)

  • Fix too broad suppression of unused-argument warnings for functions and methods where the first argument is named request. Now issues warnings for the rest of the arguments if they are unused. Fix #249 (Pascal Urban)

  • Pass arguments of scripts/test.sh to test_func/pytest to ease development (Pascal Urban)

  • Document behavior when ForeignKey fields are referenced as strings. Fix #241

Version 2.0.11 (10 July 2019)

  • Use functools.wrap to preserve leave_module info (Mohit Solanki)

Version 2.0.10 (07 July 2019), Novi Sad edition

  • Suppress no-member for ManyToManyField. Fix #192 and #237 (Pierre Chiquet)

  • Fix UnboundLocalError with ForeignKey(to=). Fix #232 (Sardorbek Imomaliev)

Version 2.0.9 (26 April 2019)

  • Fix UnboundLocalError: local variable 'key_cls' referenced before assignment for cases when models is a python package, the to argument is a string that is used in this pattern app.Model and also there is some other bool const like null=True right after to. (Sardorbek Imomaliev)

  • Don’t crash if ForeignKey field doesn’t have keyword arguments Fix #230

Version 2.0.8 (18 April 2019)

  • Support recursive (self) ForeignKey relations. Fix #208 (Daniil Kharkov)

Version 2.0.7 (16 April 2019)

  • Fixed AstroidImportError for DecimalField. Fix #221 (Daniil Kharkov)

  • Add load_configuration() in pylint_django/__init__.py. Fix #222 #222

  • Support ForeignKey relations with to keyword. Fix #223 (Daniil Kharkov)

Version 2.0.6 (27 Feb 2019)

  • Updating dependency version of pylint-plugin-utils as pylint 2.3 release was not compatible #220

  • Improvements to tox.ini: #217 and #216 (@aerostitch)

  • Add support for new load_configuration hook of pylint #214 (@matusvalo)

  • ‘urlpatterns’ no longer reported as an invalid constant name

Version 2.0.5 (17 Dec 2018)

Bumping the version number because there’s been a mix-up between GitHub tags and the versions pushed to PyPI for 2.0.3 and 2.0.4.

Please use 2.0.5 which includes the changes mentioned below!

Version 2.0.4 (do not use)

  • Avoid traceback with concurrent execution. Fix #197

  • Suppress no-member errors for LazyFunction in factories

  • Suppress no-member errors for RelatedManager fields

  • Clean up compatibility code: PR #207

Version 2.0.3 (do not use)

  • Fixing compatability between ranges of astroid (2.0.4 -> 2.1) and pylint (2.1.1 -> 2.2). #201 and #202

Version 2.0.2 (26 Aug 2018)

  • Suppress false-positive no-self-argument in factory.post_generation. Fix #190 (Federico Bond)

Version 2.0.1 (20 Aug 2018)

  • Enable testing with Django 2.1

  • Add test for Model.objects.get_or_create(). Close #156

  • Add test for objects.exclude(). Close #177

  • Fix Instance of ‘Model’ has no ‘id’ member (no-member), fix Class ‘UserCreationForm’ has no ‘declared_fields’ member. Close #184

  • Fix for Instance of ‘ManyToManyField’ has no ‘add’ member. Close #163

  • Add test & fix for unused arguments on class based views

Version 2.0 (25 July 2018)

  • Requires pylint >= 2.0 which doesn’t support Python 2 anymore!

  • Add modelform-uses-unicode check to flag dangerous use of the exclude attribute in ModelForm.Meta (Federico Bond).

Version 0.11.1 (25 May 2018), the DjangoCon Heidelberg edition

  • Enable test case for urlpatterns variable which was previously disabled

  • Disable unused-argument message for the request argument passed to view functions. Fix #155

  • Add transformations for model_utils managers instead of special-casing them. Fix #160

Version 0.11 (18 April 2018), the TestCon Moscow edition

  • New JsonResponseChecker that looks for common anti-patterns with http responses returning JSON. This includes:

    HttpResponse(json.dumps(data))
    
    HttpResponse(data, content_type='application/json')
    
    JsonResponse(data, content_type=...)

Version 0.10.0 (10 April 2018)

  • Remove the compatibility layer for older astroid versions

  • Make flake8 happy. Fix #102

  • Fix: compatibility with Python < 3.6 caused by ModuleNotFoundError not available on older versions of Python (Juan Rial)

  • Show README and CHANGELOG on PyPI. Fix #122

  • Fix explicit unicode check with python_2_unicode_compatible base models (Federico Bond)

  • Suppress not-an-iterable message for ‘objects’. Fix #117

  • Teach pylint_django that objects.all() is subscriptable. Fix #144

  • Suppress invalid-name for wsgi.application. Fix #77

  • Add test for WSGIRequest.context. Closes #78

  • Register transforms for FileField. Fix #60

  • New checker pylint_django.checkers.db_performance. Enables checking of migrations and reports when there’s an AddField operation with a default value which may slow down applying migrations on large tables. This may also lead to production tables being locked while migrations are being applied. Fix #118

  • Suppress no-member for factory.SubFactory objects. Useful when model factories use factory.SubFactory() for foreign key relations.

Version 0.9.4 (12 March 2018)

  • Add an optional dependency on Django

  • Fix the DjangoInstalledChecker so it can actually warn when Django isn’t available

  • Fix #136 by adding automated build and sanity test scripts

Version 0.9.3 (removed from PyPI)

  • Fix #133 and #134 by including package data when building wheel and tar.gz packages for PyPI (Joseph Herlant)

Version 0.9.2 (broken)

  • Fix #129 - Move tests under site-packages/pylint_django (Mr. Senko)

  • Fix #96 - List Django as a dependency (Mr. Senko)

Version 0.9.1 (26 Feb 2018)

  • Fix #123 - Update links after the move to PyCQA (Mr. Senko)

  • Add test for Meta class from django_tables2 (Mr. Senko)

  • Fix flake8 complaints (Peter Bittner)

  • Add missing .txt and .rc test files to MANIFEST.in (Joseph Herlant)

Version 0.9 (25 Jan 2018)

  • Fix #120 - TypeError: ‘NamesConsumer’ object does not support indexing (Simone Basso)

  • Fix #110 and #35 - resolve ForeignKey models specified as strings instead of class names (Mr. Senko)

Version 0.8.0 (20 Jan 2018)

  • This is the last version to support Python 2. Issues a deprecation warning!

  • #109, adding ‘urlpatterns’, ‘register’, ‘app_name’ to good names. Obsoletes #111, fixes #108 (Vinay Pai)

  • Add ‘handler500’ to good names (Mr. Senko)

  • #103: Support factory_boy’s DjangoModelFactory Meta class (Konstantinos Koukopoulos)

  • #100: Fix E1101:Instance of ‘proxy‘ has no ‘format’ member’ when using .format() on a ugettext_lazy translation. Fixes #80 (canarduck)

  • #99: Add tests and transforms for DurationField, fixes #95 (James M. Allen)

  • #92: Add json field to WSGIRequest proxy (sjk4sc)

  • #84: Add support for django.contrib.postgres.fields and UUIDField (Villiers Strauss)

  • Stop testing with older Django versions. Currently testing with Django 1.11.x and 2.0

  • Stop testing on Python 2, no functional changes in the source code though

  • Update tests and require latest version of pylint (>=1.8), fixes #53, #97

  • #81 Fix ‘duplicate-except’ false negative for except blocks which catch the DoesNotExist exception.

Version 0.7.4

  • #88 Fixed builds with Django 1.10 (thanks to federicobond)

  • #91 Fixed race condition when running with pylint parallel execution mode (thanks to jeremycarroll)

  • #64 “Meta is old style class” now suppressed on BaseSerializer too (thanks to unklphil)

  • #70 Updating to handle newer pylint/astroid versions (thanks to iXce)

Version 0.7.2

  • #76 Better handling of mongoengine querysetmanager

  • #73 #72 Make package zip safe to help fix some path problems

  • #68 Suppressed invalid constant warning for “app_name” in urls.py

  • #67 Fix view.args and view.kwargs

  • #66 accessing _meta no longer causes a protected-access warning as this is a public API as of Django 1.8

  • #65 Add support of mongoengine module.

  • #59 Silence old-style-class for widget Meta

Version 0.7.1

  • #52 - Fixed stupid mistake when using versioninfo

Version 0.7

  • #51 - Fixed compatibility with pylint 1.5 / astroid 1.4.1

Version 0.6.1

  • #43 - Foreign key ID access (somefk_id) does not raise an ‘attribute not found’ warning

  • #31 - Support for custom model managers (thanks smirolo)

  • #48 - Added support for django-restframework (thanks mbertolacci)

Version 0.6

  • Pylint 1.4 dropped support for Python 2.6, therefore a constraint is added that pylint-django will only work with Python2.6 if pylint<=1.3 is installed

  • #40 - pylint 1.4 warned about View and Model classes not having enough public methods; this is suppressed

  • #37 - fixed an infinite loop when using astroid 1.3.3+

  • #36 - no longer warning about lack of __unicode__ method on abstract model classes

  • PR #34 - prevent warning about use of super() on ModelManager classes

Version 0.5.5

  • PR #27 - better ForeignKey transforms, which now work when of the form othermodule.ModelClass. This also fixes a problem where an inferred type would be _Yes and pylint would fail

  • PR #28 - better knowledge of ManyToManyField classes

Version 0.5.4

  • Improved resiliance to inference failure when Django types cannot be inferred (which can happen if Django is not on the system path

Version 0.5.3

  • Issue #25 Fixing cases where a module defines get as a method

Version 0.5.2

  • Fixed a problem where type inference could get into an infinite loop

Version 0.5.1

  • Removed usage of a Django object, as importing it caused Django to try to configure itself and thus throw an ImproperlyConfigured exception.

Version 0.5

  • Issue #7 Improved handling of Django model fields

  • Issue #10 No warning about missing unicode if the Django python3/2 compatability tools are used

  • Issue #11 Improved handling of Django form fields

  • Issue #12 Improved handling of Django ImageField and FileField objects

  • Issue #14 Models which do not define unicode but whose parents do now have a new error (W5103) instead of incorrectly warning about no unicode being present.

  • Issue #21 ForeignKey and OneToOneField fields on models are replaced with instance of the type they refer to in the AST, which allows pylint to generate correct warnings about attributes they may or may not have.

Version 0.3

  • Python3 is now supported

  • __unicode__ warning on models does not appear in Python3

Version 0.2

  • Pylint now recognises BaseForm as an ancestor of Form and subclasses

  • Improved Form support

  • Issue #2 - a subclass of a Model or Form also has warnings about a Meta class suppressed.

  • Issue #3 - Form and ModelForm subclasses no longer warn about Meta classes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylint-django-2.4.0.tar.gz (60.1 kB view details)

Uploaded Source

Built Distribution

pylint_django-2.4.0-py3-none-any.whl (80.2 kB view details)

Uploaded Python 3

File details

Details for the file pylint-django-2.4.0.tar.gz.

File metadata

  • Download URL: pylint-django-2.4.0.tar.gz
  • Upload date:
  • Size: 60.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.19.1 setuptools/41.6.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.12

File hashes

Hashes for pylint-django-2.4.0.tar.gz
Algorithm Hash digest
SHA256 9c324bf1734b0bae7693fa92ee6f486f35665a3519e5344bce0aa5f5743bd6f2
MD5 36e78c71361eefc7f1cce52605dda3cb
BLAKE2b-256 cf803bb05ac836eda4903c1eb698f8f5f7158a36cc1dce14174af3486381138b

See more details on using hashes here.

File details

Details for the file pylint_django-2.4.0-py3-none-any.whl.

File metadata

  • Download URL: pylint_django-2.4.0-py3-none-any.whl
  • Upload date:
  • Size: 80.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.19.1 setuptools/41.6.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.12

File hashes

Hashes for pylint_django-2.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8dc2901d290b9b913eafc701659136b409cddb9b0f5310c219ecf15bd0a86e7b
MD5 9e1cd6943bd9e0a96e3e83b0f8aea634
BLAKE2b-256 bad67b09ed4950bd5cd2ec7ea648264d856d52b9195717cad650ecb223916cce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page