Introducing Pydantic
00:00 In the previous lesson, I showed you a “Hello, world!” FastAPI app. In this lesson, I’m going to introduce you to Pydantic, a data validation library used by FastAPI.
00:10 Pydantic is a third-party data validation library. It is its own thing. It isn’t part of FastAPI, but FastAPI does integrate with it closely. Pydantic allows you to declare classes with attributes using data type notation.
00:26 If you’re familiar with Python’s dataclasses, it’s kind of like those. It’s quite a bit more powerful though, and it predates dataclasses, if you’re wondering why they both exist.
00:36
When you declare a Pydantic class, you use type hints on its attributes. So if I had a class called Person, I could say that the .name attribute has to be a string and that the .age attribute has to be an integer.
00:48 If you try to instantiate the class with the wrong types, Pydantic throws an error. In fact, this is why FastAPI uses it. Your API is going to be sending data back and forth, and people calling your API could send you all sorts of stuff. As the message body of HTTP is only text, you need to do conversion between text and numbers, and you need to know when you need a number or something else.
01:12 Pydantic takes care of all of that for you. Let’s head into the REPL and write a simple Pydantic class so you can see how it works.
01:21
To define a Pydantic class, you need to inherit from the BaseModel class. So first, let me import that.
01:30 Let’s create a class that stores information about a book. In fact, this is the first step in your FastAPI server journey. All the following lessons in this course will be implementing an API for a digital library.
01:45
My Book class inherits from BaseModel, and it has three attributes: a title,
01:53
an author, and a page count. The title’s a string, the author’s also a string, and the page count is an integer. Now that I’ve got the class, let me instantiate it.
02:05 By default, all the attributes are required, so I’ll need all of them when I construct a new object.
02:13 You use keyword args to specify the values of each of the attributes. In fact, you have to. This is something I mess up all the time. I frequently call the constructor, passing in just the values in the order of the declaration, and that doesn’t work.
02:27 So don’t be like me. Make sure you use the keyword args. Let’s look at our scary clown. And like any Python object, you can get at the attributes directly.
02:37 One of the most common data formats for web APIs is JSON and dictionaries. So you’re going to be dealing with those a lot. FastAPI takes care of most of this for you, but sometimes you’ll have a dict-based version before you have the Pydantic one.
02:52 Remember, keyword args for a callable can be populated with a dict using the double-star notation like this.
03:07 In case you haven’t seen it before, the double-star notation tells Python to turn the dict into arguments for a callable, which in this case is the keyword arguments for the constructor, resulting in another book, a scary fish instead of a scary clown.
03:23 And just to prove that Pydantic is a validation library, let me try to do something that isn’t valid.
03:33
The pages attribute is required. Since I didn’t include it, Pydantic threw a validation error telling me that the pages field was missing.
03:42 You also get an error if the types don’t match.
03:51
Here, when I tried to use a string for pages, it told me that it had to be an integer. Pydantic is pretty powerful and has lots of other features.
04:00 In fact, it’s starting to become a common way of doing configuration files as well. What I’ve shown you is just a little taste, but it’s enough for our FastAPI app.
04:11 Before starting that app, though, a little aside about HTTP methods and my favorite software acronym, CRUD.
Become a Member to join the conversation.
