Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Protocol: keystone of Python type hints

Protocol: keystone of Python type hints

The static type system supporting type hints in Python is becoming more expressive with each new PEP, but PEP 544—Protocols: Structural subtyping (static duck typing) is the most important enhancement since type hints were first introduced. The typing.Protocol special class lets you define types in terms of the interface implemented by objects, regardless of type hierarchies, in the spirit of duck typing—but in a way that can be verified by static type checkers and IDEs.

Luciano Ramalho

May 10, 2021
Tweet

More Decks by Luciano Ramalho

Other Decks in Programming

Transcript

  1. TYPING . PROTOCOL The keystone of Python's static type system

    d u c k t y p i n g f t w 🦆 Luciano Ramalho [email protected] @ramalhoorg
  2. THE LIZARD BOOK, SECOND EDITION •1st ed: published in 9

    languages •Now with: 100+ pages on type hints, many annotated examples •New async/await content, examples with FastAPI, asyncio & Curio •Draft available on early release at O'Reilly Learning (oreilly.com) •In print: Q4, 2021 (estimated) 2
  3. Don’t check whether it is-a duck:
 check whether it quacks-like-a

    duck,
 walks-like-a duck, etc, etc,
 depending on exactly what subset
 of duck-like behavior you need [...] — Alex Martelli in comp-lang-python, 2000-07-26
 "polymorphism (was Re: Type checking in python?)" 
 source: https://mail.python.org/pipermail/python-list/2000-July/046184.html Duck typing defined
  4. TWO DIMENSIONS OF TYPING 8 STRUCTURAL TYPES NOMINAL TYPES RUN

    TIME
 CHECKING STATIC
 CHECKING duck
 typing static
 typing
  5. TWO DIMENSIONS OF TYPING 9 STRUCTURAL TYPES NOMINAL TYPES RUN

    TIME
 CHECKING STATIC
 CHECKING duck
 typing static
 typing Java Land Python Land
  6. TWO DIMENSIONS OF TYPING 10 STRUCTURAL TYPES NOMINAL TYPES RUN

    TIME
 CHECKING STATIC
 CHECKING duck
 typing static
 typing goose typing supported by ABCs
  7. TWO DIMENSIONS OF TYPING 11 STRUCTURAL TYPES NOMINAL TYPES RUN

    TIME
 CHECKING STATIC
 CHECKING duck
 typing static
 typing goose typing static duck typing focus of this talk
  8. 12

  9. 14

  10. 16

  11. 24

  12. 29

  13. OTHER USES IN THE STANDARD LIBRARY _typeshed._SupportsLessThan is used in

    these packages and functions of the Python 3.9 standard library: 53
  14. PROTOCOLS: THE KEYSTONE OF PYTHON'S TYPE HINTS PEP 484—Type Hints

    not possible to correctly annotate max etc. from the standard library and from important packages (e.g. Requests)
 PEP 544—Protocols: Structural subtyping problem solved with the introduction of static duck typing 55
  15. I'm not against types, but I don't know of any

    type systems that aren't a complete pain, so I still like dynamic typing. — Alan Kay, lead developer of Smalltalk
 2003 ACM Turing Award Laureate source: http://www.purl.org/stefan_ram/pub/doc_kay_oop_en
  16. CELEBRATE AND EMBRACE GRADUAL TYPING 57 RUN TIME CHECKING STATIC


    CHECKING gradual typing dynamic typing static typing