Python Friday #207: Type Hints

Internally Python uses classes and therefore types everywhere. However, when we declare our methods, we omit the types and end up with limited functionality for our code editors. In this post we look at type hints and how they can improve our experience to write code.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.

 

Type hints?

Python offers type hints since version 3.5 and they are meant to be hints:

The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.

The basic example from the documentation uses this code snipped to show you the syntax:

The greetings() function accepts the parameter name of the type string and returns a string.

 

The benefits

If we add the example from above with the type hints into a module called with_types.py, we get this support in VS Code:

VS Code shows us all the methods we can call on a string.

However, if we put our method from above into a module called without_types.py and omit the type hints, we get only this support in VS Code because it knows nothing about a being a string:

Not much support from VS Code, then it does not know what type we get back.

 

What types can we use?

The “Type hints cheat sheet” offers examples for most use cases. I only picked the ones I find the most useful for Python 3.11+:

 

How to handle special cases?

If our parameter could hold a string or None, we can declare it with the Optional keyword:

If we can accept multiple types, we can use the | character to separate the different type hints:

If you do not care about the type but you need to define something, you can use Any -> None:

 

Conclusion

Type hints are optional and only hint at the type. They offer us some great help while we write code and make it clearer what type we expect in our functions. There are tools like Pydantic that leverage the type hints and give us validation and fitting error messages for no additional cost.

We are revisiting this topic when we explore FastAPI, a framework that is built on top of type hints.

1 thought on “Python Friday #207: Type Hints”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.