This gist is a collection of all the easy-to-find online resources for using typed python as of 2022/01. I collected these notes for two reasons:
- As an aside in an extensive python-dev typing discussion, Guido mentioned that it would be helpful
- There’s an active but very slow-moving effort to write official python
type system docs in
python/typing
, and I figured having an easy to skim list of the best existing discussions would be handy.
The official python typing docs web source are very much in construction.
Much of the knowledge about typing patterns is tribal rather than documented, the typing repo discussions have quite a bit of Q&A about how to use types.
The typing module docs are reasonably good, but very much just a reference.
The closest thing to complete documentation at the moment is the mypy docs page.
The other type checker docs generally don’t cover the type system itself in detail:
- pyre has a very short discussion of “Types in Python” and some helpful details in the errors page (which catalogues every error code and tries to provide an example).
- pyright docs mainly describing behavior of various “advanced” cases
- pytpye describes how its behavior differs from mypy
I looked at the top results of the following Google queries on 2022/01/26:
- “how to write typed python”
- “using type annotations python”
- “effective typed python”
- “effective python annotations”
The following seraches didn’t show anything relevant at all in the first few pages:
- “typed python design patterns”
The following are the most useful documentation-style resources I found:
- RealPython.com: Python Type Checking
- Other than the MyPy docs I think this is the most detailed guide I found:
- It covers most of the basic PEP 484 features
- The mypy docs are more reference than tutorial, this is probably the best tutorial-form resource out there.
- Other than the MyPy docs I think this is the most detailed guide I found:
- LogRocket blog This is the most up-to-date and complete of the blogs I found
- It covers an impressive number of the features of typing
- This is probably the second-best tutorial-style guide I’ve found.
- 20 Type Hinting Tricks and Tools (medium paywall)
- This blog actually has many details. It benefits from not trying to be complete.
- It discusses not only typing patterns but the various static and dynamic typing tools.
A few additional top hits are not very tutorial-like, but still interesting:
- Why I started using python type annotations, which discusses movivation for using types.
- Jukka’s Dropbox blog about typed python, focued more on the story and implementation.
Several more blogs and tutorials that were reasonably well-written but too short to contain very much information also came up in the top search results.
- PythonTutorials.net: Python Type Hints
- Get started with Python Type Hints
- Static Typing in Python - this one discusses Callable, but not the generic form.
- Towards Data Science: Type Annotations in Python (medium paywall)
- Towards Data Science: How to make Python Statically Typed (medium paywall)
- Type Annotations in Python 3.8 (medium paywall)
- Python Type Annotations (medium paywall)
- How to use Static Type Checking in Python 3.6 (medium paywall)
- The state of type hints in Python (circa 2018/05)
- Referenced by the RealPython tutorial above
- Has recommendations on when to use type hints