Mypy stubs for Django
Typesafe Django Framework
This package contains type stubs and a custom 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 this project. The final goal is to be able to get precise types for most common patterns.
pip install django-stubs
See Configuration section to get started.
mypy happy, you will need to add:
[mypy] plugins = mypy_django_plugin.main [mypy.plugins.django-stubs] django_settings_module = "myproject.settings"
Two things happeining here:
- We need to explicitly list our plugin to be loaded by
- Our plugin also requires
djangosettings module (what you put into
DJANGO_SETTINGS_MODULEvariable) to be specified
This fully working typed boilerplate can serve you as an example.
We rely on different
|django-stubs||mypy version||django version||python version|
|0.12.x||old semantic analyzer (<0.711), dmypy support||2.1.x||^3.6|
Is this an official Django project?
No, it is not. We are indendepent from Django at the moment. There's a proposal to merge our project into the Django itself. You show your support by linking the PR.
Is it safe to use this in production?
Yes, it is! This project does not affect your runtime at all.
It only affects
mypy type checking process.
But, it does not make any sense to use this project without
mypy crashes when I run it with this plugin installed
Current implementation uses Django runtime to extract models information, so it will crash, if your installed apps or
models.py is not correct. For this same reason, you cannot use
reveal_type inside global scope of any Python file that will be executed for
In other words, if your
manage.py runserver crashes, mypy will crash too.
You can also run
option to get extra information about the error.
I cannot use QuerySet or Manager with type annotations
You can get a
TypeError: 'type' object is not subscriptable
when you will try to use
This happens because Django classes do not support
__class_getitem__ magic method.
You can use strings instead:
'Manager[MyModel]', this way it will work as a type for
mypy and as a regular
str in runtime.
Currently we are working on providing
__class_getitem__ to the classes where we need them.
How can I use HttpRequest with custom user model?
You can subclass standard request like so:
from django.http import HttpRequest from my_user_app.models import MyUser class MyRequest(HttpRequest): user: MyUser
And then use
MyRequest instead of standard
HttpRequest inside your project.
awesome-python-typing- Awesome list of all typing-related things in Python.
djangorestframework-stubs- Stubs for Django REST Framework.
pytestplugin that we use for testing
mypystubs and plugins.
wemake-django-template- Create new typed Django projects in seconds.
To get help
We have Gitter here: https://gitter.im/mypy-django/Lobby
If you think you have more generic typing issue, please refer to https://github.com/python/mypy and their Gitter.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size django_stubs-1.5.0-py3-none-any.whl (300.9 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size django-stubs-1.5.0.tar.gz (162.4 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for django_stubs-1.5.0-py3-none-any.whl