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