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.
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):
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)
Outline (40 minutes):
Typevarand creating your own types; Annotating classes before they are instantiated; Any type)
Laura Hampton is a Python developer in New York City, where she works at Datadog.