Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: Type hints motivation

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now we have a little bit of code to start from on this one,
0:03 and that's just because there's a bunch of things that's not worth
0:06 you watching me type, but we're just going to extend it with this idea of
0:09 type hints. So over here have a program called "No Types Program".
0:12 That means it has no type hints,
0:14 no type information. None of that.
0:17 And let's look at what it does.
0:18 It's really simple. It has this running maximum of,
0:21 basically it lets you order things off of a menu, like breakfast or dinner or whatever,
0:26 and then it will figure out how much does that cost?
0:28 And it'll actually keep track of what is your most expensive meal that you've had this,
0:32 today. So this is the program that,
0:34 given a set of things from the menu,
0:36 is supposed to, does not yet,
0:37 it's supposed to add up what all the items are.
0:40 And then down here, we're gonna create some items, like we've got this item class,
0:44 item named tuple that can have a name and a price, or name and a value,
0:47 something like that. And then we're gonna create a couple of lists,
0:50 and we're gonna have it to add up that, add up the dinner item, add up
0:54 the breakfast items, and we'll see what the most expensive one was. It's fairly contrived,
0:58 but I just want to show you some of the pieces here.
1:00 So first of all, let's look at this counter.
1:03 When I just look at it, the fact that this is plural maybe tells me potentially
1:08 I could like loop over it.
1:09 I know I'm supposed to add up the prices. We'll do
1:11 "for i in itmes" do something.
1:14 What do I get? What are these items? Now
1:16 something a little bit funky is gonna happen.
1:18 I think PyCharm was going to tell me what I can do if I hit
1:21 "i.", what is available here?
1:24 Check that out. It says it has a value and it has a name,
1:27 and these come from this item.
1:29 Well how the heck did it know that? Normally it wouldn't,
1:32 but because it sees us creating only item lists and passing them over there,
1:36 it's like "ah we're pretty sure this is gonna work".
1:38 But if we happen to have not been using it,
1:41 do this. Now, I come over here and say "i." what do I
1:45 get? Nothing. Absolutely nothing.
1:47 This is all entirely useless and not relevant.
1:50 So how do I know what I can do?
1:52 How do I know I can loop over these things?
1:53 How do I know that I can work with it?
1:55 If the program can't figure out what you're passing through the entire flow or this is
1:59 a library that you've built and you hand over to somebody,
2:02 it's not being used yet, how would the editors be able to help you at
2:05 all? And how would they be certain that that's right.
2:09 Similarly, over here, we've got this max value, running
2:12 max, we want to do some kind of test.
2:14 I mean, it probably looks like this.
2:16 "If total is greater than running max, running max equals total",
2:21 however, this is gonna crash.
2:23 Why? Because you can't compare a number against none,
2:26 so I'll just say, "if not running max, or" like this.
2:30 Okay. So what is this running max supposed to be?
2:32 Should we really set it to, is this an integer? Is it a float?
2:35 I don't know. What we're gonna do is I'm gonna go ahead and just make this
2:38 work here, and then we're gonna improve it by using type annotations.
2:41 So let's go and put this back.
2:43 We could go up and look at our definition.
2:45 Oh, yeah, We have a value here.
2:47 Also be "total += i.value"
2:51 I should add up the prices.
2:53 There's our max. And then if we go and run it, alright,
2:57 give it a shot. Oh,
2:58 look at that. It works great.
2:59 Let's create some items. Dinner was $38.
3:01 Breakfast was $23. Our most expensive
3:04 meal was $38. So this is great.
3:06 The program is fine. There's nothing wrong with the program,
3:08 but looking at it and knowing what to do, well, didn't give us much help.
3:13 And if I get it wrong,
3:14 does it give me any help there?
3:16 Nope. It doesn't say" there is no other",
3:18 It just says "Fine, that'll probably work.
3:20 We really don't know what we're doing,
3:21 we can't help you". It would be nice if the editors and other tools said,
3:24 "no, no, no, there's no other, this is not gonna work.
3:26 I don't know what you're thinking,
3:27 but it's not gonna work", right? So we can add type hits to this program to fix all of these issues.