Save 37% off PRO during our Black Friday Sale! »

Python Type Hints: Take It Easy

Python Type Hints: Take It Easy

The introduction of the type hints supporting Python 2.7 and Python >
3.5 is one of the biggest changes in the history of Python. The best
feature of type hints: they are totally optional. Type hints help
detect bugs and enhance readability—but not always. They also
introduce complexity and make it harder to leverage some of Python's
most powerful dynamic features. The cost-benefit of using type hints
varies from project to project. Sometimes there is value in type
hinting part of a codebase but not all of it. This talk introduces
type hints and discusses the pros and cons of this epic new feature.

27c093d0834208f4712faaaec38c2c5c?s=128

Luciano Ramalho

June 10, 2020
Tweet

Transcript

  1. 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
  2. 2 https://twitter.com/raymondh/status/1255580152706158592

  3. TYPE HINTS? A brief introduction 3

  4. 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'}
  5. 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
  6. EXAMPLES 6

  7. THE FIRST BUG MYPY FOUND FOR ME 7

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

  9. "SUBPASCAL" INTERPRETER Example from Programming Languages Workshop
 at Garoa Hacker

    Clube https://tgo.li/OfLiPr2018 9
  10. SUBPASCAL PARSER 10

  11. IMPORTING TYPES FROM TYPING 11

  12. DATA CLASSES PEP 557, Python 3.7 12

  13. NAMED TUPLE (CLASSIC) 13

  14. TYPED NAMED TUPLE 14

  15. DATA CLASS 15

  16. DATA CLASS WITH MORE FIELDS 16

  17. OVERLOADS 17

  18. OVERLOAD EXAMPLE #1 18

  19. MAX OVERLOAD 19

  20. MAX: CURRENT TYPE HINTS ON TYPESHED 20

  21. RECENT DEVELOPMENTS 21

  22. 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]: ...
  23. STATIC DUCK TYPING PEP 544 — Protocols: Structural subtyping (static

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

    duck typing) 24
  25. MORE REALISTIC EXAMPLE 25

  26. MAX: PROPOSED TYPE HINTS FOR TYPESHED 26

  27. MAX: PROPOSED TYPE HINTS FOR TYPESHED 27

  28. BENEFITS 28

  29. 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
  30. COST There is no free lunch 30

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

  32. 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
  33. 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
  34. 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
  35. FAQ Some answers 35

  36. WILL PYTHON BECOME A STATICALLY TYPED LANGUAGE? No. Section Non-goals

    of PEP 484: 36
  37. 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
  38. 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
  39. REFERENCES Before reading all those PEPs 39

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

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

  42. Let's keep this conversation going! Twitter: @ramalhoorg E-mail: lramalho@thoughtworks.com DANKE

    SCHÖN