Python 3, an Illustrated Tour Transcripts
Chapter: Type Annotations
Lecture: Walk-through: Annotations

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


Talk Python's Mastodon Michael Kennedy's Mastodon