Chapter 5. The Data Parts: Type Hints and Pydantic

Data validation and settings management using Python type hints.

Fast and extensible, pydantic plays nicely with your linters/IDE/brain. Define how data should be in pure, canonical Python 3.6+; validate it with pydantic.

Samuel Colvin

Preview

FastAPI stands largely on a Python package called Pydantic. This uses models (Python object classes) to define data structures. These are heavily used in FastAPI applications, and are a real advantage when writing larger applications.

Type Hinting

It’s time to learn a little more about Python type hints.

Chapter 2 mentioned that, in many computer languages, a variable points directly to a value in memory. This requires the programmer to declare its type, so the size and bits of the value can be determined. In Python, variables are just names associated with objects, and it’s the objects that have types.

In normal programming, a variable ...

Get FastAPI now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.