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

    View full-size slide

  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

    View full-size slide

  3. LEARNING ABOUT PROTOCOLS
    ...and contributing to typeshed
    3

    View full-size slide

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

    View full-size slide

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

    CHECKING
    dynamic
    typing
    static
    typing

    View full-size slide

  6. 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

    View full-size slide

  7. TWO DIMENSIONS OF TYPING
    8
    STRUCTURAL TYPES
    NOMINAL TYPES
    RUN TIME

    CHECKING
    STATIC

    CHECKING
    duck

    typing
    static

    typing

    View full-size slide

  8. TWO DIMENSIONS OF TYPING
    9
    STRUCTURAL TYPES
    NOMINAL TYPES
    RUN TIME

    CHECKING
    STATIC

    CHECKING
    duck

    typing
    static

    typing
    Java Land
    Python Land

    View full-size slide

  9. TWO DIMENSIONS OF TYPING
    10
    STRUCTURAL TYPES
    NOMINAL TYPES
    RUN TIME

    CHECKING
    STATIC

    CHECKING
    duck

    typing
    static

    typing
    goose
    typing
    supported
    by ABCs

    View full-size slide

  10. 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

    View full-size slide

  11. THE POWER OF MAX
    13

    View full-size slide

  12. EXAMPLES WITH 2-ARGUMENT MAX
    15

    View full-size slide

  13. MAX WITH NUMBERS
    18

    View full-size slide

  14. EXAMPLES WITH 2-ARGUMENT MAX
    19

    View full-size slide

  15. STEP 0: FLOATS
    20

    View full-size slide

  16. 0. IMPLEMENTATION & TESTS
    21

    View full-size slide

  17. 0. TESTS PASSING
    22

    View full-size slide

  18. 0. PENDING ISSUE
    23

    View full-size slide

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

    View full-size slide

  20. 1. IMPLEMENTATION
    26

    View full-size slide

  21. ISSUE 1.1: UNSUPPORTED OPERAND >=
    27

    View full-size slide

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

    View full-size slide

  23. 2ND TRY: NUMERIC UNION


    (MISSTEP)
    30

    View full-size slide

  24. 2. IMPLEMENTATION
    31

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  27. STEP 3: RESTRICTED TYPEVAR
    34

    View full-size slide

  28. 3. IMPLEMENTATION
    35

    View full-size slide

  29. 3. TESTS PASSING
    36

    View full-size slide

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

    View full-size slide

  31. DUCK TYPED MAX
    38

    View full-size slide

  32. MORE EXAMPLES WITH 2-ARGUMENT MAX
    39

    View full-size slide

  33. MORE EXAMPLES WITH 2-ARGUMENT MAX
    40

    View full-size slide

  34. MORE EXAMPLES WITH 2-ARGUMENT MAX
    41

    View full-size slide

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

    View full-size slide

  36. 4. IMPLEMENTATION
    43

    View full-size slide

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

    View full-size slide

  38. 4. MYPY UNHAPPY
    45

    View full-size slide

  39. STEP 5: PROTOCOL +
    BOUND TYPEVAR
    46

    View full-size slide

  40. 5. IMPLEMENTATION
    47

    View full-size slide

  41. 5. TESTS PASSING, MYPY HAPPY
    48

    View full-size slide

  42. 5. RESULTS FIXED
    49

    View full-size slide

  43. MAX OVERLOAD
    50

    View full-size slide

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

    View full-size slide

  45. OTHER USES
    52

    View full-size slide

  46. OTHER USES IN THE STANDARD LIBRARY
    _typeshed._SupportsLessThan is used in these packages
    and functions of the Python 3.9 standard library:
    53

    View full-size slide

  47. WRAPPING UP
    54

    View full-size slide

  48. 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

    View full-size slide

  49. 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

    View full-size slide

  50. CELEBRATE AND EMBRACE GRADUAL TYPING
    57
    RUN TIME
    CHECKING
    STATIC

    CHECKING
    gradual
    typing
    dynamic
    typing
    static
    typing

    View full-size slide

  51. e-mail: [email protected]
    @ramalhoorg
    THANK YOU!

    View full-size slide