Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: Adding type hints
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright. Now, let's just make a copy. So we have this old version without any type hints, and I'll have one with actual hints, types.
0:08 So let's just set that to run. See, it still works, but what I want to do is add some extra information.
0:13 I would like to say that this can be an integer except for, hold on, PyCharm now says there's a problem. You said it was an integer like 1 or 7 or -3,
0:23 that's not what none is. None cannot be interacted with as an integer, right? So when we have a type that is either none or something,
0:31 we have to say this is in optional of that thing. An optional is not built into the language. Some languages have really cool syntax like that to mean
0:40 it could be none, but we don't have that. So we have to import this from typing, okay? So now it lets us either have it as an integer or none,
0:51 right? So now we have described this as either being an integer or being just none. Down here, let's go down,
0:58 it's going to know that this could be an integer because it's set to zero. Probably don't need to do that.
1:02 But remember, we saw this "i." We got some help, but only because we were calling it. Take that away for a second.
1:09 And now if we say "i." again, no help. So the next thing to do is say what goes here. So what we can do is say this is an iterable. Now again,
1:19 this doesn't come up just like optional. We've gotta import that from typing, and that tells us that we can put it into a loop,
1:25 but it still doesn't tell you what it is, so we can say it is an iterable of this item, named tuple, if we say that, check this out.
1:33 Here's our value that we're looking for. Perfect. And if we had other, all of sudden no, no, no, you can't have other.
1:39 There is no other. There's item and there's name. Sorry, there's name and there's value. Again,
1:45 all of this works. We might as well go over here and say this is going to return, I think if we look at the items we create, no,
1:51 no, these are integers. So we can just say this returns and int, alright? Perfect. Perfect. And now if we call it down here,
1:57 nothing. Nothing should be changed. But you can notice that if we were trying to pass in, like here, 7, It'll give you a warning like,
2:06 "hey, 7 is not iterable, we expected something you could loop over, not a 7". Alright, so let's just run it one more time.
2:12 Make sure it still works. It does. But now, not only does it work,
2:17 it works better with the editors because of this kind of stuff and the validation for verifying what we're passing in and what we're getting back.
2:26 Perfect. So this is the idea of type hints. It's super valuable in general, but it is extra useful in FastAPI because not only does it tell you what
2:34 the program does, FastAPI actually takes that and uses it for things like type conversion and whatnot.