>>> '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