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, rather than just analyzing your code, you actually need to run it.
0:06 So you probably need a driver file or some test scripts or something, while it's running, it will drop some information into a sqlite file
0:13 and then with that sqlite file, you can tell it to annotate code with type information.
0:18 We're going to look at the same markov file that we were looking at previously and here for monkeytype, I'm going to make a code runner,
0:25 I'm just going to call it run, it's going to import doc test and it's going to import the markov module that we have
0:32 and it's just going to run the doc tests on the module and hopefully using that information, It will be able to give us some type information.
0:39 Using monkeytype is pretty straightforward. I say pip install monkeytype to get it installed and then I rather than running Python on my code
0:47 I say monkeytype run with the runner file, and in this case again, it's going to run through that doc test
0:54 and trace the code and remember those types and put them in a sqlite file. And then after I've done that, I can run monkeytype stub markov
1:02 that will create the stub information. And so this is what it outputs, it just prints this out to standard out
1:08 and you can see that it has added some types to my functions and to my methods. One thing to note is that it added Python 3 style annotation support
1:19 so this looks pretty good, if you look at the types that it added for example in the git table function,
1:25 you'll note that it added this nested dictionary of strings mapping the dictionaries, of strings mapping the integers, so that's pretty good.
1:33 And it looks like all the type information there is pretty correct and should be good to use.

Talk Python's Mastodon Michael Kennedy's Mastodon