Python · Advanced level

Advanced level · Module 2: Testing and Code Quality · Lesson 4 of 4

Style, Linting and Type Hints

Keep code consistent and catch mistakes early with PEP 8, a linter and a type checker.

In this lesson you will

  • Run ruff and fix reported issues
  • Add type hints and check them with mypy

Key concepts

PEP 8lintertype hintstatic analysis

The lesson

Consistent style makes code easier to read for everyone. PEP 8 is Python's style guide; tools such as ruff check it automatically and spot likely bugs, such as unused variables.

Type hints document what a function expects and returns: def convert(amount: float, rate: float) -> float:. Editors use them to warn you about mistakes before you run the code.

Treat linter warnings as a to-do list. Fix them, or consciously decide that a rule does not apply and say why.

Step by step

  1. Install ruff and run it on your project.
  2. Fix the warnings one category at a time.
  3. Add type hints to all public functions.
  4. Run ruff and your tests again.

Practice activity

Bring your Intermediate project to zero ruff warnings and add type hints to all public functions.

Expected result: A clean ruff report and typed public functions.

Check your understanding

  1. Does Python enforce type hints at runtime?

    Show answer

    No; they are checked by tools such as mypy and help readers.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.