A Gradual Introduction to Types
By Laura Hampton

The types of variables provide insight into a program’s behavior and the validity of its results. This talk will cover Python’s resources for validating type. It will discuss using type checking as a debugging tool, how gradual typing can make Python behave like a statically typed language, and cover some edge cases in typing with the mypy library.

Saturday 11:05 a.m.–11:35 a.m.

Long Description:

When I was learning Python, my teacher told me that in order to understand what my code was doing, I needed to know the value and type of every variable. I’ve found it to be valuable advice for debugging and for understanding my code. Python’s type checking tools enable types to be programmatically analyzed before runtime, allowing developers to write code that is easier to reason about and avoids certain types of bugs. Because of this, several large projects have adopted type checking for their Python codebases.

Recent versions of Python allow dynamic and static typing in the same application. In the past, types have been handled by the Python interpreter in an implicit way. Python 3.0 added syntax for annotating types in functions, and this was extended to allow declaring the types of variables in 3.6. (As a side effect, it’s possible to declare a variable without assigning it a value!)

This talk will cover gradual typing and how type annotations can make Python behave like a statically typed language, the benefits and drawbacks of using a type checker, and cover some examples of handling type checking cases using the mypy library. Newer programmers will find that type checking is a useful debugging tool that gives insight into what their code is doing. More experienced programmers will learn more about how type checking is implemented in Python, and will be able to live peacefully with code that behaves as both dynamically and statically typed.

Outline (30 minutes):

  • Dynamic and static typing (4 mins)
  • Gradual typing, and how type declarations can work with dynamic typing and make Python behave more like a statically typed language (6 mins)
  • Python’s tools for type checking - MyPy and the typing library (3 mins)
  • The difference between structural typing and nominal typing (4 mins)
  • Interesting type annotation cases in mypy (8 mins) (Functions that return None; Annotating lists, tuples and other container types; Annotating classes before they are instantiated; Using protocols for structural typing; Any type)
  • When to use a type checker, and what kinds of errors it can catch (and which it can’t ) (5 mins)

Outline (40 minutes):

  • Dynamic and static typing (4 mins)
  • Gradual typing, and how type declarations can work with dynamic typing and make Python behave more like a statically typed language (5 mins)
  • Python’s tools for type checking - MyPy and the typing library ( 3 mins)
  • The difference between structural typing and nominal typing (4 mins)
  • Annotating code with mypy (8 mins) (Functions that return None; Annotating lists, tuples and other container types; Typevar and creating your own types; Annotating classes before they are instantiated; Any type)
  • The type hierarchy and relationships between types (4 mins)
  • Type protocols (how methods implemented on a type affect its behavior) (4 mins)
  • Generic types and type variables (4 mins)
  • When to use a type checker, and what kinds of errors it can catch (and which it can’t) (4 mins)

Laura Hampton

Laura Hampton is a Python developer in New York City, where she works at Datadog.

Twitter

Sponsors