Kacper e05b84e32d Issue 378 (#387)
* proper redirect return type annotations made with Literal

* Mapping instead of Dict type annotation for context in render() with test

* removed Union and Context

* typo

Co-authored-by: Kacper Szmigiel <szmigielkacper@gmai.com>

Add __init__ to OrderedSet (#381)

Issue 382 (#384)

* WIP fix, pushed for testing

* added _ prefix for internal types

Co-authored-by: Kacper Szmigiel <szmigielkacper@gmai.com>

Fix parameter types for assertJSONEqual/NotEqual (#385)

Add get_supported_language_variant (#386)

Issue 309 (#383)

* added tags for user models

* type test for HttpRequest.user

* test for User and AnonymousUser tags

* httrequest test fix

* checking python version fix for readibility

* Rewrite version check for readability

* Annotate is_authenticated/is_anonymous with Literal-type

* Add auth in INSTALLED_APPS in test

* Fix wrong type assertion in test

* Fix misconception of how branch-testing works

* Remove user from WSGIRequest

* Change HttpRequest-transformer to set user-type to include AnonymousUser

* Add check for anonymous_user_info=None to appease mypy

* Isort transformers/request

* Remove trailing whitespace

* Remove unused import

Co-authored-by: Kacper Szmigiel <szmigielkacper@gmai.com>

* fix formatting and unused import

* reformatted again

Co-authored-by: Kacper Szmigiel <szmigielkacper@gmai.com>
2020-06-03 23:46:30 +03:00
2020-06-03 23:46:30 +03:00
2020-06-03 20:29:19 +03:00
2020-03-13 16:45:45 +03:00
2020-06-03 23:46:30 +03:00
2019-08-13 15:06:13 +03:00
2020-02-06 15:01:56 +03:00
2020-06-03 20:29:19 +03:00
2019-02-11 13:56:34 +03:00
2019-09-14 21:49:13 +03:00
2020-02-15 08:28:47 +03:00
2018-12-22 05:18:27 +03:00
2020-05-05 18:38:11 +03:00

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.3.0 0.750 2.2.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 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.

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

In other words, if your manage.py runserver crashes, mypy will crash too. You can also run mypy with --tb 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 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 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.

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.

Description
PEP-484 stubs for Django
Readme 3.8 MiB
Languages
Python 100%