django-compat-lint

Overview

django_compat_lint -- check Django compatibility of your code

Django's API stability policy is nice, but there are still things that change from one version to the next. Figuring out all of those things when it's time to upgrade can be tediious and annoying as you flip back and forth between the release notes and your code, or start grepping for things in your code.

So why not automate it?

django_compat_lint, in the grand tradition of lint tools, is a simple and extensible engine for going through files of code line by line, applying some rules that look for potential problems, and then report those problems. As the name suggests, it is geared toward checking a Django codebase and finding potential issues you'd run into when upgrading to a more recent Django.

How to use it

Put simply:

python django_compat_lint.py [OPTIONS] [FILE1] [FILE2]...

OPTIONS is a set of command-line options. There is one universal command-line option, implemented as -l or --level, specifying the level of messages to report. See below for a definition of the message levels and what they mean.

Beyond that, different options (run -h or --help to see a list) can be specified depending on what code-checking rules you have available.

The output will be a series of messages, on stdout, each specifying its level, the file it came from, the line of code it came from, and the problem or suggestion that was noticed.

Two useful shortcuts are available for specifying files to check:

  • If no files are specified, all .py files in the current working directory are checked.
  • A path to a directory can be specified; all .py files in that directory will be checked.

Recursive checking involving os.walk() is left as an exercise for someone to send a pull request for.

How it works

django_compat_lint uses one or more sets of rules to check your code. A rule is simply a callable; it will be given the line of code to check, the name of the file that line came from, and an object representing the command-line options being used. It should return a 3-tuple of (warnings, errors, info), which are the supported levels of messages. Which levels are actually displayed is controlled by a command-line flag; these levels should be used for:

warning
Something that isn't going to immediately break the code, but may cause problems later. Deprecated APIs, for example, will issue warnings (since the APIs will still be usable for a couple Django versions).
error
Something that is going to immediately break your code if you try to run under a newer Django version. APIs and modules which have been removed are typical examples of this.
info
Something that doesn't and won't break your code, but is an outdated idiom or something which can be accomplished in a better way using more recent Django.

Registering rules

Rules live in the rules/ subdirectory, and a set of rules is simply a Python module which exports a variable named rules. This should be a list of dictionaries, one per rule. Each dictionary should have the following keys. The first five correspond exactly to the same-named arguments to parser.add_option() in Python's optparse module (which implements the parsing of command-line flags):

long_option
The (long) command-line flag for this rule. To avoid conflicts, rules cannot use short flags.
action
What action to take with the flag.
dest
Similarly, where to store the value of the command-line flag.
help
A brief description of the rule and what it checks, for help output.

The remaining keys are:

callback
The callback which implements the rule.
enabled
A callable which is passed the command-line options, and returns a boolean indicating, from those options, whether this rule is enabled.

A simple example

Suppose that a new version of Django introduces a model field type called SuperAwesomeTextField, which is just like TextField but better. So people who are upgrading may want to change from TextField to SuperAwesomeTextField. A simple rule for this might live in a file named superawesomefield.py. First, the callback for the rule:

def check_superawesomefield(line, filename, options):
    info = []
    if filename == 'models.py' and 'TextField' in line:
        info.append('Consider using SuperAwesomeField instead of TextField.')
    return []. [], info

This checks for the filename 'models.py' since a model field change is probably only applicable to models files. And it checks for use of the model TextField, by just seeing if that appears in the line of code. More complex things might use regular expressions or other tricks to check a line.

Since it's only ever going to give an "info"-level message, the "warnings" and "errors" lists are just always empty.

Then, at the bottom of the file, the rule gets registered:

rules = [
    {'option': '-a',
     'long_option': '--superawesomefield',
     'action': 'store_true',
     'dest': 'superawesomefield',
     'help': 'Check for places where SuperAwesomeField could be used.',
     'callback': check_superawesomefield,
     'enabled': lambda options: options.superawesomefield,}
]

And that's it -- the engine will pick up that rule, and enable it whenever the appropriate command-line flag is used.

Owner
James Bennett
James Bennett
A django model and form field for normalised phone numbers using python-phonenumbers

django-phonenumber-field A Django library which interfaces with python-phonenumbers to validate, pretty print and convert phone numbers. python-phonen

Stefan Foulis 1.3k Dec 31, 2022
Django-static-site - A simple content site framework that harnesses the power of Django without the hassle

coltrane A simple content site framework that harnesses the power of Django with

Adam Hill 57 Dec 06, 2022
A visual indicator of what environment/system you're using in django

A visual indicator of what environment/system you're using in django

Mark Walker 4 Nov 26, 2022
django-compat-lint

django_compat_lint -- check Django compatibility of your code Django's API stability policy is nice, but there are still things that change from one v

James Bennett 40 Sep 30, 2021
With Django Hijack, admins can log in and work on behalf of other users without having to know their credentials.

Django Hijack With Django Hijack, admins can log in and work on behalf of other users without having to know their credentials. Docs 3.x docs are avai

1.2k Jan 05, 2023
Django-Audiofield is a simple app that allows Audio files upload, management and conversion to different audio format (mp3, wav & ogg), which also makes it easy to play audio files into your Django application.

Django-Audiofield Description: Django Audio Management Tools Maintainer: Areski Contributors: list of contributors Django-Audiofield is a simple app t

Areski Belaid 167 Nov 10, 2022
A fresh approach to autocomplete implementations, specially for Django. Status: v3 stable, 2.x.x stable, 1.x.x deprecated. Please DO regularely ping us with your link at #yourlabs IRC channel

Features Python 2.7, 3.4, Django 2.0+ support (Django 1.11 (LTS), is supported until django-autocomplete-light-3.2.10), Django (multiple) choice suppo

YourLabs 1.7k Jan 01, 2023
Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project

Django URL Shortener Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project Install this package to your Dja

Rishav Sinha 4 Nov 18, 2021
django app that allows capture application metrics by each user individually

Django User Metrics django app that allows capture application metrics by each user individually, so after you can generate reports with aggregation o

Reiner Marquez 42 Apr 28, 2022
A simple demonstration of integrating a sentiment analysis tool in a django project

sentiment-analysis A simple demonstration of integrating a sentiment analysis tool in a django project (watch the video .mp4) To run this project : pi

2 Oct 16, 2021
Reusable workflow library for Django

django-viewflow Viewflow is a lightweight reusable workflow library that helps to organize people collaboration business logic in django applications.

Viewflow 2.3k Jan 08, 2023
A Django app for managing robots.txt files following the robots exclusion protocol

Django Robots This is a basic Django application to manage robots.txt files following the robots exclusion protocol, complementing the Django Sitemap

Jazzband 406 Dec 26, 2022
RestApi With Django 3.2 And Django Rest Framework

RestApi-With-Django-3.2-And-Django-Rest-Framework Description This repository is a Software of Development with Python. Virtual Using pipenv, virtuale

Daniel Arturo Alejo Alvarez 6 Aug 02, 2022
Template de desarrollo Django

Template de desarrollo Django Python Django Docker Postgres Nginx CI/CD Descripción del proyecto : Proyecto template de directrices para la estandariz

Diego Esteban 1 Feb 25, 2022
Getdp-project - A Django-built web app that generates a personalized banner of events to come

getdp-project https://get-my-dp.herokuapp.com/ A Django-built web app that gener

CODE 4 Aug 01, 2022
Logan is a toolkit for building standalone Django applications

Logan Logan is a toolkit for running standalone Django applications. It provides you with tools to create a CLI runner, manage settings, and the abili

David Cramer 206 Jan 03, 2023
Django Federated Login provides an authentication bridge between Django projects and OpenID-enabled identity providers.

Django Federated Login Django Federated Login provides an authentication bridge between Django projects and OpenID-enabled identity providers. The bri

Bouke Haarsma 18 Dec 29, 2020
Example project demonstrating using Django’s test runner with Coverage.py

Example project demonstrating using Django’s test runner with Coverage.py Set up with: python -m venv --prompt . venv source venv/bin/activate python

Adam Johnson 5 Nov 29, 2021
django-tables2 - An app for creating HTML tables

django-tables2 - An app for creating HTML tables django-tables2 simplifies the task of turning sets of data into HTML tables. It has native support fo

Jan Pieter Waagmeester 1.6k Jan 03, 2023
Django REST Client API

Django REST Client API Client data provider API.

Ulysses Monteiro 1 Nov 08, 2021