← Return to program

Taming the Duck with Type Hints

Sunday 2:50 PM–3:20 PM in Hall B

Python with its flexible typing is amazingly productive - in a short time you can come up with an awesome program. But as that program ages and grows its like a child with untied shoe laces. Which is where type hints come in - type hints are easy to use, will help tie up laces and continually check that they don't come undone again.

See this talk and many more by getting your ticket to PyCon AU now!

I want a ticket!

Python is a dynamically duck-typed programming language. This means that an object's type can change at any time (dynamic), and an object's type isn't checked before use - instead if the operation works then the the object was close enough (duck typing - it quacked like a duck so it must be a duck!).

Together dynamic and duck typing help make Python a great language to get complicated things up and running quickly. That has made Python a great language to learn as you can quickly become very productive.

But this strength for easy experimentation can quickly become a weakness as your program grows - mistakes with the type of variables and arguments can be really hard to find. In fact, if testing isn't thorough enough, its very easy for these type errors to leak into released code.

Enter Type Hints. These 'hints' can be added at any time (you can add them after the quick experimentation stage) to help you in two ways. First, they document to others (and yourself in a months time) what types your functions expect, and second, they allow static checking tools like mypy to check that your code honours those type hints.

This talk explains how types work in python and, using code examples, how easy it is for type errors to creep into code. Then we look at type hints, how easily they can be added to code and how to run tools like 'mypy' to find problems, fix them, and re-check your code as you make changes!

Brian Danilko

Brian has produced software for over 30 years on lots of different embedded projects and still finds it interesting, and loves learning new things. He has been a python user for over 20 of those years, making tools, writing tests, creating educational robot programming systems, and, at one time, keeping his financial books on a python system. Brian has always been passionate about programming languages and tools, and about communicating the particularly interesting bits to others.