|
| 1 | +# Static Type Checking with mypy |
| 2 | + |
| 3 | +Python does not require type annotations to run a program. |
| 4 | + |
| 5 | +This project still uses annotations because they document what values functions |
| 6 | +accept and return: |
| 7 | + |
| 8 | +```python |
| 9 | +def count_words(text: str) -> int: |
| 10 | + ... |
| 11 | +``` |
| 12 | + |
| 13 | +The annotation says that `text` should be a string and the result should be an |
| 14 | +integer. |
| 15 | + |
| 16 | +## Type hints do not check themselves |
| 17 | + |
| 18 | +Python normally does not reject a call just because it conflicts with an |
| 19 | +annotation. Type hints are metadata that tools can inspect. |
| 20 | + |
| 21 | +A static type checker reads the code without running it and reports operations |
| 22 | +that are inconsistent with the annotations. This project uses mypy for that |
| 23 | +job. |
| 24 | + |
| 25 | +Tests and type checking answer different questions: |
| 26 | + |
| 27 | +- pytest runs code and checks observed behavior, |
| 28 | +- mypy analyzes possible value types before the code runs. |
| 29 | + |
| 30 | +Both are useful, and neither replaces the other. |
| 31 | + |
| 32 | +## Add mypy as a development dependency |
| 33 | + |
| 34 | +mypy is needed while developing the project, not while using `text-toolkit` as |
| 35 | +a library. Add it to the `dev` dependency group: |
| 36 | + |
| 37 | +```bash |
| 38 | +uv add --group dev mypy |
| 39 | +``` |
| 40 | + |
| 41 | +After cloning this repository, `uv sync` installs mypy with the other |
| 42 | +development dependencies. |
| 43 | + |
| 44 | +## Configure mypy |
| 45 | + |
| 46 | +The project keeps its mypy settings in `pyproject.toml`: |
| 47 | + |
| 48 | +```toml |
| 49 | +[tool.mypy] |
| 50 | +python_version = "3.12" |
| 51 | +files = ["src", "tests"] |
| 52 | +strict = true |
| 53 | +``` |
| 54 | + |
| 55 | +These options mean: |
| 56 | + |
| 57 | +- `python_version` checks code using Python 3.12 language rules, |
| 58 | +- `files` defines the directories checked when no paths are passed, |
| 59 | +- `strict` enables mypy's optional strictness checks. |
| 60 | + |
| 61 | +Strict mode works well here because this is a small project whose functions and |
| 62 | +tests already have annotations. Adding strict mode to a larger existing project |
| 63 | +may require a gradual approach. |
| 64 | + |
| 65 | +The exact checks included in strict mode can change between mypy releases. The |
| 66 | +project therefore runs mypy after dependency updates instead of assuming that a |
| 67 | +previous result will always stay valid. |
| 68 | + |
| 69 | +## Run the type checker |
| 70 | + |
| 71 | +Run: |
| 72 | + |
| 73 | +```bash |
| 74 | +uv run mypy |
| 75 | +``` |
| 76 | + |
| 77 | +The `files` setting means that no paths are needed in the command. A successful |
| 78 | +check reports output similar to: |
| 79 | + |
| 80 | +```text |
| 81 | +Success: no issues found in 4 source files |
| 82 | +``` |
| 83 | + |
| 84 | +## What an error looks like |
| 85 | + |
| 86 | +Consider code that assigns the integer returned by `count_words` to a variable |
| 87 | +declared as a string: |
| 88 | + |
| 89 | +```python |
| 90 | +word_count: str = count_words("clean Python project") |
| 91 | +``` |
| 92 | + |
| 93 | +mypy reports an incompatible assignment because the annotation and return type |
| 94 | +do not agree. The program does not need to run for mypy to find that mismatch. |
| 95 | + |
| 96 | +Read the file path, line number, and error code in the message. Fix the type or |
| 97 | +the code rather than silencing the warning without understanding it. |
| 98 | + |
| 99 | +## What mypy does not prove |
| 100 | + |
| 101 | +A successful mypy check does not mean that the program is correct. |
| 102 | + |
| 103 | +mypy does not replace: |
| 104 | + |
| 105 | +- tests for runtime behavior, |
| 106 | +- Ruff checks for linting and formatting, |
| 107 | +- code review, |
| 108 | +- clear program design. |
| 109 | + |
| 110 | +Type checking is one layer of feedback in the project workflow. |
| 111 | + |
| 112 | +## Rule of thumb |
| 113 | + |
| 114 | +Use type hints to describe interfaces. |
| 115 | + |
| 116 | +Use mypy to check whether the code follows those descriptions. |
| 117 | + |
| 118 | +Use pytest to check what the code actually does when it runs. |
| 119 | + |
| 120 | +## Continue |
| 121 | + |
| 122 | +- [Previous: Building Distributions](10_building_distributions.md) |
| 123 | +- [Next: Project Checklist](09_checklist.md) |
| 124 | +- [Back to README](../README.md) |
0 commit comments