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!