Slide 1

Slide 1 text

1 0 0 % o p t i o n a l TYPE HINTS: TAKE IT EASY Introduction, pros and cons of
 Python's gradual type system Luciano Ramalho @ramalhoorg

Slide 2

Slide 2 text

2 https://twitter.com/raymondh/status/1255580152706158592

Slide 3

Slide 3 text

TYPE HINTS? A brief introduction 3

Slide 4

Slide 4 text

STATIC TYPE DECLARATIONS Formalized in PEP 484 — Type Hints (and other PEPs) Supported by the typing module in the standard library Mostly for the use of external type checkers, not runtime 4 def parse_flag(text: str) -> bool: return text.upper() in {'Y', 'YES', 'T', 'TRUE'}

Slide 5

Slide 5 text

ABOUT GRADUAL TYPING Dynamic typing: type checking at runtime only Ex. Lisp, Smalltalk, Python, JavaScript, Ruby, Clojure, Elixir, etc.
 Static typing: type declarations in source code, type checked before runtime by static analysis of the code Ex. C, C++, Pascal, Java, C#, Haskell, Rust, Kotlin, Swift, Elm, etc.
 Gradual typing: hybrid approach—optional static type declaration, no effect during runtime Ex. ActionScript, Hack, Dart, TypeScript, Python with type hints... 5

Slide 6

Slide 6 text

EXAMPLES 6

Slide 7

Slide 7 text

THE FIRST BUG MYPY FOUND FOR ME 7

Slide 8

Slide 8 text

FIXED! random_queens_and_guard.py: https://tgo.li/rnd_queens 8

Slide 9

Slide 9 text

"SUBPASCAL" INTERPRETER Example from Programming Languages Workshop
 at Garoa Hacker Clube https://tgo.li/OfLiPr2018 9

Slide 10

Slide 10 text

SUBPASCAL PARSER 10

Slide 11

Slide 11 text

IMPORTING TYPES FROM TYPING 11

Slide 12

Slide 12 text

DATA CLASSES PEP 557, Python 3.7 12

Slide 13

Slide 13 text

NAMED TUPLE (CLASSIC) 13

Slide 14

Slide 14 text

TYPED NAMED TUPLE 14

Slide 15

Slide 15 text

DATA CLASS 15

Slide 16

Slide 16 text

DATA CLASS WITH MORE FIELDS 16

Slide 17

Slide 17 text

OVERLOADS 17

Slide 18

Slide 18 text

OVERLOAD EXAMPLE #1 18

Slide 19

Slide 19 text

MAX OVERLOAD 19

Slide 20

Slide 20 text

MAX: CURRENT TYPE HINTS ON TYPESHED 20

Slide 21

Slide 21 text

RECENT DEVELOPMENTS 21

Slide 22

Slide 22 text

USABILITY IMPROVEMENTS PEP 585 — Type Hinting Generics In Standard Collections Starting with Python 3.9 (backported to 3.7, 3.8) 
 22 from __future__ import annotations def loads(input: Union[str, bytes], *, encoding: Optional[str] = None) -> dict[str, Any]: ...

Slide 23

Slide 23 text

STATIC DUCK TYPING PEP 544 — Protocols: Structural subtyping (static duck typing) 23

Slide 24

Slide 24 text

STATIC DUCK TYPING PEP 544 — Protocols: Structural subtyping (static duck typing) 24

Slide 25

Slide 25 text

MORE REALISTIC EXAMPLE 25

Slide 26

Slide 26 text

MAX: PROPOSED TYPE HINTS FOR TYPESHED 26

Slide 27

Slide 27 text

MAX: PROPOSED TYPE HINTS FOR TYPESHED 27

Slide 28

Slide 28 text

BENEFITS 28

Slide 29

Slide 29 text

BENEFITS OF TYPE HINTS Document function signatures Support the IDE for better code completion and refactorings Finding bugs before running the program Automating object serialization/desseralization They are optional, avoiding the problem of diminishing returns. 29

Slide 30

Slide 30 text

COST There is no free lunch 30

Slide 31

Slide 31 text

DAVID BEAZLEY IS NOT ON BOARD... https://twitter.com/dabeaz/status/981699763996450817 31

Slide 32

Slide 32 text

A TSUNAMI OF PEPS (OUTATED! 17 PEPS AS OF JUN/2020) 32 Ano PEP Título Python 2006 3107 Function Annotations 3.0 2014 484 Type Hints 3.5P 483 The Theory of Type Hints N/A 2015 482 Literature Overview for Type Hints N/A 2016 526 Syntax for Variable Annotations 3.6 2017 544 Protocols: Structural subtyping (static duck typing) 3.8 557 Data Classes 3.7 560 Core support for typing module and generic types 3.7 561 Distributing and Packaging Type Information 3.7 563 Postponed Evaluation of Annotations 3.7 2018 586 Literal Types 3.8 2019 585 Type Hinting Usability Conventions 3.8D 589 TypedDict: Type Hints for Dicts with a Fixed Set of Keys 3.8 591 Adding a final qualifier to typing 3.8 3.8

Slide 33

Slide 33 text

DOWNSIDES Effort to learn a whole new set of abstractions Generics, variance, structural typing, type variables etc. Effort to write type hints Most of the time is not hard, but it may be hard or even impossible Better support in recent versions of Python But also: possible to use in Python 2.7, easing transition Slight delay in program startup Not important for long running processes 33

Slide 34

Slide 34 text

COST X BENEFIT Benefits grow when... The codebase is large The team is made of professional software developers Bernat Gabor* gives a good rule:
 if you invest in automated tests,
 you should invest in type hints 
 * link at the end 34

Slide 35

Slide 35 text

FAQ Some answers 35

Slide 36

Slide 36 text

WILL PYTHON BECOME A STATICALLY TYPED LANGUAGE? No. Section Non-goals of PEP 484: 36

Slide 37

Slide 37 text

WHAT IS THE RUNTIME EFFECT OF TYPE HINTS? None. The Python interpreter and standard library ignore type hints* Also from PEP 484: 37 * a few exceptions, ex.: dataclasses and functools.singledistpatch at import time

Slide 38

Slide 38 text

SHOULD EVERYONE USE TYPE HINTS? No. Python users are very diverse: engineers, financial analysts, students, scientists and researches from almost every field (physics, biology, sociology, artificial intelligence). The cost-benefit analysis is not the same for all of these groups. To learn how to program or do exploratory programming, type annotations introduce complexity, reduce the flexibility of the code, and do not bring much value. Type hints is most useful in teams of professional developers, with large code bases, used to a more complex tooling (IDEs, linters, automated tests, continuous integration etc.) 38

Slide 39

Slide 39 text

REFERENCES Before reading all those PEPs 39

Slide 40

Slide 40 text

BERNAT GABOR: THE STATE OF TYPE HINTS IN PYTHON https://www.bernat.tech/the-state-of-type-hints-in-python/ 40

Slide 41

Slide 41 text

REAL PYTHON: PYTHON TYPE CHECKING GUIDE https://realpython.com/python-type-checking/ 41

Slide 42

Slide 42 text

Let's keep this conversation going! Twitter: @ramalhoorg E-mail: [email protected] DANKE SCHÖN