Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Python Type Checking: Summary

Type hinting in Python is a very useful feature that you can happily live without. Type hints don’t make you capable of writing any code you can’t write without using type hints. Instead, using type hints makes it easier for you to reason about code, find subtle bugs, and maintain a clean architecture.

In this course, you learned how type hinting works in Python, and how gradual typing makes type checks in Python more flexible than in many other languages. You’ve seen some of the pros and cons of using type hints, and how they can be added to code using annotations or type comments. Finally, you saw many of the different types that Python supports, as well as how to perform static type checking.

There are many resources to help you learn more about static type checking in Python. PEP 483 and PEP 484 give a lot of background on how type checking is implemented in Python. The Mypy documentation has a great reference section detailing all the different types available. You might also want to check out How to Use Type Hints for Multiple Return Types in Python.

To download the code in this course, click the link below:


Sample Code (.zip)

8.3 KB

00:00 This video is the conclusion and course review. Hey, congratulations! You completed the course! This course was but an introduction to type hinting and applying type checking to your code. To continue your learning, check out some of the links below this video.

00:16 I’m going to take you through a review of all that you covered in this course. You started out with an intro and course overview. Then you went right into the difference between dynamic typing and static typing, comparing the dynamic language of Python versus the static typing in something like Java. Then you learned about duck typing, where you do not check types at all.

00:41 Instead, you check for the presence of given methods or attributes.

00:48 Next, it was time to start adding type hints, and you learned how to add type hints to functions.

00:57 After that, you’re ready to type check your code using the tool Mypy, which you installed and got to practice finding type errors.

01:08 That was followed by the pros and the cons of adding type hints to your code, and you learned that type hints could be gradually added to your code. If you’re creating code that’s going to be shared with others, or is important enough to add unit tests to, then you probably should think about adding type hints.

01:26 The next video covered the history of annotations and how they’re now being used as type hints. You learned how a function has an .__annotations__ dictionary and how there’s an __annotations__ dictionary at the module level, too, for variables.

01:42 And then to compare annotations with type comments in the next video, where type comments allow you to add type checking to legacy Python code. Then there was a chance to play around with Python types a little bit further with a card game, and you got to practice using the typing module and a few additional type hints for sequences, like tuples and lists.

02:06 And then, finally, this conclusion and course review. I want to thank you for joining me on this course. Make sure that you take time to practice with what you’ve learned.

aaliquegrahame on Nov. 3, 2019

Great info! Not sure if I missed this but hints would work for user defined types as well right?

Geir Arne Hjelle RP Team on Nov. 3, 2019

Yes, type hints work great for user defined types as well.

I don’t think it’s covered in the videos, but you can find some information about using your own classes as types in the article the videos are based on (see the Supporting Material link under the video):

km on Jan. 8, 2020

Another wonderful learning !

Ghani on Oct. 17, 2020

Excellent tutorial; thanks!

glenferguson on Oct. 9, 2021

I have been using type hints for years and still learned something new in the video. Specifically, adding alias for types :)

Glenn Lehman on March 13, 2023

I came to learn about duck typing stayed for the entire tutorial.

rusbayts on May 10, 2023

Exciting and helpful, thanks a lot! Is it possible to use type hints in single-dispatch generic functions to extend their functionality? Let’s say I need to make a @singledispatchmethod implement dispatch according to the type of a range of arguments passed into the function in a container - e.g., list or tuple:

from datetime import date
from functools import singledispatchmethod
from typing import Tuple, Dict

class BirthInfo:
    def __init__(self, birth_date):
        raise ValueError(f"unsupported date format: {birth_date}")

    def _from_date(self, birth_date): = birth_dat

    def _from_tuple_of_dicts(self, birth_date: Tuple[dict, dict, dict]): = date(*tuple(pair[key]
                                for pair in birth_date
                                for key in pair))

dav = Person("Dav Smith", ({"year": 2000}, {"month": 7}, {"day": 25}))

Can it be done with type hints for sequence objects? Or is it better to create a custom class of some kind for it? The above example raises a TypeError: singledispatchmethod.register() missing 1 required positional argument: ‘cls’. It can be handled by passing ‘tuple’ as ‘cls’ into @init.register(tuple), but I am curious if it can be done just with type hints?

Become a Member to join the conversation.