Skip to main content

Mypy stubs for Django

Project description

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

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 = MyUserManager()

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


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.3.0.tar.gz (157.0 kB view details)

Uploaded Source

Built Distribution

django_stubs-1.3.0-py3-none-any.whl (296.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-stubs-1.3.0.tar.gz
  • Upload date:
  • Size: 157.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.5

File hashes

Hashes for django-stubs-1.3.0.tar.gz
Algorithm Hash digest
SHA256 1251b31ec8845ff5ff80cf42c5f70bf2f021d29b5d9708ce7bad39d27924cb08
MD5 d67108de8d48f21906a67a95d40b3d44
BLAKE2b-256 74ef471bcb33232bf6416ed41e2b663d990c1fc4bd7bdb2f21515827325cd52a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_stubs-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 296.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.5

File hashes

Hashes for django_stubs-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9fd4b63686ca5b6e931a2136e4f2511ac8381fde53f43b5d6d3c147c3ed1b76
MD5 bbd5bc8d70444d1328e4578626bf7492
BLAKE2b-256 e6ce742985ccfac9a80788626774e7c829adec45449fceb9e9856e89fdea17cc

See more details on using hashes here.

Supported by

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