{"id":5193,"library":"django-types","title":"django-types","description":"django-types provides essential type stubs for the Django framework, enabling comprehensive static type checking of Django projects using tools like mypy. It works by augmenting existing Django imports with type information. The current version is 0.23.0, and it's actively maintained with releases tied to Django's lifecycle and bug fixes.","status":"active","version":"0.23.0","language":"en","source_language":"en","source_url":"https://github.com/sbdchd/django-types","tags":["typing","django","mypy","stubs","type-checking"],"install":[{"cmd":"pip install django-types mypy django","lang":"bash","label":"Install library, mypy, and Django"}],"dependencies":[{"reason":"Provides type stubs for Django; requires Django itself to be installed for usage.","package":"Django","optional":false},{"reason":"The primary static type checker django-types integrates with.","package":"mypy","optional":false}],"imports":[{"note":"django-types provides type stubs for standard Django imports. You should import directly from `django.*` as usual; mypy will automatically pick up the stub information from django-types.","wrong":"from django_types.db.models import Model","symbol":"Model","correct":"from django.db.models import Model"},{"note":"Similar to models, you import standard Django components, and django-types adds type information.","symbol":"HttpRequest","correct":"from django.http import HttpRequest"}],"quickstart":{"code":"import os\nimport sys\nfrom pathlib import Path\n\n# Simulate a minimal Django project structure for type checking\nproject_dir = Path('./myproject_for_typechecking')\nproject_dir.mkdir(exist_ok=True)\n(project_dir / '__init__.py').touch(exist_ok=True)\n\n# Simulate app creation\napp_dir = project_dir / 'myapp'\napp_dir.mkdir(exist_ok=True)\n(app_dir / '__init__.py').touch(exist_ok=True)\n\n# Create a dummy settings.py\nsettings_content = '''\nINSTALLED_APPS = ['myapp']\nSECRET_KEY = 'insecure-key-for-testing'\nDATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}\n'''\n(project_dir / 'settings.py').write_text(settings_content)\n\n# Create a typed model in myapp/models.py\nmodels_content = '''\nfrom django.db import models\n\nclass MyTypedModel(models.Model):\n    name = models.CharField(max_length=255)\n    value = models.IntegerField(default=0)\n\n    class Meta:\n        app_label = 'myapp'\n\ndef get_model_instance() -> MyTypedModel:\n    # This function is just to demonstrate type hints\n    return MyTypedModel(name='Test', value=10)\n'''\n(app_dir / 'models.py').write_text(models_content)\n\n# Create a mypy configuration file\nmypy_config_content = '''\n[mypy]\nplugins = mypy_django_plugin.main\n\n[mypy.plugins.django_settings]\nmodule = myproject_for_typechecking.settings\n'''\n(Path('.') / 'mypy.ini').write_text(mypy_config_content)\n\nprint(\"Generated project structure and mypy.ini. Now run:\")\nprint(\"  PYTHONPATH=. mypy myproject_for_typechecking\")\n\n# Clean up (optional, for actual runtime) - commented out for user to inspect files\n# import shutil\n# shutil.rmtree(project_dir)\n# (Path('.') / 'mypy.ini').unlink()\n","lang":"python","description":"This quickstart demonstrates how to set up a minimal Django project, define a typed model, and configure `mypy` with `django-types` for static analysis. It generates dummy files and provides the `mypy` command to run. Key steps are: installing `django-types` alongside `mypy` and Django, creating `mypy.ini` with the `mypy_django_plugin`, and specifying the Django settings module."},"warnings":[{"fix":"Always check the `django-types` documentation (or PyPI project page) for supported Django versions. Ensure your `django-types` version matches or falls within the supported range for your `Django` installation. Upgrade both in tandem if necessary.","message":"django-types provides type stubs for specific Django versions. Using a version of `django-types` that is incompatible with your installed `Django` version can lead to incorrect type checking results or errors.","severity":"breaking","affected_versions":"<0.23.0 (and future versions)"},{"fix":"Add `plugins = mypy_django_plugin.main` to the `[mypy]` section of your `mypy.ini` or equivalent configuration file. Additionally, specify your Django settings module using `[mypy.plugins.django_settings]\nmodule = your_project.settings`.","message":"For `django-types` to function correctly, you *must* enable the `mypy_django_plugin` in your `mypy` configuration (e.g., `mypy.ini`, `pyproject.toml`). Without it, `mypy` will not apply the Django-specific type logic.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `mypy.plugins.django_settings.module` is correctly configured in your `mypy.ini` (e.g., `module = your_project.settings`). If running `mypy` from your project root, you might need to add `PYTHONPATH=.` or ensure `your_project` is discoverable on the Python path.","message":"When running `mypy`, it needs to be able to locate your Django settings module. This often requires setting `PYTHONPATH` correctly or explicitly passing the settings module via a `mypy.ini` configuration or command-line argument.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}