If you are new to Python, you may have heard of type checking and type hinting, and wondered what they are all about. Within this post we will provide a 101 to explain what type checking and type hinting are and the problem they look to solve.

Let's dive in,

The Issue

Python is a dynamically typed language. Dynamic means the variable type is not declared when assigning the variable. Instead, variable types are checked at runtime.

Why does this matter? Well, let's take an example. In Python, we set the variable vlan_id, for which we assign a string value of 100. Along with this, we also create a function to perform an action to this variable, such as increment the value by 1:

vlan_id = "100"

def add_vlan(vlan_id):
    return vlan_id + 1

However, if we now call our function, at runtime we will get an error as we are trying to add an integer (number) to a string. In other words, our variable vlan_id should have been assigned an integer-based value instead of a string-based value.

>>> add_vlan(vlan_id)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in add_vlan
TypeError: can only concatenate str (not "int") to str

Type Hinting

So what can we do to prevent issues such as this? Well Python provides what is known as type hinting.

Type hinting allows us to hint (annotate) at what the type of a value should be.

Here's an example where we are hinting that the vlan_id should be an int, both at the point of assignment and also when being supplied as the function input.

vlan_id: int = "100"

def add_vlan(vlan_id: int):
    return vlan_id + 1
    
...

However, even though we have added type hints, we've only solved half the issue. Our type hints are only, well, a hint - an annotation on what the types "should" be. And, like before, the type checking will only occur at runtime. Therefore, in this case our code will still fail.

This is where we require a type checker such as mypy, which will parse these type hints and report any issues prior to runtime.

mypy Example

Lets now look at a quick example of checking our type hints with mypy.

# Install mypy
$ poetry add -D mypy

We can now run mypy on our file (shown below). As a result, we will get the expected issue that the variable we have assigned to vlan_id is of the wrong type. Good stuff!

$ mypy 003_type_checking/001_mypy_issues.py
003_type_checking/001_mypy_issues.py:1: error: Incompatible types in assignment (expression has type "str", variable has type "int")
Found 1 error in 1 file (checked 1 source file)

If you have enjoyed or found this useful, check out our new course -- Automating Python Code Quality.

Automating Python Code Quality
Learn how to Automate Python Code Quality through open-source tooling, to save you and your team time.
Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜