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.
0:01 So we have this old version without any type hints,
0:04 and I'll have one with actual hints, types.
0:07 So let's just set that to run.
0:08 See, it still works, but what I want to do is add some extra information.
0:12 I would like to say that this can be an integer except for, hold on,
0:16 PyCharm now says there's a problem.
0:18 You said it was an integer like 1 or 7 or -3,
0:22 that's not what none is. None cannot be interacted with as an integer, right?
0:27 So when we have a type that is either none or something,
0:30 we have to say this is in optional of that thing.
0:34 An optional is not built into the language.
0:36 Some languages have really cool syntax like that to mean
0:39 it could be none, but we don't have that.
0:41 So we have to import this from typing,
0:45 okay? So now it lets us either have it as an integer or none,
0:50 right? So now we have described this as either being an integer or being just
0:55 none. Down here, let's go down,
0:57 it's going to know that this could be an integer because it's set to zero.
0:59 Probably don't need to do that.
1:01 But remember, we saw this "i." We got some help,
1:05 but only because we were calling it.
1:07 Take that away for a second.
1:08 And now if we say "i." again, no help.
1:11 So the next thing to do is say what goes here.
1:15 So what we can do is say this is an iterable. Now again,
1:18 this doesn't come up just like optional.
1:20 We've gotta import that from typing,
1:21 and that tells us that we can put it into a loop,
1:24 but it still doesn't tell you what it is,
1:26 so we can say it is an iterable of this item, named tuple,
1:30 if we say that, check this out.
1:32 Here's our value that we're looking for. Perfect.
1:34 And if we had other, all of sudden no,
1:36 no, no, you can't have other.
1:38 There is no other. There's item and there's name. Sorry,
1:42 there's name and there's value. Again,
1:44 all of this works. We might as well go over here and say this is going
1:46 to return, I think if we look at the items we create, no,
1:50 no, these are integers. So we can just say this returns and int, alright?
1:53 Perfect. Perfect. And now if we call it down here,
1:56 nothing. Nothing should be changed.
1:59 But you can notice that if we were trying to pass in, like here,
2:03 7, It'll give you a warning like,
2:05 "hey, 7 is not iterable,
2:06 we expected something you could loop over,
2:08 not a 7". Alright, so let's just run it one more time.
2:11 Make sure it still works. It does.
2:14 But now, not only does it work,
2:16 it works better with the editors because of this kind of stuff and the validation for
2:21 verifying what we're passing in and what we're getting back.
2:25 Perfect. So this is the idea of type hints. It's super valuable in general,
2:29 but it is extra useful in FastAPI because not only does it tell you what
2:33 the program does, FastAPI actually takes that and uses it for things like type conversion and whatnot.