Slide 1

Slide 1 text

Static Type Analysis for Robust Data Products (with Python) Marco Bonzanini PyData London 2017

Slide 2

Slide 2 text

Nice to meet you

Slide 3

Slide 3 text

Python is weakly typed

Slide 4

Slide 4 text

Python is weakly typed strongly typed

Slide 5

Slide 5 text

Python is weakly typed strongly typed dynamically typed

Slide 6

Slide 6 text

>>> foobar = 1 >>> type(foobar)

Slide 7

Slide 7 text

>>> '1' + 1 Traceback (most recent call last): File "", line 1, in TypeError: Can't convert 'int' object
 to str implicitly

Slide 8

Slide 8 text

>>> 1.0 == 1 == True True >>> 1 + True 2 >>> 10 * False 0

Slide 9

Slide 9 text

>>> '1' * 2 '11' >>> '1' + 2 Traceback (most recent call last): File "", line 1, in TypeError: Can't convert 'int' object to str implicitly

Slide 10

Slide 10 text

Duck Typing

Slide 11

Slide 11 text

If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck. — somebody on the Web

Slide 12

Slide 12 text

EAFP principle

Slide 13

Slide 13 text

“It’s easier to ask forgiveness
 than it is to get permission” EAFP principle — Grace Hopper

Slide 14

Slide 14 text

LBYL principle

Slide 15

Slide 15 text

Tests for pre-conditions before making calls LBYL principle “Look Before You Leap”

Slide 16

Slide 16 text

Example of LBYL

Slide 17

Slide 17 text

if hasattr(duck, 'quack'): duck.quack() else: # not a duck! Example of LBYL

Slide 18

Slide 18 text

Example of EAFP

Slide 19

Slide 19 text

try: duck.quack() except: # not a duck! Example of EAFP

Slide 20

Slide 20 text

try: duck.quack() except: # not a duck! Example of EAFP

Slide 21

Slide 21 text

try: duck.quack() except AttributeError: # not a duck! Example of EAFP

Slide 22

Slide 22 text

try: dog.quack() # if the dog quacks # it’s still a duck except AttributeError: dog.woof_woof() EAFP + Duck Typing

Slide 23

Slide 23 text

So What?

Slide 24

Slide 24 text

How many type-related errors can you catch before runtime?

Slide 25

Slide 25 text

Javascript Python Java C++ How many type-related errors can you catch before runtime? Dynamic Static

Slide 26

Slide 26 text

But I Like Dynamic Types Leave Me Alone

Slide 27

Slide 27 text

•Flexibility But I Like Dynamic Types Leave Me Alone

Slide 28

Slide 28 text

•Flexibility •Less verbose But I Like Dynamic Types Leave Me Alone

Slide 29

Slide 29 text

•Flexibility •Less verbose •Write code faster But I Like Dynamic Types Leave Me Alone

Slide 30

Slide 30 text

How Can You Live without Static Types?

Slide 31

Slide 31 text

How Can You Live without Static Types? •Catch errors before runtime

Slide 32

Slide 32 text

How Can You Live without Static Types? •Catch errors before runtime •Code documentation

Slide 33

Slide 33 text

How Can You Live without Static Types? •Catch errors before runtime •Code documentation •Support for IDEs

Slide 34

Slide 34 text

How Can You Live without Static Types? •Catch errors before runtime •Code documentation •Support for IDEs •(compiler optimisations)

Slide 35

Slide 35 text

How Can You Live without Static Types? •Catch errors before runtime •Code documentation •Support for IDEs •(compiler optimisations)

Slide 36

Slide 36 text

Problems many of us have

Slide 37

Slide 37 text

Problems many of us have • New hires

Slide 38

Slide 38 text

Problems many of us have • New hires • Refactoring

Slide 39

Slide 39 text

Problems many of us have • New hires • Refactoring • Poor documentation

Slide 40

Slide 40 text

Problems many of us have • New hires • Refactoring • Poor documentation • Not enough tests

Slide 41

Slide 41 text

PEP 3107 — Function Annotations
 (since Python 3.0)

Slide 42

Slide 42 text

def do_stuff(a: int, b: int) -> str: ... return something PEP 3107 — Function Annotations
 (since Python 3.0)

Slide 43

Slide 43 text

def do_stuff(a: int, b: int) -> str: ... return something PEP 3107 — Function Annotations
 (since Python 3.0) (annotations are ignored by the interpreter)

Slide 44

Slide 44 text

PEP 484 — Type Hints
 (since Python 3.5)

Slide 45

Slide 45 text

typing module: semantically coherent PEP 484 — Type Hints
 (since Python 3.5)

Slide 46

Slide 46 text

typing module: semantically coherent PEP 484 — Type Hints
 (since Python 3.5) (annotations still ignored by the interpreter)

Slide 47

Slide 47 text

… In Practice?

Slide 48

Slide 48 text

… In Practice?

Slide 49

Slide 49 text

… In Practice? $ pip install mypy

Slide 50

Slide 50 text

… In Practice? $ pip install mypy $ mypy

Slide 51

Slide 51 text

Example

Slide 52

Slide 52 text

Example from typing import List, Dict def do_stuff(a: int) -> Dict: b = [] # type: List[int] for x in range(a): b.append(x) return b

Slide 53

Slide 53 text

Example $ mypy example.py example.py:7: error: Incompatible return value type (got List[int], expected Dict[Any, Any])

Slide 54

Slide 54 text

Gradual Typing

Slide 55

Slide 55 text

Gradual Typing • From dynamic to static overnight?

Slide 56

Slide 56 text

Gradual Typing • From dynamic to static overnight? • Any reduces the friction

Slide 57

Slide 57 text

Gradual Typing • From dynamic to static overnight? • Any reduces the friction • Improving code understanding

Slide 58

Slide 58 text

Supported Types

Slide 59

Slide 59 text

Supported Types • from typing import … • List, Dict, Tuple, … • Iterable, Optional, Union, Any, … • … and more • Built-in types and custom objects

Slide 60

Slide 60 text

Python Requirements

Slide 61

Slide 61 text

Python Requirements • typing: since Python 3.5

Slide 62

Slide 62 text

Python Requirements • typing: since Python 3.5 • mypy runs on Python 3.3+

Slide 63

Slide 63 text

Python Requirements • typing: since Python 3.5 • mypy runs on Python 3.3+ • Using Python 2.7? Annotations in comments

Slide 64

Slide 64 text

When to run it

Slide 65

Slide 65 text

When to run it # pre-flight-checks-in-your-ci-server.sh flake8 myprogram # linter pytest myprogram # unit tests MYPYPATH=./stubs # static analysis mypy myprogram

Slide 66

Slide 66 text

Is it slow?

Slide 67

Slide 67 text

Is it slow? NO* YMMV *

Slide 68

Slide 68 text

Third-party libraries

Slide 69

Slide 69 text

Third-party libraries • Stubs: interface definition in *.pyi

Slide 70

Slide 70 text

Third-party libraries • Stubs: interface definition in *.pyi • mypy --follow-imports silent

Slide 71

Slide 71 text

Third-party libraries • Stubs: interface definition in *.pyi • mypy --follow-imports silent • --follow-imports {normal, skip, error}

Slide 72

Slide 72 text

But… Duck Typing!

Slide 73

Slide 73 text

But… Duck Typing!

Slide 74

Slide 74 text

But… Duck Typing! There is no free lunch

Slide 75

Slide 75 text

Summary

Slide 76

Slide 76 text

Summary • From script to mature codebase • Better understanding of your codebase • Life easier with heterogeneous teams

Slide 77

Slide 77 text

THANK YOU @MarcoBonzanini GitHub.com/bonzanini marcobonzanini.com

Slide 78

Slide 78 text

mypy references • http://www.mypy-lang.org/ • http://mypy.readthedocs.io/ Images: • Rubber ducks: https://en.wikipedia.org/wiki/File:Rubber_ducks.jpg • The Thinker: https://pixabay.com/en/the-thinker-rodin-museum-thinker-1431333/ • Skull and bones: https://commons.wikimedia.org/wiki/File:Skull_and_crossbones.svg • Scrum: https://commons.wikimedia.org/wiki/File:Scrum_Italy_New_Zealand.jpg • Alberto Sordi / spaghetti: https://it.wikipedia.org/wiki/File:Un_americano_a_Roma_-_maccheroni.jpg