Mypy stubs for Django
Project description
pep484 stubs for Django framework
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.
Installation
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
[mypy]
plugins =
mypy_django_plugin.main
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.
[mypy]
strict_optional = True
; this one is new
[mypy.plugins.django-stubs]
django_settings_module = mysettings
where mysettings is a value of DJANGO_SETTINGS_MODULE (with or without quotes)
New implementation uses Django runtime to extract models information, so it will crash, if your installed apps 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.
Notes
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']):
pass
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 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-stubs-1.0.0.tar.gz.
File metadata
- Download URL: django-stubs-1.0.0.tar.gz
- Upload date:
- Size: 150.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74e472c2d073aab6396882bf0434e5a837a0db64293456c3b2364e6d87df08cd
|
|
| MD5 |
da215ae82b2822e9946b6b846d76dc29
|
|
| BLAKE2b-256 |
63d1f2a23fe13c3a6ac83353e5b2e4e15224814d53dd351ef218eaf7da71fd95
|
File details
Details for the file django_stubs-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_stubs-1.0.0-py3-none-any.whl
- Upload date:
- Size: 286.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dab9309a61e6f149a1b9142f5310f689dd5dab38ec82d8f5e16e75221815ba7
|
|
| MD5 |
8a5c178aab21419a0671cd9b0057ea81
|
|
| BLAKE2b-256 |
ee3aefb605ab3a872caddf840af2988ab7b370dcc04d0916f7aa8c2975a426a6
|