🐍 Python for Everyone

← Back to interview questions

Chapter 30 Project 70 min build

Lint + Test + Ship

A real, multi-piece CI/CD setup — not a single calculator.py with a one-job workflow bolted on. You'll work with an actual installable Python package (expense_tracker, three modules plus a real pytest suite of 22 tests), find and fix one genuine lint violation in it, and read through a real multi-job, matrix-strategy GitHub Actions workflow that lints and tests that package across two Python versions.

No live GitHub Actions run happens here This course/site has no way to push to GitHub or trigger a real Actions run from this environment. What you're building and verifying instead is everything a live run would actually do, run for real, locally: ruff check . (the exact command the lint job runs), pytest (the exact command the test job runs, once per matrix Python version), and yaml.safe_load() on ci.yml to confirm the workflow file itself is valid, well-formed YAML. If this folder were pushed to a real GitHub repo with Actions enabled, ci.yml would run these exact same commands on GitHub's own runners.
What's in sample_project/ expense_tracker/ — a real installable package: models.py (an Expense class), tracker.py (an ExpenseTracker class with add/total/filter/budget logic), and report.py (builds a human-readable summary — and contains the one intentional lint violation). tests/ — 22 real pytest tests across three files, covering happy paths and edge cases. .github/workflows/ci.yml — the real multi-job workflow this project is built around.

How to run

terminal
cd chapters/chapter-30-cicd-pipelines/project
python3 starter.py

This installs pytest and ruff if you don't already have them, then runs both against sample_project/ as shipped — you'll see ruff catch exactly one violation and pytest pass all 22 tests.

Your job

1 Fix the lint violation

Open sample_project/expense_tracker/report.py. Near the top:

report.py
import sys  # unused import -- intentionally left in for the lint job to catch

sys is never actually used anywhere in the file. Delete that import line, then re-run ruff and confirm it reports zero violations:

terminal
cd sample_project
ruff check .
Output before the fix
F401 [*] `sys` imported but unused
  --> expense_tracker/report.py:8:8
   |
 8 | import sys  # unused import -- intentionally left in for the lint job to catch
   |        ^^^
help: Remove unused import: `sys`

Found 1 error.

Thought process: an unused import is harmless at runtime — the code still works — but it's exactly the kind of small drift a linter exists to catch before it accumulates: dead imports make it harder to tell what a module actually depends on, and can hide a real bug (importing the wrong name and never noticing because nothing ever used it to fail loudly).

Write the prompt you'd give an AI assistant to fix this and confirm nothing else broke:

Show a strong version of this prompt
ruff flagged `import sys` in report.py as unused (rule F401). Confirm `sys` really isn't referenced anywhere else in this file, remove the import line, and tell me the exact `ruff check .` command I should run afterward to confirm the violation is gone.

2 Read through ci.yml

Open sample_project/.github/workflows/ci.yml. It has two jobs:

.github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: "pip"
          cache-dependency-path: requirements-dev.txt
      - run: pip install -r requirements-dev.txt
      - run: ruff check .

  test:
    name: Test (Python ${{ matrix.python-version }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.11", "3.12"]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          cache: "pip"
          cache-dependency-path: requirements-dev.txt
      - run: pip install -r requirements-dev.txt
      - run: pytest -v

Thought process: before trusting any generated workflow YAML, it's worth being able to answer, line by line, "what triggers this, how many jobs run, does anything depend on anything else, and what exact shell command does each step actually run" — a workflow that "looks right" but was never traced through this carefully is exactly how subtle mistakes (see this project's AI-paired page) slip past review.

Write the prompt you'd give an AI assistant to build a workflow like this from scratch:

Show a strong version of this prompt
Write me a GitHub Actions workflow (.github/workflows/ci.yml) for a Python package called expense_tracker, with a tests/ directory using pytest and dev dependencies in requirements-dev.txt. Requirements: a separate lint job that runs `ruff check .` once, and a separate test job that runs `pytest -v` across a matrix of Python 3.11 and 3.12. Both jobs should trigger on push and pull_request targeting main, use actions/checkout@v4 and actions/setup-python@v5 with pinned versions (not @main), and use pip caching via setup-python's built-in cache option so dependency installs are fast on repeat runs.

3 Re-run everything and verify

terminal
cd sample_project
ruff check .
pytest -v
cd ..
python3 solution.py

solution.py runs ruff check . and pytest for real against your edited copy, and re-parses ci.yml with yaml.safe_load() to confirm the workflow itself is still structurally sound: valid YAML, both triggers present, both jobs present, the matrix has 2+ Python versions, and pip caching is configured. All 7 checks should pass once you've fixed the lint violation.

Why this project matters

A workflow YAML file is worthless if the commands inside it don't actually behave the way you think they do — the single most common real-world CI mistake is a workflow that looks correct but was never actually run locally first, so a typo in a script name or a missing dependency only surfaces after a push, wasting a full round trip. This project forces the habit the rest of your CI/CD career depends on: run the exact commands your workflow runs, locally, before you ever trust what GitHub reports back.

More project ideas

No starter/solution files are provided for these on purpose — each one is meant to be built and verified by editing ci.yml (and re-running the underlying commands) yourself.

1 Add a third Python version to the matrix, and think through the tradeoff

Scenario: Your team wants to officially support Python 3.10 in addition to 3.11 and 3.12.

What to do: Add "3.10" to the python-version matrix list in ci.yml. Actually run pytest locally under a Python 3.10 interpreter if you have one available (or reason through the code — does expense_tracker use any syntax newer than 3.10?) to confirm it would actually pass.

Suggested approach: Every extra matrix version is a real tradeoff: more confidence the package works everywhere it claims to, at the direct cost of more total CI runtime and more runner-minutes consumed on every single push. Write down, in your own words, how you'd decide where to draw the line for a real team's project.

2 Only run the expensive test job on pull_request, not every push

Scenario: The lint job is fast and cheap, but imagine the test job were much slower (a large integration suite hitting a real database) — running it on every single push to every branch would be wasteful.

What to do: Change the test job so it only runs on pull requests, while lint still runs on both push and pull_request. (Hint: a job-level on: doesn't exist — the trigger lives at the workflow level, so this actually requires either two separate workflow files, or an if: condition on the job.)

Suggested approach: Look up GitHub Actions' github.event_name context variable and add if: github.event_name == 'pull_request' to the test job, then validate the resulting YAML with yaml.safe_load() the same way solution.py does.

3 Add a status badge to the README showing CI pass/fail

Scenario: Anyone visiting the repo on GitHub should be able to see at a glance whether the latest commit on main is passing CI, without clicking into the Actions tab.

What to do: GitHub generates a badge URL for any workflow: https://github.com/<owner>/<repo>/actions/workflows/ci.yml/badge.svg. Add that as a Markdown image at the top of sample_project/README.md.

Suggested approach: Since this workflow was never actually pushed to a real GitHub repo, the badge URL won't resolve to real data here — write the correct badge Markdown anyway and explain, in a comment, exactly what <owner>/<repo> would need to be replaced with for it to work for real.

4 Add a job that builds a distributable package artifact

Scenario: Beyond lint and test, a real Python package's pipeline usually also builds the actual installable artifact (a wheel) so it can be published or deployed without rebuilding from source again.

What to do: Add a third job, build, that checks out the code, sets up Python, installs the build package, runs python -m build inside sample_project/ to produce a .whl and .tar.gz in dist/, then uses actions/upload-artifact@v4 to save them. Actually run python -m build locally yourself to confirm it produces real files.

Suggested approach: Since pyproject.toml already declares [build-system] and [tool.setuptools.packages.find], python -m build should work as-is once the build package is installed — inspect the resulting dist/*.whl filename and explain what each part of it encodes (package name, version, Python tag).

← Interview Questions Now Pair With an AI →