Skip to content
PEP-484 stubs for Django
Python
Branch: master
Clone or download
AndreyMZ and sobolevn Fix `django.contrib.admin.options.BaseModelAdmin.has_add_permission` (#…
…205)

* Fix `django.contrib.admin.options.BaseModelAdmin.has_add_permission` (#203).

* Add `django.db.models.base.Model.unique_error_message` (#204).
Latest commit 87d59c7 Oct 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Adds github tempaltes, refs #112 (#155) Sep 6, 2019
django-sources @ f452d42 Fix typecheck tests ci (#165) Sep 16, 2019
django-stubs Fix `django.contrib.admin.options.BaseModelAdmin.has_add_permission` (#… Oct 11, 2019
mypy_django_plugin Fix ForeignKey type for self-reference defined in the abstract model (#… Oct 5, 2019
scripts improve annotations in some places (#202) Oct 7, 2019
test-data Fix ForeignKey type for self-reference defined in the abstract model (#… Oct 5, 2019
.editorconfig Removes .editorconfig, refs #132 Aug 13, 2019
.gitignore enable tests typechecking using django submodule Jul 24, 2019
.gitmodules enable tests typechecking using django submodule Jul 24, 2019
.travis.yml add linting stubs with flake8-pyi and check for unused imports (#186) Sep 24, 2019
LICENSE.txt Change license to MIT Feb 11, 2019
README.md Reorganize code a bit, add current directory to sys.path (#198) Oct 5, 2019
dev-requirements.txt fix compatibility with 0.730 (#189) Sep 28, 2019
flake8-pyi.ini add linting stubs with flake8-pyi and check for unused imports (#186) Sep 24, 2019
mypy.ini remove some asserts that could be validly triggered Jul 25, 2019
pyproject.toml ci: minor cleanup (#161) Sep 14, 2019
pytest.ini rewrite pytest-mypy-plugin extension code to use new hook Jul 24, 2019
reformat.xsh finish moving generated stubs Dec 22, 2018
release.xsh do not release if uncommitted changes Jun 30, 2019
setup.cfg add linting stubs with flake8-pyi and check for unused imports (#186) Sep 24, 2019
setup.py Fix crash if model from same app referenced in RelatedField cannot be… Oct 5, 2019

README.md

mypy logo

pep484 stubs for Django framework

Build Status Checked with mypy Gitter

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.

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

Installation

pip install django-stubs

Mypy compatibility

django-stubs mypy version django version python version
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

Configuration

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

[mypy]
plugins =
    mypy_django_plugin.main

in your mypy.ini or setup.cfg file.

Plugin also requires Django settings module (what you put into DJANGO_SETTINGS_MODULE variable) to be specified.

[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)

Current 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.

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

Notes

Type implementation monkey-patches Django to add __class_getitem__ to the Manager class. If you would 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.

You can’t perform that action at this time.