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

Type Hints: Adding Types To Legacy Codebases

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Type Hints: Adding Types To Legacy Codebases

Avatar for Iyanuoluwa Ajao

Iyanuoluwa Ajao

September 12, 2018

More Decks by Iyanuoluwa Ajao

Other Decks in Programming

Transcript

  1. Who is this Guy?  My name is Iyanuoluwa Ajao.

     NYSC member @ Delta State  Graduated from Physiology dept., University of Ibadan  Technical Writer @ Twilio  Remote Developer @ TeapotNG  Twitter: @IyanuAshiri  GitHub: IyanuAshiri
  2. Brief Background  Python is an interpreted and dynamic language.

     Dynamic?  Interpreted?  Python 3.6 introduced Type Hints with the typing module. What is Type Hints? 
  3. Difference Between Static Typing And Type Hints  Types are

    enforced at compile time.  Special tools aren’t required to take full advantage of typing.  E.g. Java, C#, Golang  Types are not enforced even at runtime.  Special tools are required. E.g mypy, pyre  E.g. Python 
  4. Advantages of Adding Types  It enables the detection of

    many programming errors quickly, reliably and automatically.  It provides ready-made documentation.  It eliminates an entire category of bugs. Type errors at runtime are the most common errors encountered by Python developers.  It reduces the number of unit tests.
  5. Common Opinions from Pythonistas  You don’t need types, you

    can always use docstrings and comments.  You don’t need types if you already have unit tests.
  6. Complex data types from typing import List numbers: List[int] =

    list() numbers.append(‘iyanu’) # mypy will complain # it will run because it is type hints not static typing
  7. Complex data type / Creating aliases from typing import Dict,

    List list_of_dicts: List[Dict[str, int ]] = [{“Iyanu”: 10, “Ajao”: 12}] ListOfDict = List[Dict[str, int]] list_of_dicts: ListOfDict = [{“Iyanu”: 10, “Ajao”: 12}]
  8. Explaining The Advantages of Types def multiply(num1: int, num2: int)

    - > int: return num1*num2  Ready-made documentation
  9. Reliable detection of errors (b) mypy multiply.py # errors are

    outputted by mypy but you can choose to ignore. Type hints not enforced
  10. First Limitation from typing import List numbers: List[int] = [1,

    2] numbers.append(‘iyanu’) # mypy will complain # List can contain heterogenous objects but mypy doesn’t know that
  11. First Workaround from typing import List, Union numbers: List[Union[str, int]]

    = [1, 2] numbers.append(‘iyanu’) # mypy won’t complain 
  12. Second Workaround (a) from typing import Union def get_id() -

    > Union[int, None]: pass def send_email(id: int): pass
  13. Second Workaround (b) from typing import Optional def get_id() -

    > Optional[int]: pass def send_email(id; int): pass
  14. Gradual Typing  Gradual typing means you can type check

    your programs even if it not all parts of the program have type hints.  It is a way to incrementally add types to our codebase.
  15.  In Python, only functions with type hints are checked.

    Hence we can gradually add types to our codebase and parts that don’t yet have type hints aren’t affected.  The type checker doesn’t bother checking functions with no types.  It is best to use the type checker in your continuous integration.
  16. Automated Gradual Typing  MonkeyType – a tool developed at

    Instagram to add type hints to your code. It collects runtime types of function arguments and return values. add.py def add(num1, num2): return num1 + num2 new_file.py add(1, 2)
  17. $ monkeytype run new_file.py # this dumps calltraces into a

    file called monkeytype.sqlite $ monkeytype stub add.py $ monkeytype apply add.py
  18. Tools Required For Typing  Mypy  Pyre  Pycharm

    IDE has a built-in real-time type checker  MonkeyType