Python 3, an Illustrated Tour Transcripts
Chapter: Type Annotations
Lecture: Walk-through (part 2): Annotation Tooling
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
In this video, we're going to talk about annotate 3rd and the pyannotate section. So we're assuming that you've already done steps 1 through 4 here.
0:09
The first step is to install pyannotate, I've got my virtual env active over here. I'm just going to say pip install pyannotate.
0:20
Okay, the next one is to create a function test_pa that exercises pi3pa.py and sticks the Json output into type_info.json.
0:30
So I'm going to make a file similar to our runmt.py called runpa, touch runpa.py and let's open runpa.py here.
0:42
I'll split my screen here so we can see both of these. Okay, so there's py3pa.py, let's open runpa.py.
0:53
I want this to be similar to runmt, so we'll just copy all this, it's going to be very similar to this, but we're going to change a little bit
1:01
we're going to change the mt to pa.
1:20
Okay, if you go to the pyannotate website, it's got some boilerplate here that we can put, we need to make this little context manager here
1:28
and we need to do our import, so we'll drop in the boiler plate here, and let's do our import from pyannotate import.
1:45
Okay, so we import this collect types guide, we init the collect types and then we make context manager,
1:51
with our context manager we run our code and then we dump our stats here
1:56
and according to here it says that we want to dump it into this guy type_info.json. So we'll do that type_info.json here.
2:11
Okay, so I think we've got that part. We've got the function test_pa, and it exercises our code,
2:21
now let's run this, we're just going to run this with Python and then using pyannotate that should look at this type_info guy here.
2:31
Let's just say Python runpa.py, it doesn't say anything but now it should be a type_info in here, let's just look at type info
2:39
and it looks like it makes sense, when we call character.speed, it should return an int and it had one sample in there, it looks okay.
2:48
Force looks like it took an int and a float and returned a float. So looks like that's doing okay,
2:55
it looks like quad was called with ints and returned floats. So we can call pyannotate, you can just say pyannotate.h
3:04
it's going to give you a little help documentation. Let's say pyannotate and we have py3pa.py and it says I don't have type_info.json, interesting,
3:14
we got to change our name here, type_info.json. Okay, and now if we run it, we get this little guy here
3:34
and it says this is the refactor, this is the diff that it would make and note that it's making Python 2 comments here
3:42
instead of doing the Python 3 annotations. So this is a supported syntax for Python 2 and at the end it says note that this was a dry run
3:52
we can use -w to actually write the file. So let's use -w to write the file here. And if we look at py3pa now,
4:01
he's got the type annotations in the comments here, cool. And it says run mypy against py3pa, let's run mypy against this
4:09
and see what complaints we get here. Okay, no complaints there, we'll just turn on the strict and see if strict complains about anything else.
4:19
So that says we are still missing some stuff, and so this shows the gradual typing of mypy that it only checks what's annotated
4:29
and we could go in and add these other annotations, we saw how to do that previously, but luckily pyannotate gives us a little leg up
4:38
if we've got tests or something that we can run and get that information in there, that will just give us a little head start to get that running.
4:44
Another benefit of this pyannotate code again is that it works in Python 2 & 3.