Skip to main content

Mypy stubs for Django

Project description

mypy logo

pep484 stubs for Django framework

Build Status Checked with mypy

This package contains type stubs and mypy plugin to provide more precise static types and type inference for Django framework. Django uses some Python "magic" that makes having precise types for some code patterns problematic. This is why we need to accompany the stubs with mypy plugins. The final goal is to be able to get precise types for most common patterns.

Supports Python 3.6/3.7, and Django 2.1/2.2.

Could be run on earlier versions of Django, but expect some missing imports warnings.


pip install django-stubs

WARNING: All configuration from pre-1.0.0 versions is dropped, use one below.

WARNING: 1.0.0 breaks dmypy, if you need it, stay on the 0.12.x series.

To make mypy aware of the plugin, you need to add

plugins =

in your mypy.ini file.

Plugin requires Django settings module (what you put into DJANGO_SETTINGS_MODULE variable) to be specified inside mypy.ini file.

strict_optional = True

; this one is new
django_settings_module = mysettings

where mysettings is a value of DJANGO_SETTINGS_MODULE (with or without quotes)

Do you have trouble with mypy / the django plugin not finding your settings module? Try adding the root path of your project to your PYTHONPATH environment variable. If you use pipenv you can add the following to an .env file in your project root which pipenv will run automatically before executing any commands.:


New implementation uses Django runtime to extract models information, so it will crash, if your installed apps is not correct. For this same reason, you cannot use reveal_type inside global scope of any Python file that will be executed for django.setup().

In other words, if your runserver crashes, mypy will crash too.


Implementation monkey-patches Django to add __class_getitem__ to the Manager class. If you'd use Python3.7 and do that too in your code, you can make things like

class MyUserManager(models.Manager['MyUser']):
class MyUser(models.Model):
    objects = UserManager()

work, which should make a error messages a bit better.

Otherwise, custom type will be created in mypy, named MyUser__MyUserManager, which will rewrite base manager as models.Manager[User] to make methods like get_queryset() and others return properly typed QuerySet.

To get help

We have Gitter here

If you think you have more generic typing issue, please refer to and their Gitter.

Project details

Download files

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

Files for django-stubs, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size django_stubs-1.0.2-py3-none-any.whl (286.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-stubs-1.0.2.tar.gz (150.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page