Skip to main content
EuroPythonCode of ConductLive đŸ“č

`typing.Protocol`: type hints as Guido intended

Room:
The Auditorium
Start (Dublin time):
Start (your time):
Duration:
45 minutes

Abstract

If your type-hinted Python code is Java flavored, you're probably underusing typing.Protocol. Python is literally built on structural typing, a.k.a. duck typing. It's how __special__ methods work. Type hints were introduced in Python 3.5 without support for duck typing, but it was added in Python 3.8 and we should all be using typing.Protocol to have our code statically checked and Pythonic.

TalkSoftware Engineering & Architecture

Description

Duck typing and static typing are not opposites. Go is a successful statically checked language with support for duck typing through interfaces that work like typing.Protocol does. A Protocol subclass defines an interface that past and future classes can implement without any coupling to the interface: they simply provide the required methods. That's statically checked duck typing: a powerful combination!

In this talk we'll get back to basics looking at how duck typing is used in Python since the beginning, how __dunder__ methods leverage that idea to support what we recognize as Pythonic code. Then we'll see how typing.Protocol fills the gap in the original PEP 484—Type Hints, and finally lets us properly annotate code that leverages the flexibility and loose coupling of duck typing. Finally, we'll look at the experience of the Go community to learn what makes a good Protocol. Spoiler alert: your favorite Python ABC may not be the basis of a useful Protocol!


The speaker

Luciano Ramalho

Luciano Ramalho is the author of Fluent Python, published in 9 languages. He is a Principal Consultant at Thoughtworks and a Fellow of the Python Software Foundation.



← Back to schedule