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
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, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:
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):
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
On a Mac (zsh):
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.pyanditems_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.pyandmigrations/: 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.iniand.gitignore: the packages at exact versions, where pytest finds the tests, and what Git leaves out.
What to do
- Make
pyproject.tomlin the exercise folder, with Ruff'sline-length = 100under[tool.ruff],select = ["E", "F", "I"]under[tool.ruff.lint], and mypy'sdisallow_untyped_defs = trueunder[tool.mypy]. The README shows the whole file. - Run
ruff check .and fix all four problems it reports, initems_data.py,routers/items.pyandrouters/claims.py.ruff check --fix .makes two of the fixes; the other two are yours. Change the code; never leave a rule out ofselector add a# noqacomment. - Run
ruff format ., so every file has the same layout. It reformats two files. - Run
mypy .and fix all three errors it reports, initems_data.pyandrouters/claims.py, never with a# type: ignorecomment. Runpytestafter each fix: the API's tests must stay green. - Make
.github/workflows/ci.yml(the folder.github, the folderworkflowsinside it, then the file): a workflow withname: CIthat runson: [push, pull_request], with one job thatruns-on: ubuntu-latest. Its steps, in this order:uses: actions/checkout@v7;uses: actions/setup-python@v7withpython-version: "3.15"(in quotes) andcache: pip; thenrun: pip install -r requirements.txt,run: ruff check .,run: ruff format --check .,run: mypy .andrun: pytest.
Check your work
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
== Nonebecomesis None. Ruff calls that fix unsafe, so--fixleaves it to you, as Ruff and mypy on an API Project shows.ruff formatnever wraps a comment. Split a long one over two lines yourself, each starting with#.- mypy reads your Pydantic models. A field typed
datearrives as adate, and the data module asks for text:.isoformat()turns one into the other, as the lesson'sadd_itemroute does. cursor.lastrowidcan beNone, as far as mypy knows.add_iteminitems_data.pyalready 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:
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:

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.
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.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:
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 mainPaste 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.