# Exercise: Lost and Found CI

Set up Ruff and mypy for the Lost and Found Desk's FastAPI project in pyproject.toml, fix everything they report without changing what the API answers, and add a GitHub Actions workflow that runs Ruff, mypy and pytest on every push.

Journeyman · Python · GitHub Actions · Software Development · Updated November 19, 2026

By Gregor Dzierzon

The Lost and Found Desk's API works, and its tests pass. But three students write it, each in their own style, a few likely mistakes have crept in, and nothing checks a push before it lands on GitHub. You'll give the project the setup a team would: Ruff and mypy with their settings in `pyproject.toml`, every problem they report fixed, and a GitHub Actions workflow that runs every check on every push. When you push, GitHub shows your first green run.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-fastapi-lost-and-found-ci](https://github.com/ask-codey/exercise-fastapi-lost-and-found-ci), also linked from the **Exercise** box beside the lesson. Clone it into your `python-course` folder. In VS Code's terminal:

```bash
cd ~/python-course
git clone https://github.com/ask-codey/exercise-fastapi-lost-and-found-ci.git
```

Git makes a folder, `exercise-fastapi-lost-and-found-ci`, with the exercise code inside. *Get the Exercise Code with Git* walks through cloning step by step, if you'd like a reminder. Open that folder in VS Code (**File > Open Folder**), open its terminal with **Terminal > New Terminal**, and make the exercise's virtual environment. On Windows (PowerShell):

```powershell
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
```

On a Mac (zsh):

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

Ruff 0.17.5 and mypy 2.4.1 are pinned in `requirements.txt`, so the last command installs them with everything else. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.

The folder holds:

- `routers/items.py`, `routers/claims.py` and `items_data.py`: the routes and the data module, for you to clean up. The comments at the top of the two routers list every request the API answers.
- `main.py`, `database.py` and `migrations/`: the app, the database connection and the two tables. Ready to use.
- `tests/test_api.py`: the API's own 8 tests, which pass already and must still pass at the end.
- `tests/test_quality.py`: the exercise's 11 checks, on your settings, a clean run of Ruff and mypy, and your workflow file. You don't need to change either test file.
- `requirements.txt`, `pytest.ini` and `.gitignore`: the packages at exact versions, where pytest finds the tests, and what Git leaves out.

## What to do

1. **Make `pyproject.toml`** in the exercise folder, with Ruff's `line-length = 100` under `[tool.ruff]`, `select = ["E", "F", "I"]` under `[tool.ruff.lint]`, and mypy's `disallow_untyped_defs = true` under `[tool.mypy]`. The README shows the whole file.
2. **Run `ruff check .` and fix all four problems it reports**, in `items_data.py`, `routers/items.py` and `routers/claims.py`. `ruff check --fix .` makes two of the fixes; the other two are yours. Change the code; never leave a rule out of `select` or add a `# noqa` comment.
3. **Run `ruff format .`**, so every file has the same layout. It reformats two files.
4. **Run `mypy .` and fix all three errors it reports**, in `items_data.py` and `routers/claims.py`, never with a `# type: ignore` comment. Run `pytest` after each fix: the API's tests must stay green.
5. **Make `.github/workflows/ci.yml`** (the folder `.github`, the folder `workflows` inside it, then the file): a workflow with `name: CI` that runs `on: [push, pull_request]`, with one job that `runs-on: ubuntu-latest`. Its steps, in this order: `uses: actions/checkout@v7`; `uses: actions/setup-python@v7` with `python-version: "3.15"` (in quotes) and `cache: pip`; then `run: pip install -r requirements.txt`, `run: ruff check .`, `run: ruff format --check .`, `run: mypy .` and `run: pytest`.

## Check your work

```bash
pytest
```

No server needs to be running. At first the API's 8 tests (`test_api_1` to `test_api_8`) pass and the 11 checks (`test_1` to `test_11`) fail: that's expected, because nothing is set up yet. Each part you finish turns its checks green. A failing check says what's missing, for example *There's no pyproject.toml yet. Make it in the exercise folder.* Checks 4 to 6 run Ruff and mypy themselves and list what they still report, with each file and line, so they take a while: up to a minute or two the first time, while mypy reads FastAPI's and Pydantic's types, and much less after that. Check 7 shows the line of `ci.yml` that YAML couldn't read.

## Hints

- **`== None` becomes `is None`.** Ruff calls that fix unsafe, so `--fix` leaves it to you, as *Ruff and mypy on an API Project* shows.
- **`ruff format` never wraps a comment.** Split a long one over two lines yourself, each starting with `#`.
- **mypy reads your Pydantic models.** A field typed `date` arrives as a `date`, and the data module asks for text: `.isoformat()` turns one into the other, as the lesson's `add_item` route does.
- **`cursor.lastrowid` can be `None`, as far as mypy knows.** `add_item` in `items_data.py` already deals with it; do the same where mypy points.
- **A function with no hints needs them all:** one on every parameter, and the return type after `->`. The functions around it use the same types.
- **Copy the steps from the lesson.** *Tests on Every Push with GitHub Actions* has the whole workflow, line by line. Indent with two spaces, never tabs.

## When you're done

Every test is green, and `ruff check .`, `ruff format --check .` and `mypy .` report nothing. pytest's progress lines:

```text
tests/test_api.py ........                                               [ 42%]
tests/test_quality.py ...........                                        [100%]
```

Its last line says `19 passed`. The last check happens on GitHub. When you push in *Submit your exercise* below, open your repository's **Actions** tab: a run of *CI* starts within seconds, and after a minute or so its job `checks` shows a green check beside every step, from checkout to `pytest`. If a step is red, open it and read its log; it's the same output you'd see on your computer.

Then add the status badge at the top of `README.md`, with your GitHub name and repository in the address, commit and push again:

```text
![CI](https://github.com/your-name/exercise-fastapi-lost-and-found-ci/actions/workflows/ci.yml/badge.svg)
```

Look back at the seven problems you fixed. Which of them could have broken the API one day, and which only made it harder to read?

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. *Hand In Your Work with Git* walks through each step.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-fastapi-lost-and-found-ci`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

   ```bash
   git remote set-url origin https://github.com/your-name/exercise-fastapi-lost-and-found-ci.git
   git add .
   git commit -m "My solution"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-fastapi-lost-and-found-ci`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

This is how a team's Python repository looks from the first day of a job: one set of settings that everyone's tools share, code that reads the same whoever wrote it, and a workflow that checks every push, so a mistake shows up as a red cross in minutes instead of a bug report days later.

## Key terms

- [Ruff](https://www.ask-codey.com/glossary/ruff)
- [mypy](https://www.ask-codey.com/glossary/mypy)
- [pyproject.toml](https://www.ask-codey.com/glossary/pyproject-toml)
- [Continuous Integration](https://www.ask-codey.com/glossary/continuous-integration)
- [Workflow](https://www.ask-codey.com/glossary/workflow)

## Related

- [Ruff and mypy on an API Project](https://www.ask-codey.com/lessons/fastapi-pro-ruff-and-mypy)
- [Tests on Every Push with GitHub Actions](https://www.ask-codey.com/lessons/fastapi-pro-github-actions-ci)
- [Keeping the Data Code in One Place](https://www.ask-codey.com/lessons/fastapi-data-module)

Source: https://www.ask-codey.com/lessons/exercise-fastapi-lost-and-found-ci
