# Add Type Hints to a Flask Codebase

An agent types every function in a Flask app, takes mypy strict from 241 errors to none, and fixes six real bugs mypy found along the way.

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

## What the agent does

The agent ran the four tests on the untouched clone, then ran `mypy --strict` on it with stub packages installed: 241 errors in 19 files. `create_app` in `app/__init__.py` hangs `elasticsearch`, `redis` and `task_queue` on the Flask object, so it declares them on a `Microblog(Flask)` subclass and adds `get_app()`, which narrows the current app with an `isinstance` assert rather than casts.

Strict mode turned up real bugs in `app/models.py`, `app/tasks.py` and `app/api/errors.py`: `check_password` hashed against a `None` password, `to_dict` and the token methods crashed on NULL timestamps, a task divided by a possibly-`None` count, and `handle_exception` passed `e.code`, which can be `None`, as a status. Each now has an explicit check, a `scalar_one()`, or a 500 fallback.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add type hints to every function and method in this Flask project and configure mypy in strict mode (strict = true in pyproject.toml or mypy.ini). Install stub packages where they exist and add a per-module ignore_missing_imports entry only for packages with no types, listing each one. Fix errors with real types, not casts or type: ignore. For attributes added to the Flask app object, declare them properly instead of casting at each use. Fix real bugs mypy finds and list them separately. Run the tests with python -m unittest, start the app with flask run and load a few pages, and open a pull request with mypy's final 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 miguelgrinberg/microblog at a975ef6: 25 files changed, 299 insertions(+), 147 deletions(-), including `pyproject.toml`, a new `requirements-dev.txt` and three local stubs in `stubs/`. `mypy` ends `Success: no issues found in 24 source files`, `python -m unittest` still reports `Ran 4 tests` and `OK`, and under `flask run` the pages and API calls it loaded answered as expected. The run copied the diff to `/workspace/.rush/artifacts/`, but capture keeps no `.diff` file, so it did not come back. No pull request was opened.

For the reviewer: `redis` moves from 5.0.1 to 5.0.2, because 5.0.1 annotates `Redis.from_url` as returning `None`; reject it if no runtime dependency may change. The hand-written stubs for `flask_login`, `flask_httpauth` and `flask_wtf` type `current_user` as `User`, which is wrong for anonymous visitors. Redis, Elasticsearch and the translation API were not exercised.

## Run it on every Linear issue labelled typing

An automation with a `linear` trigger on `linear.issue.labeled` and the label `typing` runs the recipe's saved prompt with the issue as context. An issue that names one blueprint, such as `app/api`, keeps the pull request small enough to read.

## Why run it on Rush?

Adding annotations is the visible part. The useful part here was the six bugs strict mode exposed while the four tests stayed green, such as a user with no password or a `None` `last_seen`, each listed apart from the typing so a reviewer can judge them on their own.

## Frequently asked questions

### Did it use casts or type: ignore to reach zero errors?

No. The repository has no `cast()` and no `# type: ignore` after the change. Two narrow exceptions remain: `app.tasks` sets `disallow_untyped_calls = false` because rq's `Job.save_meta` is unannotated, and some form values are narrowed with `assert`, which `python -O` strips.

### Which packages are ignored rather than typed?

`flask_babel`, `flask_mail`, `flask_moment` and `langdetect`, which ship no types and have no stub package. `types-requests`, `types-Flask-Migrate` and `types-WTForms` are pinned in `requirements-dev.txt`.

## More Flask jobs

- [Find Dead Code in a Flask App](https://getrush.ai/run/find-dead-code/flask/)
- [Fix Failing GitHub Actions Checks in a Flask App](https://getrush.ai/run/fix-failing-ci/flask/)
- [Fix N+1 Queries in a Flask App](https://getrush.ai/run/fix-n-plus-one-queries/flask/)

## The same job on other stacks

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

## 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/)
