Python Typing Hints List

Posted on Fri, 24 May 2019 in Python

Here is my small list of sources of type hints in Python that I use almost every day.

Recently I changed a project and a team. Now I'm working on DWH and ETL-processes. It is a quite big project. There is roughly the same amount of LOC but there are more developers working on it. I'm sure that type hints are more necessary for it.

Not only because of the number of developers but because of specific features of DWH/ETL-projects. They tend to get data from different sources and store them in different storages for different types of consumers. That leads to a code fragmentation. Without "the glue" the project falls apart to a bunch of scripts.

As soon as I joined the project I started to add type hints to important parts that worked on. Gradually other colleges joined in. After a while, we got a critical mass of typed Python code. That's why we decided to make type hinting obligatory (not for the entire project, but for a significant part of it).

I was asked to make a list of the most necessary and useful links that could help in everyday work. There were several requirements to that list:

  • It should contain an as small amount of links as possible
  • It should contain only that is useful for everyday work
  • It should contain only links to short dense articles

I tried to follow these restrictions not always successfully. However, I did my best. So here is my list.

It is a very long Real Python's tutorial. It covers almost everything from type theory to mypy setup. It's really The Ultimate Guide to Python Type Checking. It's the best place to start. The main disadvantage of this source that it is too long.

In contrast to the Ultimate Guide this cheat sheet is extremely short. It is easy to use. Mypy developers cover all basic cases (85-90% of all cases that I've ever met). At the very beginning of my type of hinting experiments, I used to use it constantly. However, this source covers only very basic cases. If you need to solve something complex, the cheat sheet isn't for you.

Python Documentation is one of the reasons for my love for this language. Docs for typing library isn't an exception. The majority of the questions can be solved here. Almost always it's enough just to look through it to get the right idea of type hinting pattern.

Sometimes documentation isn't enough. It's time for PEP 484 and PEP 483. I think these two PEPs are only I've ever used in everyday work. They are really handful to understand the logic on which type hinting is based. Without that understanding often it's next to impossible to find an appropriate solution on how to add a hint in a code.

That is all the links I use more or less regularly. Of course, sometimes I have to look through StackOverflow. But as usual, you have to know what you are looking for and how to use it. Sometimes the right variant isn't the most popular one or checked as "right" one.

All these links tell nothing about creating processes for type checking in a project. I wrote about it in 2017. Here is the link to this article.