Python 3, an illustrated tour Transcripts
Chapter: Type Annotations
Lecture: 3rd-party: monkeytype

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In order to use the monkeytype tool,
0:02 rather than just analyzing your code, you actually need to run it.
0:05 So you probably need a driver file or some test scripts or something,
0:08 while it's running, it will drop some information into a sqlite file
0:12 and then with that sqlite file, you can tell it to annotate code with type information.
0:17 We're going to look at the same markov file that we were looking at previously
0:21 and here for monkeytype, I'm going to make a code runner,
0:24 I'm just going to call it run, it's going to import doc test
0:28 and it's going to import the markov module that we have
0:31 and it's just going to run the doc tests on the module
0:33 and hopefully using that information,
0:35 It will be able to give us some type information.
0:38 Using monkeytype is pretty straightforward.
0:40 I say pip install monkeytype to get it installed
0:43 and then I rather than running Python on my code
0:46 I say monkeytype run with the runner file,
0:49 and in this case again, it's going to run through that doc test
0:53 and trace the code and remember those types and put them in a sqlite file.
0:58 And then after I've done that, I can run monkeytype stub markov
1:01 that will create the stub information.
1:04 And so this is what it outputs, it just prints this out to standard out
1:07 and you can see that it has added some types
1:11 to my functions and to my methods.
1:14 One thing to note is that it added Python 3 style annotation support
1:18 so this looks pretty good, if you look at the types that it added
1:22 for example in the git table function,
1:24 you'll note that it added this nested dictionary of strings mapping the dictionaries,
1:29 of strings mapping the integers, so that's pretty good.
1:32 And it looks like all the type information there
1:35 is pretty correct and should be good to use.