What are the three solutions mentioned in the chapter for handling data validation and type checking in Python, and which one does the author choose for FastAPI?
The three solutions are dataclasses, attrs, and Pydantic. The author chooses Pydantic for FastAPI because it is integrated into FastAPI and stands out for validation.
The chapter lists three solutions that address data validation and type checking needs: dataclasses (part of standard Python), attrs (a third-party superset of dataclasses), and Pydantic (also third-party, but integrated into FastAPI). The author notes that Pydantic is an easy choice for someone already using FastAPI, that it stands out for validation, and that its integration with FastAPI catches many potential data errors. The author also mentions that Pydantic uses inheritance from BaseModel rather than decorators, which is a stylistic difference. The author's final decision is to use Pydantic in the book, while acknowledging the alternatives might be useful outside FastAPI.
Key points
- The three solutions are dataclasses, attrs, and Pydantic.
- Dataclasses are part of standard Python.
- attrs is third-party and a superset of dataclasses.
- Pydantic is third-party and integrated into FastAPI.
- The author chooses Pydantic because it is well integrated with FastAPI and excels at validation.
Related questions
FastAPI: Modern Python Web Development
Bill Lubanovic;
First Edition · O'Reilly Media, Inc.