Home

Awesome

mypy-django-example

A demo project for django-mypy

This repository contains an implementation of the django 1.10 tutorial extended with PEP-484 type annotations. Its goal is to be used as an example and demonstration of the mypy-django type stubs project. See the README file at https://github.com/machinalis/mypy-django for more details.

Usage

To type check the project you should install mypy-django and run:

$ mypy --strict-optional -p polls
polls/apps.py:1: error: No library stub file for module 'django.apps'
polls/apps.py:1: note: (Stub files are from https://github.com/python/typeshed)
polls/models.py:3: error: No library stub file for module 'django.db'
polls/tests.py:4: error: No library stub file for module 'django.test'
polls/views.py:1: error: No library stub file for module 'django.db.models.query'
polls/views.py:4: error: No library stub file for module 'django.shortcuts'
$ mypy --strict-optional -p tutorial
tutorial/urls.py:17: error: No library stub file for module 'django.contrib'
tutorial/urls.py:17: note: (Stub files are from https://github.com/python/typeshed)
tutorial/wsgi.py:12: error: No library stub file for module 'django.core.wsgi'

The error messages known shown above are expected (They just mention that mypy-django doesn't cover some of the Django modules).

If you need information about how to run this as a project you should probably follow the django tutorial itself.

Using type annotations

If you take a look at the repo and its commits, you'll see how annotations were added; this can be useful as an example of what to expect when annotating your own code.

Most of the changes consisted mainly in adding types to views, namely a request: HttpRequest argument and a return value -> HttpResponse. See for example 60c1ff4.

Other functions/methods that are now views can usually be annotated in a fairly simple and obvious way, see for example 74335dd, 07a8f6b or b18aa7b

What follows is a list of trickier cases