Python 3, an Illustrated Tour Transcripts
Chapter: Type Annotations
Lecture: 3rd-party: pyannotate

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at the pyannotate tool, again, this is very similar to monkeytype.
0:05 We run our code and again we might need a driver file, this is not static. In this case, it's going to click information in a Json file
0:11 and then we tell it to annotate our code with our type information, very easy install, I just say pip install pyannotate
0:18 and then I'm going to run a driver file. And then after I've run that driver file, I'm going to say pyannotate - w
0:24 that means write the markov.py file or update it with the type information. Here's my driver file, I'm going to import doc test and markov
0:32 and I'm also going to import the collect types from the pyannotate runtime module and you can see all this is doing is it's saying if I'm running this
0:41 then I'm going to init my types collection and then it's going to have a context manager there
0:46 that's going to run my code inside of the context manager that collects the information and then it's going to dump that information into a Json file.
0:54 Here's a diff of the output that comes out of that. We see that it has imported from typing optional and dict so it's got some imports there
1:02 and we see that it's added some type information here as a comment, so this is the Python 2 style of annotation there
1:09 and those types look fine, it's also done some typing here. So it says that data in is an optional string that looks okay
1:18 and note that get table also looks like it's returning this nested data structure
1:24 of dictionaries, mapping strings to subdictionaries, mapping strings to counts of strings. So all of these types look good.
1:32 They're just in the Python 2 style rather than Python 3, note that we can use those in Python 3 as well.
1:38 Again, pyannotate supports Python 2.7 style annotations as of when this video was recorded.


Talk Python's Mastodon Michael Kennedy's Mastodon