Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: Concept: Type hints
0:00 Now that you saw how useful type annotations can be, or type hits,
0:03 let's just review the concept of them really quick.
0:05 So here we have a function with no type information.
0:08 We have some running max. It's set to nothing in the beginning.
0:11 What's it supposed to be set to?
0:12 I don't know some larger thing, that could be the largest order,
0:16 that could be the value of the largest order,
0:19 who knows, right? There's no information communicated there.
0:21 Also, here's a counter function.
0:23 It takes something. Based on the name being plural,
0:26 I'm guessing I could loop over those things,
0:28 but I'm not 100% sure. And then there is a total, probably know what to
0:31 do with that, right? So this is not super helpful.
0:33 Adding types lights up a lot of insight into how it's meant to be used.
0:38 So now we have a optional int.
0:40 So it starts out as nothing,
0:41 but it's supposed to be set to an integer.
0:44 And here we have a counter.
0:45 It takes terrible things so I could loop over it. And what's in them?
0:49 It's an item. Because it's an item,
0:51 the editors know that it has a value and a name and it says "we return
0:55 an integer". So just reading this code without doing anything else or seeing how it's
0:58 used, we know much more about what's possible as well as our editors know what
1:03 we should be able to do.
1:04 So if we try to, add a,
1:06 you know, some kind of field lookup or something that doesn't exist,
1:08 while we're looping over the items,
1:09 it will be able to help us and say,
1:11 "No, you're doing that wrong. Its name or value, those are your only two choices". Cool, right?