All Projects → typeddjango → Django Stubs

typeddjango / Django Stubs

Licence: mit
PEP-484 stubs for Django

Programming Languages

python
139335 projects - #7 most used programming language
python3
1442 projects

Projects that are alternatives of or similar to Django Stubs

Factory boy
A test fixtures replacement for Python
Stars: ✭ 2,712 (+343.86%)
Mutual labels:  hacktoberfest, django
Django Rest Passwordreset
An extension of django rest framework, providing a configurable password reset strategy
Stars: ✭ 238 (-61.05%)
Mutual labels:  hacktoberfest, django
Django Sockpuppet
Build reactive applications with the django tooling you already know and love.
Stars: ✭ 225 (-63.18%)
Mutual labels:  hacktoberfest, django
Django Inline Actions
django-inline-actions adds actions to each row of the ModelAdmin or InlineModelAdmin.
Stars: ✭ 170 (-72.18%)
Mutual labels:  hacktoberfest, django
Cookiecutter Django Vue
Cookiecutter Django Vue is a template for Django-Vue projects.
Stars: ✭ 462 (-24.39%)
Mutual labels:  hacktoberfest, django
Django Behaviors
Easily integrate common behaviors for Django models, e.g. Timestamps, Publishing, Authoring, Editing and more.
Stars: ✭ 201 (-67.1%)
Mutual labels:  hacktoberfest, django
Zds Site
Cœur du projet technique de Zeste de Savoir
Stars: ✭ 236 (-61.37%)
Mutual labels:  hacktoberfest, django
Django Sql Explorer
Easily share data across your company via SQL queries. From Grove Collab.
Stars: ✭ 1,958 (+220.46%)
Mutual labels:  hacktoberfest, django
Openwisp Controller
Network and WiFi controller: provisioning, configuration management and updates, (pull via openwisp-config or push via SSH), x509 PKI management and more. Mainly OpenWRT, but designed to work also on other systems.
Stars: ✭ 377 (-38.3%)
Mutual labels:  hacktoberfest, django
Django X509
Reusable django app implementing x509 PKI certificates management
Stars: ✭ 326 (-46.64%)
Mutual labels:  hacktoberfest, django
Consumerfinance.gov
Django project protecting American consumers
Stars: ✭ 164 (-73.16%)
Mutual labels:  hacktoberfest, django
Site
pythondiscord.com - A Django and Bulma web application.
Stars: ✭ 580 (-5.07%)
Mutual labels:  hacktoberfest, django
Django Loci
Reusable Django app for storing geographic and indoor coordinates. Maintained by the OpenWISP Project.
Stars: ✭ 164 (-73.16%)
Mutual labels:  hacktoberfest, django
Weblate
Web based localization tool with tight version control integration.
Stars: ✭ 2,719 (+345.01%)
Mutual labels:  hacktoberfest, django
Djangocms Installer
Console wizard to bootstrap django CMS projects
Stars: ✭ 148 (-75.78%)
Mutual labels:  hacktoberfest, django
Celery Progress
Drop in, configurable, dependency-free progress bars for your Django/Celery applications.
Stars: ✭ 230 (-62.36%)
Mutual labels:  hacktoberfest, django
Django Anon
Anonymize production data so it can be safely used in not-so-safe environments
Stars: ✭ 136 (-77.74%)
Mutual labels:  hacktoberfest, django
Openwisp Users
Implementation of user management and multi-tenancy for OpenWISP
Stars: ✭ 145 (-76.27%)
Mutual labels:  hacktoberfest, django
Djangocms Blog
django CMS blog application - Support for multilingual posts, placeholders, social network meta tags and configurable apphooks
Stars: ✭ 268 (-56.14%)
Mutual labels:  hacktoberfest, django
Django Dbbackup
Management commands to help backup and restore your project database and media files
Stars: ✭ 471 (-22.91%)
Mutual labels:  hacktoberfest, django
mypy logo

pep484 stubs for Django

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

To make mypy aware of the plugin, you 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.7.0 0.790 2.2.x || 3.x ^3.6
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 can show your support by liking 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], Manager[MyModel] or some other Django-based Generic types.

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

  1. You can go with our django_stubs_ext helper, that patches all the types we use as Generic in django.

Install it:

pip install django-stubs-ext  # as a production dependency

And then place in your top-level settings:

import django_stubs_ext

django_stubs_ext.monkeypatch()
  1. 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.

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.

My QuerySet methods are returning Any rather than my Model

QuerySet.as_manager() is not currently supported.

If you are using MyQuerySet.as_manager(), then your Manager/QuerySet methods will all not be linked to your model.

Example:

from django.db import models

class MyModelQuerySet(models.QuerySet):
  pass

class MyModel(models.Model):
  bar = models.IntegerField()
  objects = MyModelQuerySet.as_manager()

def use_my_model():
  foo = MyModel.objects.get(id=1) # This is `Any` but it should be `MyModel`
  return foo.xyz # No error, but there should be

There is a workaround: use Manager.from_queryset instead.

Example:

from django.db import models

class MyModelQuerySet(models.QuerySet):
  pass

MyModelManager = models.Manager.from_queryset(MyModelQuerySet)

class MyModel(models.Model):
  bar = models.IntegerField()
  objects = MyModelManager()

def use_my_model():
  foo = MyModel.objects.get(id=1)
  return foo.xyz # Gives an error

Related projects

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.

Contributing

This project is open source and community driven. As such we encourage contributions big and small. You can contribute by doing any of the following:

  1. Contribute code (e.g. improve stubs, add plugin capabilities, write tests etc) - to do so please follow the contribution guide.
  2. Assist in code reviews and discussions in issues.
  3. Identify bugs and issues and report these

You can always also reach out in gitter to discuss your contributions!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].