Python 3, an illustrated tour Transcripts
Chapter: Type Annotations
Lecture: Walk-through: Annotations
0:00 In this video we're going to look at annotate_test.py.
0:03 The first thing it says to do is to make a copy of py3code.py to py3code.pyORIG
0:08 So I've got those files in my documents directory.
0:12 I'm going to say copy documents talkpy labs in py3code
0:23 and let's just copy that to py3code.pyORIG
0:26 You can do this through an explorer window if you're on Windows
0:30 or from the terminal if you want to, either way.
0:33 Okay, so there we go.
0:36 Next thing it says is to use virtual environment and pip to install mypy.
0:41 So I'm in my directory where my virtual environment is,
0:45 I've got it activated so I can say pip install mypy here
0:50 and it will go out and fetch it and install it.
0:53 Okay, so now I should have mypy in my path and I can run it.
0:58 Excellent, okay.
1:01 Next thing it says is to run mypy-- strict on py3code, let's try that.
1:06 So I'm going to go into my directory where I have the labs here.
1:14 I'm going to say mypy--strict on py3code,
1:26 mypy--strict on py3code.py.
1:32 Okay, and I get a bunch of things,
1:34 on line 4 we're missing an annotation, line 7 we're missing an annotation,
1:38 on line 10 missing an annotation, line 15 missing an annotation.
1:42 So let's go through and see if we can fix some of these things here.
1:46 Let's open up py3code, line 4 it says we're missing an annotation,
1:50 that's this guy right here.
1:52 So it looks like this is returning an integer,
1:54 so let's add an annotation here to just say return int,
1:57 and let's run it and see if that fixed it.
2:04 Okay, and so now I no longer have that there.
2:16 I'm just going to copy and paste this so I can quickly throw these on here.
2:29 All of these methods here return an integer.
2:32 Okay, let me run mypy again and see what it's complaining about now.
2:37 We're now in line 44, function is missing an annotation on 44.
2:43 So here's 44, this is our force guy and we've got mass and acceleration,
2:47 so I'm just going to say that mass is a float and acceleration is a float
2:52 and those return an int and quad here.
2:59 I'm going to say a is a float and b is a float and c is a float
3:14 and this also returns a float here, and we'll change this guy,
3:19 it should return a float instead of an int.
3:28 Okay, we got on line 52 incompatible return type
3:32 got tuple of float float and not an expected float.
3:35 Interesting, okay on 52 it says that we're returning this guy as a float and this guy's a float.
3:42 So, let's see, we can use PyCharm to fix that for us,
3:50 it has the smarts to fixes this for us.
3:51 Another thing we might want to do is you might want to just say this is a quad result here
3:57 and put that up here and say quad result here
4:09 that way if I'm getting these quad results in other places I can reuse that if I want to.
4:16 Okay, and it says that tuple is not defined, let's define tuple.
4:20 We can probably use PyCharm to do that, so import this name, and it's typing.tuple.
4:30 So if you're not familiar with that, there's the typing library
4:32 and you can import that tuple guy.
4:35 And the nice thing about using the tuple here as we use these square brackets
4:37 and we can say that this is a tuple of floats
4:40 rather than just saying that it is of the tuple class
4:43 we're specifying what is inside the tuple, kind of cool.
4:47 Let's run it again and make sure it works.
4:50 Okay, we're good to go there. Let's go back to our annotate test.
4:55 Okay, run py3code with this super test and keyword test.
5:03 Okay, so let's try and do this here.
5:07 We're going to run py3code with super test and keyword test
5:13 with ignore missing imports, I'll just copy this.
5:21 Okay my copy didn't work, so ignore my copy
5:24 mypy --strict py3code and it says ignore missing imports, so we'll ignore.
5:37 Missing Imports tells it that if you've got import data
5:42 that you don't have type information for it, just ignore that don't complain about it.
5:47 So I wanted this to do super test and keyword only test I believe.
5:54 Okay, and so if we do this, now we're getting function is missing a type annotation there,
6:00 keyword test and super tests are missing type annotations.
6:06 In this case, it's not missing anything extra in py3code.
6:09 So there aren't any other issues.
6:12 We could go through super test and we could add annotations
6:15 to super test if we wanted to here.
6:22 So let's look at super test, on line 3, it says function 3, 31 and 33.
6:28 So here's line 3, we could just say that this returns none here,
6:42 31 def speed this returns an int
6:47 and 33, cannot assign to a method.
6:55 Okay, in that case, it's just complaining, it's just giving us a warning
6:58 it's saying you know what, you're doing some monkey patching here,
7:00 you probably shouldn't do that.
7:07 Okay, 31 is still complaining,
7:11 it's saying I take a self so this would actually be a character,
7:19 let's see if that resolves that issue.
7:23 Okay, and keyword has a couple of things
7:28 and it's actually complaining about the test here.
7:31 So in the keyword test saying too many positional arguments for force and quad,
7:36 that's kind of cool, note that this is our actual test that's calling this.
7:40 If we were using mypy in the continuous integration situation here
7:45 note that we're intentionally calling this in a bad way,
7:48 we would want to probably turn off mypy for this keyword test
7:52 because this is a false positive here.
7:57 Okay, so hopefully you got a little bit of a feel for adding type annotations here.
8:03 It's not too difficult, and we saw that we can do some cool things
8:07 I had erroneously put float here as the return type
8:11 and mypy told me that that's not what it returned, it actually returns a tuple.
8:15 So this is a great feature that's coming out
8:18 and I hope you can take advantage of it to make your code more robust
8:22 or find bugs earlier in your code.