learning-it by example

Back to the video

Type Hints and mypy: Catch Data Bugs Before You Run

Mypy reported 4 errors from 3 bugs without running the code, and missed a wrong average.

Video #145 ยท Python for data

Steps in the video

  1. 0:00 Four mypy errors from three bugs, found without running the code
  2. 0:30 The idea
  3. 1:04 Setup
  4. 1:26 Two helpers
  5. 1:45 Three bugs
  6. 3:03 The fixes
  7. 3:25 A bug the types cannot see
  8. 4:08 What this does not show
  9. 4:34 Mistakes to avoid
  10. 5:01 My take and next steps
  11. 5:26 Recap

Files

  • bad.py (34 lines)
  • blind.py (14 lines)
  • good.py (36 lines)
  • run.py (17 lines)

Download the code (zip)

Tested with

Python 3.12.15 | mypy==2.4.0

What this does not show

Made-up orders and three bugs I wrote on purpose; standard library only, so no data frames were tested; one mypy version, two runs; mypy skips functions without hints by default.

Sources

Learning IT by Example.