Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Leveraging Tools for Static Type Checking

00:00 Leverage Tools for Static Type Checking As a dynamically typed language, Python doesn’t actually enforce type hints at runtime. This means that a function can specify any desired return type, and the program would still run without actually returning a value of that type or raising an exception.

00:17 Even though Python doesn’t enforce type hints at runtime, you can use third-party tools for type checking, some of which may integrate with your code editor through plugins.

00:27 They can be helpful for catching type-related errors during the development or testing process. Mypy is a popular third-party static type checker tool for Python.

00:38 Other options include Pytype, Pyre, and Pyright. They all work by inferring variable types and their values and checking against the corresponding type hints.

00:48 To use mypy in your project, start by installing it in your virtual environment using pip.

01:01 This will bring the mypy command into your project. If you recall the parse_email() function from earlier in the course, it takes a string with an email address as a parameter.

01:12 Other than that, it returns either None or a tuple of two strings containing the username and the domain. Save the function in a file named email_parser.py if you haven’t done so already.

01:32 You run this code through mypy by typing mypy, followed by the name of the Python file at the command line.

01:43 This runs an automated static code analysis without executing the code. mypy tries to assess whether the actual values will have the expected types according to the type hints declared.

01:54 In this case, everything seems to be correct. But what happens if you’ve made a mistake in your code? Let’s say that the return value of parse_email has an incorrect type hint indicating a single string instead of a tuple of two strings.

02:17 The modified parse_email() function has a discrepancy between the type hint and one of the values that it actually returns.

02:26 When you run mypy on the modified script, you’ll see the error as on screen. This message indicates that the function returns a tuple with two strings instead of the expected single string value.

02:38 This information is invaluable because it can prevent bugs from happening at runtime. In addition to type checking, mypy can infer types for you.

02:48 When you run your script through mypy, you can pass in any expression or variable to the reveal_type() function without having to import it.

02:56 It will infer the type of the expression and print it out for you to see. When annotating functions with type hints, you can call reveal_type() for trickier cases. On screen, you can see how to use this function to determine the actual type of the parse_email return value.

03:15 Here you see the variable result contains the two components from a parsed email address. You can pass this variable into the reveal_type() function for mypy to infer the type. Note that the IDE used on screen, Visual Studio Code, is highlighting result for informational purposes with a blue underline, and that hovering over this will reveal the type of the return that it is inferred.

03:40 Here’s how you’d run it in the console.

03:46 When you run mypy on the Python file, it prints out the inferred type from the script’s reveal_type() function. In this example, mypy correctly infers that the result variable is a tuple containing two strings or an empty value of None.

04:01 Remember, that IDEs and third-party static type checker tools can catch type-related errors in the development and testing process. These tools infer the type from return values and ensure that functions are returning the expected type.

04:15 So make sure to take advantage of this useful function as it can reveal type hints of more complicated return types. In the next section of the course, you’ll take a look back at what you’ve learned.

Become a Member to join the conversation.