Skip to main content

Mypy stubs for Django

Project description

mypy logo

Typesafe Django Framework

Build Status Checked with mypy Gitter

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.

Installation

pip install django-stubs

See Configuration section to get started.

Configuration

To make mypy happy, you will need to add:

[mypy]
plugins =
    mypy_django_plugin.main

[mypy.plugins.django-stubs]
django_settings_module = "myproject.settings"

in your mypy.ini or setup.cfg file.

Two things happeining here:

  1. We need to explicitly list our plugin to be loaded by mypy
  2. Our plugin also requires django settings module (what you put into DJANGO_SETTINGS_MODULE variable) to be specified

This fully working typed boilerplate can serve you as an example.

Version compatibility

We rely on different django and mypy versions:

django-stubs mypy version django version python version
1.6.0 0.780 2.2.x || 3.x ^3.6
1.5.0 0.770 2.2.x || 3.x ^3.6
1.4.0 0.760 2.2.x || 3.x ^3.6
1.3.0 0.750 2.2.x || 3.x ^3.6
1.2.0 0.730 2.2.x ^3.6
1.1.0 0.720 2.2.x ^3.6
0.12.x old semantic analyzer (<0.711), dmypy support 2.1.x ^3.6

FAQ

Is this an official Django project?

No, it is not. We are independent 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.

mypy crashes when I run it with this plugin installed

The 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 django.setup().

In other words, if your manage.py runserver crashes, mypy will crash too. You can also run mypy with the --tb option to get extra information about errors.

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 QuerySet[MyModel] or Manager[MyModel].

This happens because Django classes do not support __class_getitem__ magic method.

You can use strings instead: 'QuerySet[MyModel]' and '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 create a HttpRequest that's guaranteed to have an authenticated user?

Django's built in HttpRequest has the attribute user that resolves to the type

Union[User, AnonymousUser]

where User is the user model specified by the AUTH_USER_MODEL setting.

If you want a HttpRequest that you can type-annotate with where you know that the user is authenticated you can subclass the normal HttpRequest class like so:

from django.http import HttpRequest
from my_user_app.models import MyUser

class AuthenticatedHttpRequest(HttpRequest):
    user: MyUser

And then use AuthenticatedHttpRequest instead of the standard HttpRequest for when you know that the user is authenticated. For example in views using the @login_required decorator.

Related projects

To get help

We have Gitter here: https://gitter.im/mypy-django/Lobby

If you think you have a more generic typing issue, please refer to https://github.com/python/mypy 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.

Source Distribution

django-stubs-1.7.0.tar.gz (199.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_stubs-1.7.0-py3-none-any.whl (381.6 kB view details)

Uploaded Python 3

File details

Details for the file django-stubs-1.7.0.tar.gz.

File metadata

  • Download URL: django-stubs-1.7.0.tar.gz
  • Upload date:
  • Size: 199.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for django-stubs-1.7.0.tar.gz
Algorithm Hash digest
SHA256 ddd190aca5b9adb4d30760d5c64f67cb3658703f5f42c3bb0c2c71ff4d752c39
MD5 efc5c107db9b0387f213a9c9602fdff5
BLAKE2b-256 4198369f6daf6e96b3a1ce1d15f98c30403323395defbbc7c9e15270e4b476ad

See more details on using hashes here.

File details

Details for the file django_stubs-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: django_stubs-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 381.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for django_stubs-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30a7d99c694acf79c5d93d69a5a8e4b54d2a8c11dd672aa869006789e2189fa6
MD5 0d5d999a617e9459182e39c082789c33
BLAKE2b-256 7991dbee3edc44d8545d4cc0a1be06a3a43cb5239d23115ba8d6ff52676cad03

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page