• Stars
    star
    556
  • Rank 77,268 (Top 2 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created over 10 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pylint plugin for improving code analysis for when using Django

pylint-django

https://coveralls.io/repos/github/PyCQA/pylint-django/badge.svg?branch=master

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 preferred 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.

More Repositories

1

isort

A Python utility / library to sort imports.
Python
6,308
star
2

bandit

Bandit is a tool designed to find common security issues in Python code.
Python
5,900
star
3

pycodestyle

Simple Python style checker in one Python file
Python
4,924
star
4

pylint

It's not just a linter that annoys you!
Python
4,246
star
5

flake8

flake8 is a python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code.
Python
3,068
star
6

pyflakes

A simple program which checks Python source files for errors
Python
1,304
star
7

pydocstyle

docstring style checker
Python
1,105
star
8

flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.
Python
1,034
star
9

autoflake

Removes unused imports and unused variables as reported by pyflakes
Python
844
star
10

redbaron

Bottom-up approach to refactoring in python
Python
683
star
11

mccabe

McCabe complexity checker for Python
Python
602
star
12

docformatter

Formats docstrings to follow PEP 257
Python
511
star
13

pep8-naming

Naming Convention checker for Python
Python
471
star
14

astroid

A common base representation of python source code for pylint and other projects
Python
425
star
15

modernize

Modernizes Python code for eventual Python 3 migration. Built on top of fissix (a fork of lib2to3)
Python
326
star
16

baron

IDE allow you to refactor code, Baron allows you to write refactoring code.
Python
285
star
17

flake8-import-order

Flake8 plugin that checks import order against various Python Style Guides
Python
276
star
18

eradicate

Removes commented-out code from Python files
Python
195
star
19

doc8

Style checker for sphinx (or other) rst documentation.
Python
158
star
20

flake8-docstrings

Integration of pydocstyle and flake8 for combined linting and reporting
Python
144
star
21

flake8-commas

Flake8 extension for enforcing trailing commas in python
Python
128
star
22

flake8-pyi

A plugin for Flake8 that provides specializations for type hinting stub files
Python
70
star
23

pylint-celery

Pylint plugin for analysing code using Celery
Python
34
star
24

meta

Documentation about how the PyCQA organization works
Python
24
star
25

pylint-plugin-utils

Utilities and helpers for writing Pylint plugins
Python
20
star
26

oeuvre

A repository to collect examples of Python code for testing code-quality tools
Python
11
star
27

flake8-polyfill

Project to make writing plugins across major versions of flake8 easier
Python
11
star
28

flake8-json

JSON formatter for Flake8 output
Python
9
star
29

bandit-action

GitHub Action to run Bandit
Dockerfile
8
star
30

infrastructure

Mirror of PyCQA's infrastructure playbooks
6
star
31

mccabe-console-script

Add a console script for the mccabe complexity checker
Python
4
star