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

Why You Should Care About Types by Luka Sterbic

Pycon ZA
October 10, 2019

Why You Should Care About Types by Luka Sterbic

By now you have probably all heard about Python static typing. But why should you care? Are types in Python even Pythonic? SPOILER: Guido says so. Is Python turning into Java? In this talk I’ll try to answer these questions and explain how type annotations helped my team scale, made our code more efficient and new hires happier.

We will start with how the typing system in Python came to be, what is the motivation for it and how it looks today. I will demonstrate how to gradually add types to a pre-existing codebase and what benefits will your team get in the long run from a 100% typed project. I will also cover Facebook specific technologies that have now been open sourced like our runtime type collection system, MonkeyType, and the scalable type checker, Pyre!

Pycon ZA

October 10, 2019
Tweet

More Decks by Pycon ZA

Other Decks in Programming

Transcript

  1. None
  2. • Luka Sterbic • Software Eng @ Facebook London •

    https://github.com/sterbic • @lsterbic • ✈ #AvGeek #PointsAndMiles whoami
  3. 1. Why is typing important 2. Typing 101 3. Types

    in the real world 4. Lessons learnt Agenda
  4. Why should I even care about typing?

  5. What jobs?

  6. What jobs?

  7. What jobs?

  8. What jobs? • Grep for it?

  9. What jobs?

  10. What jobs?

  11. What jobs?

  12. What jobs?

  13. • Bootcamp • New hires do tasks for multiple teams

    • Team decision after ~6 weeks • Engineers can pick any team with headcount • eng productive –-≥ likely to join • Applies to Open Source too! Scale my team @FB
  14. Are Python types Pythonic?

  15. • If nothing else, because Guido said so • Are

    Python types Pythonic? Yes! > import this ...
 Explicit is better than implicit. ... Readability counts. ... In the face of ambiguity, refuse the temptation to guess.
  16. • Inspired by types in other languages • Typing is

    still optional • Has no impact at runtime (*) • Typing errors can be ignored Is Python turning into Java? No
  17. Typing 101

  18. • PEP 3107 - Function Annotations (2006) • By Collin

    Winter and Tony Lownds • PEP 484 - Type Hints (2014) • By Guido van Rossum, Jukka Lehtosalo, Łukasz Langa How did it start?
  19. What needs to be annotated?

  20. What needs to be annotated?

  21. • In some cases, variables • What needs to be

    annotated?
  22. • In some cases, variables • What needs to be

    annotated? • For Python ≤ 3.6
  23. Collections

  24. Collections

  25. Collections

  26. Collections

  27. Unions

  28. Unions

  29. Unions

  30. None
  31. TypeVars

  32. The Type Checker $ mypy test.py test.py:4: error: Argument 1

    to "sqrt" has incompatible type "str"; expected "float" • Pyre --≥ https://github.com/facebook/pyre-check
  33. Typing in the real world

  34. 1. 100% typed coverage from Day 1 2. Type checker

    in CI 3. Profit! The Easy Bit New projects
  35. 1. ... first understand gradual typing The Hard Part Existing

    (Large) Projects
  36. utils.py job.py

  37. utils.py job.py

  38. error: Argument 1 to "pretty_print" has incompatible type "str"; expected

    "Job" utils.py job.py
  39. 1. Type common libraries --≥ utils.py 2. Try running the

    type checker 3. Type checker in CI 4. Ask people to add types to new code 5. Use MonkeyType The Hard Part Bit Existing (Large) Projects
  40. 1. Collects types at runtime and logs them 2. Generates

    type stubs 3. Can even apply back type information to your code MonkeyType https://github.com/Instagram/MonkeyType
  41. Stubs

  42. $ pip install MonkeyType $ monkeytype run main.py $ monkeytype

    stub utils def pretty_print(job: str) -> None: ... $ monkeytype apply utils MonkeyType https://github.com/Instagram/MonkeyType
  43. Lessons learnt

  44. Where did the ducks end up? Duck typing in the

    typed world
  45. Protocols error: Argument 1 to "duck_things" has incompatible type "PharaohDuck";

    expected "Duck"
  46. The Typeshed • Typing for the standard library is not

    part of it ?! • Separate repo: https://github.com/python/typeshed • Bundled with your type checker • Still far from perfect --≥ send a PR!
  47. # type: ignore • Silences typing error on one statement

    • Not necessarily evil • https://github.com/python/typeshed/pull/1885
  48. None
  49. None
  50. JSON

  51. NamedTuple

  52. Summary

  53. • Type annotations are Pythonic • Start using them for

    the readability win • Get the extra safety of the type checker for free • The easy bit, new projects should be 100% typed • The hard part, existing large codebases • Start small with gradual typing • Use MonkeyType • Avoid JSON blobs and use NamedTuple instead Summary
  54. Luka Šterbić Software Engineer @ Facebook London luka@fb.com
 github.com/sterbic https://github.com/Sterbic/PyConZA-2019

  55. None