Rock Solid Python with Python Typing Transcripts
Chapter: Tools Based on Typing
Lecture: Getting Started with Beartype

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In order to use bear type, we're going to need to have it installed in our environment, aren't we? And so I'm going to go through this.
0:08 And we'll add it. Let's add it down here in bear type. Now again, once again, you do not need to run the pip tools command.
0:15 Just pip install -r requirements one time because it's going to be all preloaded when you get started.
0:24 I passed the update flag. So it's possible it's going to make some updates as well. But the most important thing is in here we have bare type.
0:33 And we're going to pip install. Our bare type, excellent. So over here I wanna do two things for examples for working with bare type.
0:45 I wanna do first just a simple introduction of how does it work? What kind of information will it catch? What won't it catch?
0:52 And then I wanna address a really big issue that many of you probably are already already considering is, what about the speed?
1:00 Sure, it's cool to verify all of our code is hanging together, but if it's really slow, then we don't want it.
1:07 So we're gonna come back and we're gonna do some timed type of, not quite profiling, because that somewhat affects the behavior,
1:14 but some timing of some code that we'll write here. So simple one first, and then one that addresses performance separately.
1:21 So let's call this runtime example and we'll say from bear type, import bear type. One is the module, the other one is a decorator.
1:39 So let's just do the main thing here, we'll say def main. And let's write a really, really simple function just so you can see what this is going to
1:48 be like. to put any type hints on the beginning part here. So we'll say equals input, enter the first number,
1:57 second number, and then let's just do some cool math here. Let's go and say...
2:16 Now we got to write our little math on numbers function called this. And this one we're gonna specify that it takes, it's specified takes an int,
2:28 but remember our number tower, let's say it could take an int or a float. So we're gonna do that. And then what is the, this fancy formula?
2:37 Let's go X times X plus three Y. That's good. Now, in order to do runtime, oh, that's supposed to be a Y, in order to do runtime checking,
2:53 all we have to do, go over here and say @BearType, like this. Now, PyCharm is helping us out of here.
3:03 It's like, ""Ugh, input actually returns a string, not a number. I mean, yes, you asked for a number, but that's not really a number.
3:13 But imagine, imagine we didn't know. Let's suppose even we did this and we said, you know what, that is a number. Good job, Michael.
3:21 And then we came over here and said, this is a region, hide this, -n region, like that. Oh, even that has got a little mark on it.
3:35 Let's just say you can't see it, right? Like it looks okay, right? From this point onward, it looks okay.
3:43 And down here, this, sure, you say it takes a float. Everything's good, so you should be able to do these mathematical operations on a float.
3:50 Yes, indeed. But is it really gonna hold together? Hint, no. Let's run it and find out. The first number is 42, and the second one is 100.
4:05 Look at this, whack. We have the bear type, the bear has roared, given us a bare type call hint parameter violation, math on numbers, this dunder main,
4:18 that just means it's really runtime_example, but it's just like when you run it, right? That's all in this convention right there.
4:26 The parameter x equals quote 42 violates the thing that it should be a float because string 42 is not an instance of float. Runtime, folks, runtime.
4:40 How cool is that? So, you know, how do we fix this? Well, we just go and do some sweet parsing up here. Run it again.
4:51 Don't really need those anymore, but run it again. Do what I tried before in 100. The result is 2,064.0. Excellent, right? Excellent.
5:09 So here we have it. Pretty awesome. It really only works on fundamental types or it doesn't really work on super nested things
5:17 like a list of set of float. It doesn't do that verification. Just looks more at the top level types. Let's do one more thing real quick here.
5:27 We'll say math on points. Not super interesting because it's a one-dimensional point, but we'll call this x.value just to
5:49 see whether it'll check it. Up here, I'll do one more. So P1 equals point of X. P2 equals.
6:06 And let's try it again. We'll say 1 and 2. Awesome. That works. Of course it runs. But what if we try to put a Y here? 1, 2. Excellent.
6:18 Excellent. So you can see even for our own types here, it's saying, ""Look, the float
6:24 value 2.0 is not an instance of the point class."" But what it doesn't do is it doesn't
6:30 look within collections. So like I said, I have a dictionary that has a list of things
6:35 and you somehow express that in typing, it's not going to verify that. But it does check, you know, top level types like this, and it's awesome.


Talk Python's Mastodon Michael Kennedy's Mastodon