Power of Typehints for writing less buggy code

akhil teja (~akhil02)


2

Votes

Description:

My Talk

Python is a dynamic language i.e, it doesn't require to know the types of variables before running the code. This paradigm makes it easy for developers to write and ship code faster. But we have a problem, there's a good chance that the code contains bugs as it's design is biased with developer's assumptions that a variable will always have the type that's to be expected while it's being used.

Example:

[There's a bug in the markdown renderer of this site(in.pycon.org/cfp/pycon-india-2023). Comments in the code block are converted into headings. Therefore pasting the code snippets as images]

enter image description here

Static analyser such as mypy will not give any error and python fails to concatenate raising error during runtime, which is undesirable.

Traceback (most recent call last): File "<string>", line 9, in <module> TypeError: can only concatenate str (not "int") to str

If the developer had been cautious and added a type annotation in the function, then mypy would have caught it earlirer, before running the code.

enter image description here

Mypy catches with below error without actually running the code.

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

My talk includes use of type annotations and generics to write maintainable python code. I'll explain on below topics:

Introduction to Type hints:

I'll introduce type hints for primitive and complex data types such as int,str,list,dict, etc..,. Then I'll explain about Callables that can identify the function signatures for both parameters and return values.

Advance Type Hints:

In this section I'll deal with generators and iterators. Share details about Optional Type annotation that can either return a value or None. Union Type annotation for more than one type. I'll also share tricks to avoid circular dependencies using typing module.

Generics:

Generics will be explored in the talk to provide insights on encapsulating any kind of data type in other type (ex: list). Example: List is a type that contains suqeunce of values of any type i.e, List[int], List[str], List[dict]

Tooling:

I'll explain about mypy and other alternative tooling available to validate python code with typehints.

Prerequisites:

Basics of Python

Content URLs:

I've recently given a talk on Rustlang and below is the link to the slides.

https://slides.com/akhilg/palette

Speaker Info:

  • I'm a Data Scientist currently working at DBS (Development Bank of Singapore) Bank.
  • I've expertise in Python, Rustlang, Machine learning and Data Engineering.
  • Python is my main tool for developing software and occasionally write software in Rustlang for high performance.
  • I'm an avid open source contributor in Python and Rust during my free time.

Speaker Links:

Section: Core Python
Type: Talks
Target Audience: Intermediate
Last Updated: