Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

LEARNING ABOUT PROTOCOLS ...and contributing to typeshed 3

Slide 4

Slide 4 text

PRINCIPLES 4

Slide 5

Slide 5 text

STATIC V. DYNAMIC TYPING 5 dynamic typing static typing

Slide 6

Slide 6 text

STATIC V. DYNAMIC TYPING 6 RUN TIME CHECKING STATIC
 CHECKING dynamic typing static typing

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

THE POWER OF MAX 13

Slide 14

Slide 14 text

14

Slide 15

Slide 15 text

EXAMPLES WITH 2-ARGUMENT MAX 15

Slide 16

Slide 16 text

16

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

MAX WITH NUMBERS 18

Slide 19

Slide 19 text

EXAMPLES WITH 2-ARGUMENT MAX 19

Slide 20

Slide 20 text

STEP 0: FLOATS 20

Slide 21

Slide 21 text

0. IMPLEMENTATION & TESTS 21

Slide 22

Slide 22 text

0. TESTS PASSING 22

Slide 23

Slide 23 text

0. PENDING ISSUE 23

Slide 24

Slide 24 text

24

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

1. IMPLEMENTATION 26

Slide 27

Slide 27 text

ISSUE 1.1: UNSUPPORTED OPERAND >= 27

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

29

Slide 30

Slide 30 text

2ND TRY: NUMERIC UNION
 
 (MISSTEP) 30

Slide 31

Slide 31 text

2. IMPLEMENTATION 31

Slide 32

Slide 32 text

ISSUE 2.1: NUMERIC UNION IS INCOMPATIBLE WITH FLOAT 32

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

STEP 3: RESTRICTED TYPEVAR 34

Slide 35

Slide 35 text

3. IMPLEMENTATION 35

Slide 36

Slide 36 text

3. TESTS PASSING 36

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

DUCK TYPED MAX 38

Slide 39

Slide 39 text

MORE EXAMPLES WITH 2-ARGUMENT MAX 39

Slide 40

Slide 40 text

MORE EXAMPLES WITH 2-ARGUMENT MAX 40

Slide 41

Slide 41 text

MORE EXAMPLES WITH 2-ARGUMENT MAX 41

Slide 42

Slide 42 text

4TH TRY: PROTOCOL RESULT (MISSTEP) 42

Slide 43

Slide 43 text

4. IMPLEMENTATION 43

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

4. MYPY UNHAPPY 45

Slide 46

Slide 46 text

STEP 5: PROTOCOL + BOUND TYPEVAR 46

Slide 47

Slide 47 text

5. IMPLEMENTATION 47

Slide 48

Slide 48 text

5. TESTS PASSING, MYPY HAPPY 48

Slide 49

Slide 49 text

5. RESULTS FIXED 49

Slide 50

Slide 50 text

MAX OVERLOAD 50

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

OTHER USES 52

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

WRAPPING UP 54

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

CELEBRATE AND EMBRACE GRADUAL TYPING 57 RUN TIME CHECKING STATIC
 CHECKING gradual typing dynamic typing static typing

Slide 58

Slide 58 text

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