Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 18: Packaging and quality tools

Objectives: LO-18.1, LO-18.2, LO-18.3
Prerequisites: the Module 18 reading, and Ruff and mypy installed in your course environment, as Setting up your computer says
Time: about 2 hours

In this lab you turn wordtally, a small program that prints the most common words in a text file, into a package: you write its pyproject.toml, install it, and run its command. Then you fix what Ruff and mypy find in two of its modules, and release version 1.0.0 with a changelog, a workflow that runs every check on GitHub, and a Git tag.

The project lives in scratch/lab18/wordtally. You write some of its files in cells that start with %%writefile and a file name, as in Lab 13: running the cell saves the rest of the cell to that file.

Installing the package takes about 15 seconds each time. The first time, pip also downloads the build backend, Hatchling, from PyPI, so you need an internet connection.

Setup

Run the next cell first. It checks that pip, Ruff, mypy, and Git are there, writes the project’s files, and defines these helpers:

  • run(*arguments) runs python -m and the arguments in the project’s folder, as a terminal in that folder would, and shows the output and the exit status. run("ruff", "check") is python -m ruff check.

  • install() installs the project in editable mode, as python -m pip install -e . would, and shows pip’s last line.

  • command(*arguments) runs the wordtally command that the install creates, by its path.

  • load(name) imports one of the package’s modules afresh from src, so the checks can call its functions.

  • edit(name, old, new) changes one piece of a file in the project, and set_version(new) changes the version in pyproject.toml.

  • git(command) runs a Git command in the project, as in Lab 10, and git_output(command) returns what it printed.

1. A package and its command

The setup cell wrote every file of the project except pyproject.toml, in the src layout:

wordtally/
├── .gitignore
├── README.md
├── sample.txt
├── src/
│   └── wordtally/
│       ├── __init__.py
│       ├── __main__.py
│       ├── cli.py
│       └── counting.py
└── tests/
    └── test_counting.py

Read cli.py in the setup cell. Its main function is the command: it reads the file named on the command line, and prints its three most common words, or as many as --top says. It finds its own version with importlib.metadata.version, so the version comes from pyproject.toml.

Exercise L18.1 (LO-18.1, ★★, about 20 minutes)

Write the project’s pyproject.toml in the next cell, above the tables that are already there. It needs:

  • a [build-system] table that builds the package with Hatchling 1.26 or newer, as the reading’s does;

  • a [project] table with the name wordtally, the version 0.1.0, the description “Print the most common words in a text file.”, Python 3.12 or newer, and no dependencies;

  • a [project.scripts] table that makes a command, wordtally, which calls the function main in the module wordtally.cli.

Leave the [tool...] tables at the end as they are: they set up Ruff and mypy for sections 2 and 3. The check reads your file, installs the package, and runs its command.

Hint

The reading’s pyproject.toml for rainkit has the same tables. Leave out readme, license, and license-files, because this project has no LICENSE file.

The package is installed in your course environment now, beside JupyterLab and pytest. Its command, wordtally, is in the same folder as the environment’s python:

python -m wordtally runs the same main through __main__.py, and pytest finds the package because it is installed:

Exercise L18.2 (LO-18.1, ★, about 10 minutes)

Predict, then run the three cells after the check to see.

  1. p1: the first cell changes cli.py so that it prints a heading above the words. Will the wordtally command print the heading without installing the package again? Answer True or False.

  2. p2: the second cell changes the version in pyproject.toml to 0.1.1, and runs wordtally --version without installing again. What does it print? Answer with the line it prints, as a string, without the newline.

  3. p3: the third cell installs the package again, and runs wordtally --version. What does it print?

2. Ruff

The next cell writes a new module, report.py, with three functions about the words in a text. The cell after it runs Ruff on it, with the settings at the end of your pyproject.toml.

Exercise L18.3 (LO-18.2, ★★, about 20 minutes)

Change the code in the next cell until both ruff check and ruff format --check pass on report.py. Then each function must still do what its docstring says.

  • Fix every finding. python -m ruff check --fix can make the safe fixes, but it changes the file, and rerunning this cell would put the old code back, so make them in the cell.

  • Don’t silence any finding with # noqa.

  • read_text should catch only the errors that mean the file can’t be read, and let any other error through, such as the TypeError from passing None as the path.

After each change, run this cell, then the cell under it, which shows Ruff’s report and the formatter’s changes. Read what each finding means before you fix it: one of them is a real bug.

Hint 1

Call longest_words("a bb ccc", 2), then longest_words("dddd e"). The second call should return ["dddd", "e"]. What does seen hold when it starts?

Hint 2

seen doesn’t need to be a parameter at all: count_words returns each word once, as a dictionary key. What does open raise when a file is missing, or when you may not read it?

3. mypy

The next cell writes stats.py, another new module. Ruff finds nothing in it, but its type hints don’t match what its functions do. The cell after it runs python -m mypy src, in strict mode, from the [tool.mypy] table in your pyproject.toml.

Exercise L18.4 (LO-18.2, ★★, about 20 minutes)

Change stats.py until mypy reports no errors in it, without # type: ignore, Any, or cast: make the hints say what the functions do, and the code handle what the hints say. describe should return the sentence The text has no words. for a text with no words. Fixing one error can reveal another, so run mypy after each change.

Hint

Dividing two int values with / gives a float. After word = most_common(text), check word is None before you use word as a string.

4. Releasing a version

Exercise L18.5 (LO-18.3, ★, about 10 minutes)

  1. A package is at version 2.3.1. In next_version, give the version of the next release after each change, if that change were the only one in the release, as a string such as "2.3.2". The last one is a release with two changes.

  2. Python sorts strings character by character, so sorted(["1.10.0", "1.9.0"]) puts 1.10.0 first, because "1" comes before "9". Write version_key(text), which returns a key that sorts versions such as "1.10.0" in the right order: a tuple of the three numbers, as integers.

Exercise L18.6 (LO-18.3, ★★, about 10 minutes)

Suppose wordtally’s first release was 0.1.0, on November 30, 2026, and these five commits were made since then. The next release is 1.0.0, on December 4, 2026.

Add --top, which sets how many words to print
Fix the crash on files that aren't UTF-8
Split the counting code into count_words and top_words
Print each word's count in a column after the words
Add tests for top_words

Add the release to the changelog in the next cell, in the format of the reading’s: a heading with the version and the date, and under it the changes sorted under ### Added, ### Changed, and ### Fixed, one entry each. Leave out what doesn’t change anything for a user, and leave ## [Unreleased] empty, because every change is in the release now.

Exercise L18.7 (LO-18.3, ★★★, about 20 minutes)

Make wordtally’s checks run on GitHub on every push:

  1. In the first cell, write requirements-dev.txt: pytest, Ruff, and mypy, each pinned with == to the version in your course environment, which the setup cell printed.

  2. In the second cell, write the workflow .github/workflows/checks.yml. It runs on every push, on ubuntu-latest, and its steps check out the repository, set up Python 3.12, install the requirements and the package in editable mode, and then run ruff check, ruff format --check, mypy src, and pytest, each in a step of its own.

The check reads both files; GitHub would run them, but this lab doesn’t push anything.

Hint

Start from the reading’s checks.yml. Write the Python version in quotation marks, "3.12": YAML reads an unquoted 3.12 as a number, and an unquoted 3.10 as 3.1.

Run the next cell to run the workflow’s four checks here, as GitHub would. They all pass once you have done Exercises L18.3 and L18.4. Read any failure: each one names the file and the line.

Exercise L18.8 (LO-18.3, ★★, about 10 minutes)

The next cell makes the project a Git repository, with one commit that holds everything so far. Then release wordtally 1.0.0 in the cell after it, in the reading’s order: set the version to 1.0.0 with set_version, install the package again, commit the change with the message Release 1.0.0, and add an annotated tag, v1.0.0, with the message wordtally 1.0.0. Use git(...) for the Git commands, written as you would type them after git in a terminal.

The project has no remote, so there is nothing to push. To start over, run the next cell again.

Summary

  • A pyproject.toml with three tables made the project a package: pip installed it, and the [project.scripts] table gave it a command.

  • The editable install picked up a change to the code at once, but a new version only after another install, because pip records the package’s details when it installs it.

  • Ruff found a shared default list that leaked words from one call into the next, a bare except that would hide every error, and four smaller problems; its formatter settled the layout.

  • mypy found a function that returned None against its hint, and then the crash that the None would cause in describe, before anything ran.

  • Semantic versioning said which number to raise, a key of integers sorted versions correctly, and the changelog told users what changed, without the commits that changed nothing for them.

  • The workflow ran the same four checks as your computer, with the tools pinned, and an annotated tag marked the release commit.