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 app for working with BTCPayServer

btcpay-django A Django app for working with BTCPayServer Installation pip install btcpay-django Developers Release To cut a release, run bumpversion,

Crawford 3 Nov 20, 2022
Meta package to combine turbo-django and stimulus-django

Hotwire + Django This repository aims to help you integrate Hotwire with Django 🚀 Inspiration might be taken from @hotwired/hotwire-rails. We are sti

Hotwire for Django 31 Aug 09, 2022
A feature flipper for Django

README Django Waffle is (yet another) feature flipper for Django. You can define the conditions for which a flag should be active, and use it in a num

950 Dec 26, 2022
A Blog Management System Built with django

Blog Management System Backend use: Django Features Enhanced Ui

Vishal Goswami 1 Dec 06, 2021
The magical reactive component framework for Django ✨

Unicorn The magical full-stack framework for Django ✨ Unicorn is a reactive component framework that progressively enhances a normal Django view, make

Adam Hill 1.4k Jan 05, 2023
Improved Django model inheritance with automatic downcasting

Polymorphic Models for Django Django-polymorphic simplifies using inherited models in Django projects. When a query is made at the base model, the inh

1.4k Jan 03, 2023
Automatically upgrade your Django projects.

django-upgrade Automatically upgrade your Django projects. Installation Use pip: python -m pip install django-upgrade Python 3.8 to 3.10 supported. Or

Adam Johnson 525 Dec 29, 2022
Django Serverless Cron - Run cron jobs easily in a serverless environment

Django Serverless Cron - Run cron jobs easily in a serverless environment

Paul Onteri 41 Dec 16, 2022
Django Email Sender

Email-Sender Django Email Sender Installation 1.clone Repository & Install Packages git clone https://github.com/telman03/Email-Sender.git pip install

Telman Gadimov 0 Dec 26, 2021
WeatherApp - Simple Python Weather App

Weather App Please star this repo if you like ⭐ It's motivates me a lot! Stack A

Ruslan Shvetsov 3 Apr 18, 2022
This is a basic Todo Application API using Django Rest Framework

Todo Application This is a basic Todo Application API using Django Rest Framework. Todo Section - User can View his previously added todo items, creat

Atharva Parkhe 1 Aug 09, 2022
This is a simple Todo web application built Django (back-end) and React JS (front-end)

Django REST Todo app This is a simple Todo web application built with Django (back-end) and React JS (front-end). The project enables you to systemati

Maxim Mukhin 5 May 06, 2022
Awesome Django Markdown Editor, supported for Bootstrap & Semantic-UI

martor Martor is a Markdown Editor plugin for Django, supported for Bootstrap & Semantic-UI. Features Live Preview Integrated with Ace Editor Supporte

659 Jan 04, 2023
Sampling profiler for Python programs

py-spy: Sampling profiler for Python programs py-spy is a sampling profiler for Python programs. It lets you visualize what your Python program is spe

Ben Frederickson 9.5k Jan 01, 2023
CRUD with MySQL, Django and Sass.

CRUD with MySQL, Django and Sass. To have the same data in db: insert into crud_employee (first_name, last_name, email, phone, location, university) v

Luis Quiñones Requelme 1 Nov 19, 2021
A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, celery and redis.

Django Channels Websocket Chatbot A Django chatbot that is capable of doing math and searching Chinese poet online. Developed with django, channels, c

Yunbo Shi 8 Oct 28, 2022
Displaying objects on maps in the Django views and administration site.

DjangoAdminGeomap library The free, open-source DjangoAdminGeomap library is designed to display objects on the map in the Django views and admin site

Vitaly Bogomolov 31 Dec 28, 2022
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
A better and faster multiple selection widget with suggestions

django-searchable-select A better and faster multiple selection widget with suggestions for Django This project is looking for maintainers! Please ope

Andrew Dunai 105 Oct 22, 2022
Bootstrap 3 integration with Django.

django-bootstrap3 Bootstrap 3 integration for Django. Goal The goal of this project is to seamlessly blend Django and Bootstrap 3. Want to use Bootstr

Zostera B.V. 2.3k Jan 03, 2023