Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Luciano Ramalho

June 10, 2020
Tweet

More Decks by Luciano Ramalho

Other Decks in Programming

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. 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'}
  3. 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
  4. 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]: ...
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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