Coding standards and tools
Last updated
Code is read far more often than it's written, by teammates, reviewers and you in six months. A team that agrees how its code looks, and lets a tool enforce it, reads faster and argues less. In this lesson you'll meet Python's style guide, PEP 8, see what a formatter and a linter each do, and use a linter's warning to find a real bug in Pila's reply code.
By the end of this lesson you can
- Explain why a team agrees on one code style
- Tell a formatter, a linter and a type checker apart
- Read linter warnings and fix what they point at
Code is read more than it’s written
Guido van Rossum, who created Python, observed that code is read much more often than it's written. PEP 8, Python's style guide, exists for the reader: four spaces per indent, snake_case names for functions and variables, CapWords for classes, UPPER_CASE for constants like DAILY_LIMIT, spaces around operators, imports at the top.
The rules themselves matter less than everyone following the same ones. Code in one consistent style reads like one author wrote it; reviewers can look at what the code does instead of how it's laid out. PEP 8 also says when to break it: when following it would make the code less readable.
Formatter, linter, type checker
Three kinds of tool do the checking, so people don't have to:
| Tool | What it does | For Python |
|---|---|---|
| Formatter | Rewrites the layout to one style: spacing, line breaks, quotes. Never changes what the code does. | ruff format, or Black |
| Linter | Warns about code that’s probably a mistake or hard to read, with a rule code for each. | ruff check, or Pylint |
| Type checker | Reads type hints and checks that values of the right types are passed around. | mypy, or Pyright |
Here's what Ruff, a fast formatter and linter for Python, says about a messy version of Pila's reply code:
ruff format sms.py 1 file reformatted ruff check sms.py sms.py:1:8: F401 [*] `os` imported but unused sms.py:3:16: E711 Comparison to `None` should be `cond is None` sms.py:6:5: F841 Local variable `reply` is assigned to but never used Found 3 errors. [*] 1 fixable with the `--fix` option.
Teams run these automatically: in the editor as they type, before each commit (Simple Git's Hooks and checks shows a pre-commit hook), and on every pull request (lesson 7.1).
Which tool catches it?
0 of 6 placed right
Puts spaces around
=and after commasSplits a 120-character line into several
Warns that
osis imported but never usedWarns that a variable is assigned but never used
Flags a bare
except:that hides every errorSays
take_number()was given a string where it expects a time
A warning that’s a bug
Here's the messy code. Run it: the first reply is missing the wait time Ate Cora asked for. Ruff's F841 warning pointed straight at it: the reply with the wait is built, then thrown away. Fix the bug so the first line reads as below, then tidy the rest the way the formatter and linter would: remove the unused import, use is None, and space it like PEP 8.
Type hints say what a function expects, and a type checker reads them. They don't change how Python runs the code:
def reply_for(number: int | None, wait_minutes: int) -> str:
...Spot the trap: tabs and spaces in review
Bea’s pull request gets nineteen review comments. Sixteen are about layout: quotes, blank lines, where a long line breaks. She and Marco start arguing about single and double quotes.
What happens if…
Check what you learned
Sources
Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.
- PEP 8: Style Guide for Python Code: “code is read much more often than it is written”
- Ruff: The Ruff Formatter: a formatter designed as a drop-in replacement for Black
- Ruff: The Ruff Linter: its default rules and the fixes it can make
- Ruff: Rules: F401, F841, E711, E722 and the rest, each explained
- PEP 484: Type Hints: annotations that type checkers read