Discover more from Daily Dose of Data Science
A 2-min Guide to Becoming a Type Hints-Savvy Python Programmer
Must-know for Python programmers.
When writing Python code, type hints provide an incredible way to specify explicit information about:
the expected types of function arguments, and
their return type.
Yet, we all know that Python NEVER enforces them.
But does that mean they have no utility?
Of course not!
Type hints are becoming increasingly valuable to Python projects lately.
Despite not being enforced, Python developers find them extremely useful to improve code quality and maintainability.
So today, let me walk you through 10 different and must-know ways to declare type hints in just two minutes.
#1) Type hints for standard Python objects
The most basic (and must-know) way to declare type hints for standard Python objects is as follows:
Everyone knows the above type hints as these are quite common.
dict specifically, however, it is recommended to use the
typing module instead because it lets us provide more information about the object:
#2) Multiple type hints for an object
If an object can have multiple types, use the
Union keyword (or the pipe symbol if you are using Python 3.10 or above):
#3) Types hints for None values
Sometimes, an object can be None or have any other standard data type(s). Declare such objects as follows:
#4) Type hints for iterable
Iterables are objects you can iterate on — list, tuple, or dict.
Declare such objects as follows:
#5) Type hints for constant objects
Some objects can never change their value throughout the program. They must remain constant. Declare them using
Final as follows:
#6) Type hints for fixed values
Some objects may not be constant but may only take a fixed set of values. For instance,
grade may only take values from a fixed set of grades.
Declare such variables as follows:
#7) Type hints for objects with any possible value
Some objects could be of any type — list, int, string, etc. Declare them as follows:
#8) Type hints in functions
We declare type hints in functions in two places:
To specify type hints for its parameters.
To specify the type hint for the object returned by the function.
You can use any of the above discussed type hints to declare type hints in functions — Any, Union, Iterable, Optional etc.
#9) Type hint of a function object
At times, a function may have another function as its parameters. This happens all the time in Python decorators.
We can declare the data type for such parameters as follows:
Callable, as the name suggests, is any object that can be invoked —
#10) Type hint aliasing
If type hints become big, complex, and unreadable, we can alias them by declaring shorter names for them:
These are the 10 most common and must-know ways to declare type hints in Python.
Of course, as discussed earlier, Python never enforces type hints.
Yet, they are immensely valuable and a must-know skill if you want to write better-documented, more reliable, and maintainable code.
👉 Over to you: What type hints did I miss?
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (550+ pages) with 320+ tips.
👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.
Thanks for reading!
Latest full articles
If you’re not a full subscriber, here’s what you missed last month:
To receive all full articles and support the Daily Dose of Data Science, consider subscribing:
👉 Tell the world what makes this newsletter special for you by leaving a review here :)
👉 If you love reading this newsletter, feel free to share it with friends!