Save 37% off PRO during our Black Friday Sale! »

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.

27c093d0834208f4712faaaec38c2c5c?s=128

Luciano Ramalho

May 10, 2021
Tweet

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 luciano.ramalho@thoughtworks.com @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. LEARNING ABOUT PROTOCOLS ...and contributing to typeshed 3

  4. PRINCIPLES 4

  5. STATIC V. DYNAMIC TYPING 5 dynamic typing static typing

  6. STATIC V. DYNAMIC TYPING 6 RUN TIME CHECKING STATIC
 CHECKING

    dynamic typing static typing
  7. 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
  8. TWO DIMENSIONS OF TYPING 8 STRUCTURAL TYPES NOMINAL TYPES RUN

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

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

    TIME
 CHECKING STATIC
 CHECKING duck
 typing static
 typing goose typing supported by ABCs
  11. 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
  12. 12

  13. THE POWER OF MAX 13

  14. 14

  15. EXAMPLES WITH 2-ARGUMENT MAX 15

  16. 16

  17. None
  18. MAX WITH NUMBERS 18

  19. EXAMPLES WITH 2-ARGUMENT MAX 19

  20. STEP 0: FLOATS 20

  21. 0. IMPLEMENTATION & TESTS 21

  22. 0. TESTS PASSING 22

  23. 0. PENDING ISSUE 23

  24. 24

  25. 1ST TRY: NUMBER (ABC) (MISSTEP) 25

  26. 1. IMPLEMENTATION 26

  27. ISSUE 1.1: UNSUPPORTED OPERAND >= 27

  28. From the standard library: cpython/Lib/numbers.py CAUSE 1.1: NUMBER ABC HAS

    NO METHODS ! ! 28
  29. 29

  30. 2ND TRY: NUMERIC UNION
 
 (MISSTEP) 30

  31. 2. IMPLEMENTATION 31

  32. ISSUE 2.1: NUMERIC UNION IS INCOMPATIBLE WITH FLOAT 32

  33. ISSUE 2.2: CAN'T USE A FRACTION RETURNED AS NUMERIC 33

  34. STEP 3: RESTRICTED TYPEVAR 34

  35. 3. IMPLEMENTATION 35

  36. 3. TESTS PASSING 36

  37. 3. ISSUE 2.1 FIXED: RETURN TYPE REFLECTS ARGUMENT TYPES 37

  38. DUCK TYPED MAX 38

  39. MORE EXAMPLES WITH 2-ARGUMENT MAX 39

  40. MORE EXAMPLES WITH 2-ARGUMENT MAX 40

  41. MORE EXAMPLES WITH 2-ARGUMENT MAX 41

  42. 4TH TRY: PROTOCOL RESULT (MISSTEP) 42

  43. 4. IMPLEMENTATION 43

  44. 4. TESTS PASSING, MYPY HAPPY (SO FAR) 44

  45. 4. MYPY UNHAPPY 45

  46. STEP 5: PROTOCOL + BOUND TYPEVAR 46

  47. 5. IMPLEMENTATION 47

  48. 5. TESTS PASSING, MYPY HAPPY 48

  49. 5. RESULTS FIXED 49

  50. MAX OVERLOAD 50

  51. SIX OVERLOADS FOR DIFFERENT ARGUMENT COMBINATIONS Code: https://github.com/python/typeshed/…/stdlib/builtins.pyi 51

  52. OTHER USES 52

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

    these packages and functions of the Python 3.9 standard library: 53
  54. WRAPPING UP 54

  55. 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
  56. 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
  57. CELEBRATE AND EMBRACE GRADUAL TYPING 57 RUN TIME CHECKING STATIC


    CHECKING gradual typing dynamic typing static typing
  58. e-mail: luciano.ramalho@thoughtworks.com @ramalhoorg THANK YOU!