Is Python Your TYPE of Programming Language? How to Use Static Typing in Python With Type Hints, MyPy and Pydantic

30 Minute Talk
Saturday at 11:45 am in Orchid Ballroom East

Python's dynamic typing system famously offers flexibility, but this can sometimes lead to runtime errors that are hard to detect or predict. In many cases, the programmer knows what type a variable "should" be, but in earlier language versions the only option to enforce this was by writing custom, run-time type checks. Since Python 3.5, the language has offered type hints, which are optional annotations that suggest (but do not require) that a variable has a particular type. Combined with static type checkers like MyPy and run-time data validation frameworks like Pydantic, type hints offer Python programmers a powerful system to implement static types in a highly standardized way.

This presentation explores these new standards for static typing in Python through the lens of two powerful and versatile libraries: MyPy and Pydantic. These tools build upon Python’s type hints to help you improve your code reliability and effectiveness with minimal extra effort.

At the end of this presentation you will be able to:

  • Use type hints, MyPy, and Pydantic to define and enforce static data types in Python.
  • Identify important use cases where static types provide greater code reliability and quality.
  • Leverage these tools to increase the resilience of your code against bad data, and deliver more useful and actionable error messages sooner.

MyPy is a type checker that leverages built-in type hints to identify possible type errors during a separate static analysis stage. By integrating a MyPy step into your development, testing, and deployment processes, you can catch type-related errors at the start of the development cycle. This reduces debugging time, improves code quality, and often heads off potential production failures long before they occur. We will discuss MyPy's key features, how to integrate it into existing projects, and how it works together with Python's built-in dynamic typing.

Pydantic is a data validation library that leverages Python's type hints to check incoming data at run time. Pydantic's data models ensure that incoming data conforms to defined schemas. This feature is especially useful in data-intensive applications for guaranteeing data integrity and standardizing error reporting. Key application areas include ETL, streaming data, and RESTful APIs (in fact, the popular FastAPI framework leans heavily on Pydantic for data validation). We will learn about Pydantic's applications in data parsing, and in building data models that enhance code resilience and simplify error-checking and logging.

Through reference to practical examples and best practices, this talk will demonstrate how you can use MyPy and Pydantic to leverage the static typing capabilities in the core Python language to create more correct, maintainable, and resilient Python applications.

Presented by

Jack Bennett