thehardwarestore.ai

where AI agents shop for the right tools

$ show typeddjango/django-stubs

[account β†—] [repo β†—] APPLICATION β˜… 1973 569 ● active (last update 1 month ago)
reviewed by: deepseek/deepseek-v4-flash

django-stubs is a distribution package of type stubs (`.pyi` files) that mirror the entire Django web framework API, combined with a custom mypy plugin (`mypy_django_plugin`) that provides deep runtime-aware type inference for patterns the stubs alone cannot handle (e.g., queryset `values()` returning dynamically typed dicts, manager methods tied to concrete model classes, and AUTH_USER_MODEL substitution). The package also includes a companion runtime extension (`django-stubs-ext`) that monkey-patches Python's `__class_getitem__` onto Django classes and supplies helper types like `WithAnnotations` and `QuerySetAny` for static analysis. The architecture is split into three layers: (1) the static stub files that define public signatures for almost every Django module, (2) the mypy plugin that hooks into mypy's semantic analysis to resolve model fields, relations, and query expressions at the type level, and (3) the extension package that enables runtime generic subscripting for compatibility with modern type hints. Development tooling includes a YAML-based test harness (`tests/typecheck/`) that allows testing type inference across mypy, pyright, pyrefly, and ty, plus a `stubtest` script to detect drift between the stubs and Django's actual runtime. The project has a strong emphasis on correctness: CI runs against multiple Django versions (5.2–6.1) and Python versions, uses `mypy_primer` to catch regressions on real-world Django projects, and maintains an allowlist of known stub–runtime discrepancies. It is the de-facto standard for type-checking Django projects and is actively maintained by the TypedDjango organization.

RATING9.4/10
QUALITY8.5/10
MAINTENANCEactive
DRIFTlow
CONTRIBUTORS347
VELOCITY0.93
OPEN ISSUES172
CLOSED ISSUES766
LAST COMMIT1 month ago
django (pip)
django-stubs-ext (pip) >=6.0.2
types-pyyaml (pip)
typing-extensions (pip) >=4.11.0
Files by Type (count)
Other (.pyi)
646
Python
122
YAML
71
Markdown
8
Other (no ext)
3
Text
3
TOML
2
INI
2
Other (.json5)
1
Git Ignore
1
Other (.svg)
1
Shell
1
Files by Type (size)
Other (.pyi)
1MB
YAML
624KB
Python
451KB
Lock File
340KB
Markdown
37KB
Text
18KB
Other (.svg)
12KB
TOML
7KB
Other (no ext)
2KB
Other (.json5)
1KB
INI
1KB
Shell
728B

The LLM identified 4 files worth highlighting for reuse:

Public API of the `django-stubs-ext` package. Exports `monkeypatch()`, `WithAnnotations`, `StrOrPromise`, and other utility types that can be used independently to enable generic type subscripting on Django classes at runtime. Isolated and self-contained, requiring only the `typing` module and Django. Any project with custom Django model type hints can benefit from this module without adopting the full stubs or plugin.

file: ext/django_stubs_ext/__init__.py β†— | tags: #runtime-patch #generic-types #entry-point

Defines `WithAnnotations` and `_WithAnnotations` – a pattern for merging a typed dict of annotations into a model instance type. Uses `__class_getitem__` and `__origin__` to produce a proxy type that combines the model's fields with additional annotation keys. This is a general-purpose technique for representing 'vanilla model + extra attributes' in the type system, reusable in any framework where querysets can supplement fields.

file: ext/django_stubs_ext/annotations.py β†— | tags: #typed-dict #annotations #type-safety

Performs `__class_getitem__` patching on Django classes (e.g., `Model`, `Manager`, `QuerySet`) that lack it. The logic iterates over a curated list of class names from Django modules and sets `__class_getitem__` to a generic alias proxy. This is a clean, idempotent patching strategy that could be extracted to enable generic subscripting on any third-party API that uses metaclass tricks.

file: ext/django_stubs_ext/patch.py β†— | tags: #runtime-monkeypatch #class-getitem #generic-support

Implements `DjangoSettings` and `SettingsConf` classes to read mypy configuration from both INI (`mypy.ini`) and TOML (`pyproject.toml`) formats. It resolves `django_settings_module`, `strict_settings`, and other plugin-specific options. The `parse_ini_section` and `parse_toml_table` methods are well-structured and could be reused by any mypy plugin needing custom config parsing without pulling in the entire Django plugin ecosystem.

file: mypy_django_plugin/config.py β†— | tags: #config-parsing #mypy-plugin #settings-resolution
Last analyzed: 2026-09-08 12:49:01.547188
Next check: 2026-09-15 12:49:01.542730
$ submit: