# Add Type Hints to a Wagtail Codebase

An agent types every function in a Wagtail site, takes mypy strict with the Django plugin from 191 errors to none, and fixes the crashes it surfaced.

[Run this on Rush](https://cloud.getrush.ai/run/add-type-hints/wagtail/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

bakerydemo had no mypy setup. The agent wrote `pyproject.toml` with `strict = true`, the `mypy_django_plugin` and `django_settings_module = "bakerydemo.settings.test"`, excluded `migrations/`, and ran it on the untouched code: 191 errors in 22 files. It pinned `django-stubs`, `wagtail-stubs` and `django-filter-stubs` in `requirements/development.txt` and added `mypy .` to `make lint-server`.

Before annotating it read `search/views.py`, the template tags, the wagtail hooks and the management commands. The fixes it lists include `Person.get_preview_context` in `base/models.py` crashing in preview with no `BlogPage`, `CaptionedImageBlock.get_preview_value` in `base/blocks.py` crashing with no images, `get_site_root` in `navigation_tags.py` crashing when no `Site` matched, and `create_random_data` saving a StreamField-style list into a rich-text field.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add type hints to every function in this project and configure mypy in strict mode. Install stubs where packages lack them, fix real bugs mypy finds and list them, run the tests, and open a pull request with mypy's output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on wagtail/bakerydemo at 890285b: 25 files changed, 315 insertions(+), 164 deletions(-), in a Python 3.12 virtualenv, which the run says Django 6 and Wagtail 8.1 need. `mypy .` ends `Success: no issues found in 43 source files`; `./manage.py test` under `bakerydemo.settings.test` found 7 tests and ended `OK` before and after; `ruff check .` printed `All checks passed!` and `ruff format --check .` `43 files already formatted`. The run copied the diff, about 1,500 lines, to `/workspace/.rush/artifacts/`, but capture keeps no `.diff` file, so it did not come back. No pull request was opened.

For the reviewer: about 20 `# type: ignore[...]` comments remain, mostly `[override]` on `get_context`, because the Wagtail stubs allow `request` to be `None`, and `[type-arg]` on block classes, where `StructBlock[Any]` fails at runtime. Several fixes replace a crash with a default (empty caption, a `/` redirect, a `None` site root), and a maintainer may prefer another.

## Run it on every Linear issue labelled mypy

An automation with a `linear` trigger on `linear.issue.labeled` and the label `mypy` runs the recipe's saved prompt with the labelled issue as its context. An issue that names one app, `blog` or `recipes`, keeps the change to one reviewable pull request on its own branch.

## Why run it on Rush?

Zero errors in strict mode can hide a lot of suppression. This run says where it suppressed and why, separates two flagged lines that were not bugs (the guarded `CSP_*` settings and taggit's sortable `Tag`) from the seven it did fix, and kept ruff clean.

## Frequently asked questions

### Which packages had no stubs?

The run found none on PyPI for `taggit`, `debug_toolbar`, `elasticsearch` or `aws_requests_auth`, so each has an `ignore_missing_imports` override in `pyproject.toml`. It missed `aws-requests-auth-stubs`, on PyPI since 2022. Installing `django-stubs` and `wagtail-stubs` also pulled in stubs for modelcluster, treebeard, laces, telepath, willow and permissionedforms.

### Does CI run mypy now?

Only through `make lint-server`. The run did not touch the lint CI workflow or `package.json`, and it did not exercise the Elasticsearch, S3, Redis or `reset_demo` paths at runtime.

## More Wagtail jobs

- [Add a New Page Type to a Wagtail Site from a Linear Issue](https://getrush.ai/run/add-workshop-page/wagtail/)
- [Fix N+1 Queries in a Wagtail Site](https://getrush.ai/run/fix-n-plus-one-queries/wagtail/)
- [Fix Wagtail Errors from Sentry Alerts](https://getrush.ai/run/fix-sentry-errors/wagtail/)

## The same job on other stacks

- [Add Type Hints to a Flask Codebase](https://getrush.ai/run/add-type-hints/flask/)

## More jobs like this: Turn a Linear label into a PR

- [Add a Typo Check to Starlight Docs](https://getrush.ai/run/add-typo-check/starlight/)
- [Warn Before Closing or Leaving a Form With Unsaved Changes](https://getrush.ai/run/add-unsaved-changes-warning/react-hook-form/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
