Building it · lesson 6 of 10
Ideas in this lesson
APEP 8Python’s style guide
Bruff formata formatter: rewrites the layout, never the meaning
Cruff checka linter: warns about suspicious code
Ddef f(n: int) -> strtype hints, checked by a type checker

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:

Three tools that read your code
ToolWhat it doesFor Python
FormatterRewrites the layout to one style: spacing, line breaks, quotes. Never changes what the code does.ruff format, or Black
LinterWarns about code that’s probably a mistake or hard to read, with a rule code for each.ruff check, or Pylint
Type checkerReads 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:

Terminal
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

  1. Puts spaces around = and after commas

  2. Splits a 120-character line into several

  3. Warns that os is imported but never used

  4. Warns that a variable is assigned but never used

  5. Flags a bare except: that hides every error

  6. Says 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.

Step 4 code sms.py
Edit the code, then run it again

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

Spot the trapBea

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

What does a formatter change?
The linter says a variable is assigned but never used. What should you do?
Why agree on one style and let a tool enforce it?

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.

All sources for Simple Software Development