Python 3, an illustrated tour Transcripts
Chapter: Type Annotations
Lecture: 3rd-party: pyannotate
0:00 Let's look at the pyannotate tool,
0:02 again, this is very similar to monkeytype.
0:04 We run our code and again we might need a driver file, this is not static.
0:08 In this case, it's going to click information in a Json file
0:10 and then we tell it to annotate our code with our type information,
0:14 very easy install, I just say pip install pyannotate
0:17 and then I'm going to run a driver file.
0:19 And then after I've run that driver file, I'm going to say pyannotate - w
0:23 that means write the markov.py file or update it with the type information.
0:28 Here's my driver file, I'm going to import doc test and markov
0:31 and I'm also going to import the collect types from the pyannotate runtime module
0:36 and you can see all this is doing is it's saying if I'm running this
0:40 then I'm going to init my types collection
0:43 and then it's going to have a context manager there
0:45 that's going to run my code inside of the context manager
0:48 that collects the information
0:50 and then it's going to dump that information into a Json file.
0:53 Here's a diff of the output that comes out of that.
0:56 We see that it has imported from typing optional and dict
0:59 so it's got some imports there
1:01 and we see that it's added some type information here as a comment,
1:04 so this is the Python 2 style of annotation there
1:08 and those types look fine, it's also done some typing here.
1:13 So it says that data in is an optional string that looks okay
1:17 and note that get table also looks like it's returning this nested data structure
1:23 of dictionaries, mapping strings to subdictionaries, mapping strings to counts of strings.
1:29 So all of these types look good.
1:31 They're just in the Python 2 style rather than Python 3,
1:34 note that we can use those in Python 3 as well.
1:37 Again, pyannotate supports Python 2.7 style annotations
1:41 as of when this video was recorded.