Python's type checking renaissance

You may have heard that TypeScript has been taking the web development space by storm in the last few years, bringing to it static types. I believe the same thing is starting to happen in the world of Python, where type checkers like mypy, Pyre, and Pyright are increasingly used, at least where Python is used by companies to write large systems.

For the last several releases of Python, there have been an increasing number of type checking features added to each release by various PEPs:

The traffic on typing-sig, the mailing list where most major new typing features are proposed and designed, has also been seeing increasing traffic year over year.1

I personally have found type checking to be very useful when working on large Python applications2. It helps me find numerous small errors that are introduced during regular development. Sure, most of these errors would be caught by the automated tests that I write for each new feature anyway, but the type checker can find a whole bunch of errors all at once without even running the program, which decreases cycle times when adding new features:

Without type checking I tend to:

  • Write a bunch of new code.
  • Repeat 4-6 times, in rapid succession:
    • Run program to manually test.
    • Find basic error (like a missing import).
    • Fix basic error.
  • Debug/fix deeper errors in the new code.

But with type checking I can:

  • Write a bunch of new code.
  • Run the type checker. Get a report of 4-6 basic errors. Fix basic errors immediately.
  • Debug/fix deeper errors in the new code.

I appreciate this increased productivity that type checking gives me when working on large programs. And I hope that more Python users try type checking for themselves, especially as Python gains an increasing number of related features in recent releases. It’s an exciting time to be a Python developer!

Related Discussion

Related Articles

Related Projects

  • TypedDict - Python typechecker support for recognizing structured dictionaries with specific named keys mapped to specific value types.
  • trycast — Parses JSON-like values whose shape is defined by typed dictionaries (TypedDicts) and other standard Python type hints.

Update History

  • 2022-03-29:
    • Update final PEPs for Python 3.10, which has now been released.
    • Update in-progress PEPs for Python 3.11, which is now in development.
    • Update speculative PEPs for Python 3.12, which is in the future.

  1. Increasing traffic on typing-sig is reflected in an increasing number of discussions year-over-year. In 2019 there were 63 discussions. In 2020 there were 113 discussions (79% increase). And 2021 is just beginning.

  2. I personally use type checking in a large Django web application that I work on.