Typing in Python

Ridwan Yusuf
3 min readJan 10, 2023

Python is not a statically-typed language. This means that it does not enforce explicit type declaration for variables and functions. Though this aids fast application development, it can make a large code-base less
maintainable and prone to errors.

The python interpreter only check types when the code is running(at runtime). For instance, it is impossible to know if 1 (an Integer) when
summed with ‘name’ (a String) would throw an error ahead of running the program.

Statically-typed languages does this check without running the program

Take a look at this function declaration snippet:

def do_something(a,b):
return a + b

We define a function do_something and it’s expected to take two parameters a and b.

The expected behavior of + operator when used with numbers is summation e.g. 1+2 = 3 while the expected behavior of this same operator with strings is concatenation e.g. ‘na’ + ‘me’ = ‘name’

As a reader of the above code, multiple assumptions might be running in your mind. What does the writer of this code intend (Number summation or String Concatenation). And in fact, what is the
do_something function expected to return after all computations? a String, an Integer, or a Float?

Typing provides a solution to the above mystery by making code more readable(a code that communicates its intent and what it does by mere reading)

The above code snippet can be made more readable by specifying the type of variables and function return type.
[*]Typing syntax coming shortly

def do_something(a:str, b: str) -> str:
return a + b

#-We declared both a and b as string.
#The return value also expected to be string

Up here, we declared both a and b as string. The return value also expected to be string

def do_something(a:int, b:int) -> int 
return a + b
#a, b, and the return value type are all declared as an integer

Another important benefit of typing is that it provides a solution to type checking before running the actual python code when used with type checking tool like mypy.

mypy offers a solution similar to Statically-typed language like Java. For instance, you can know if 1 (an integer) when summed with ‘name’ (a string) would throw an error before running the actual code.

It is worth noting that python does not enforce the this type checking, a reason why it is called Type Hinting(i.e. give an hint without actual enforcement)
Below is the syntax:

[*]variable syntax

   var_name:var_type = value 
id: int = 1
user_name: str = 'ray'
account_balance: float = 920.06
is_acticve: bool = True

from typing import Dict, List

friuts: List[str] = ['mango','orange']
data: Dict[str, str] = {'name':'ray','status':'active'}

[*] function syntax

 def (a:int, b:int) -> int:
return a + b

In function, the syntax for specifying the return type is ->.

mypy usage for type checking.
Once mypy is installed(pip install mypy), create a file called python_typing.py:

#python_typing.py file

def add(a:int, b:str)-> int:
return a + b

Run mypy python_typing.py so that mypy can check do a check on the code snippet

python_typing.py:2: error: Unsupported operand types for + ("int" and "str")  [operator]
Found 1 error in 1 file (checked 1 source file)

Interestingly, we got an error because we are trying to sum two unsupported data types (a is integer and b is a string)

So let’s modify the code

def add(a:int, b:int)-> str:
return a + b
python_typing.py:2: error: Incompatible return value type (got "int", expected "str")  [return-value]
Found 1 error in 1 file (checked 1 source file)

We fix the first error by making both parameters integer. but now we intentionally return str. So mypy complained that a+b result type(int) is not compatible with what we specify (str) as return type.

def add(a:int, b:int) -> str:
return str(a) + str(b)
Success: no issues found in 1 source file

Great! We got it working.

I hope this put some clarification on Python typing

Check this link for a cheatsheet you can always refer to.

mypy doc link

Thanks for reading and feel free to explore my video collection on YouTube for more educational content. Don’t forget to subscribe to the channel to stay updated with future releases.



Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use