Python 3, an Illustrated Tour Transcripts
Chapter: Type Annotations
Lecture: Walk-through (part 1): Annotation Tooling

Login or purchase this course to watch this video and the rest of the course contents.
0:01 In this video, we're going to look at So the first thing it says is it says copy py3code.pyORIG to and
0:18 So here, I've got my directory with my files in it and it says copy py3code.pyORIG to and Okay, install monkeytype.
0:35 I'm in my virtual environment, so I just need to say pip install monkeytype here and that should get it. Okay, it looks like it worked.
0:46 Create a function test_mt that exercises in I need to make a, I'm going to say touch,
0:59 run here and we'll split my view here. Okay, so we need to say create a function test_mt,
1:16 and it needs to have this code here in it. So let's plop this code in it. Okay, and it needs to say we need to import this code.
1:35 So we're going to say from import force and quad and Mario. Okay, and let's run test_mt at the bottom here.
1:54 Okay, and it says create a stub for in py3mt.pyi, so we're going to have to use monkeytype to create a stub here.
2:12 So the first thing we need to do is run a Python script under monkeytype tracing. So we're going to say monkeytype run
2:22 and let's run this guy that we just created here, which is called run_mt, I got a syntax error, I better fix that syntax error here.
2:33 Okay, I have now run it and let's see if we can generate a stub here. So let's say stub -h and see what the options are for stub.
2:46 It says generate a stub and a module path and our module is going to be run in py3mt, so stub py3mt, and this is the stub right here
3:02 so I can pipe this into py3mt.pyi and there is my little stub. You'll notice what it did, it imported the tuple class from the typing module,
3:15 and for force, it said that that returns a float and it said the quad returns a tuple of float float,
3:21 character speed returns an int and Mario speed returns an int. So it didn't type all of the guys, but just a few of them, but that's pretty nice.
3:32 But note that it did this not by inspection of the code, but by running the code, so if you'll remember in our runmt, we are calling force,
3:44 we're calling quad and we're calling Mario speed, we're not calling the other methods of Mario. And so that's how it determined what those types are.
3:56 So the next thing it says to do is run mypy against so let's try and run mypy against, we don't get any errors.
4:18 And if we call it with strict then we're going to get a few errors here. Note that if we call strict on pyi, we're going to get on line 4 and line 7
4:32 it says it's missing a function. So let's just look at line 4 and line 7 and see if we can divine what's going on there.
4:41 Okay, line 4 it says it's missing some type information here, so that's probably because we don't have float for mass or float for acceleration.
4:58 Okay, and probably same thing on line 7 here, we don't have type information there either
5:03 so interestingly enough, monkeytype only gave us the return types, it did not give us the input types.
5:10 This video showed how to use monkeytype to create a stub file and do some type checking by running some code and creating that stub file,
5:20 we'll show how to use pyannotate in another video.

Talk Python's Mastodon Michael Kennedy's Mastodon