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

Talk Python's Mastodon Michael Kennedy's Mastodon