Python 3, an Illustrated Tour Transcripts
Chapter: Type Annotations
Lecture: 3rd-party: mypy
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's look at mypy a little bit more. I'm going to contrast this with a tool that Google has in a minute here.
0:06
So mypy supports Python 3 style annotations, it also supports Python 2 style annotations and it supports what are called stub files.
0:17
So if you have some code that you need to type check or you want to type check against but you can't change that code,
0:23
such as code in the standard library or whatnot, you can use these stub files, these are pyi files that just have type information in them.
0:30
Mypy can run against all of these. If you want to create a stub file, if you are using a third-party library
0:36
and can't push code back upstream or they don't want to include type information, you can also create stub files as well.
0:43
Mypy ships with a stub gen.py tool to create stub files. And one of the values of having these stub files is
0:50
it gives a little bit more checking to validate that you haven't had typos in what you're calling
0:57
and that the methods and functions that you're calling all exist. Here's an example of running mypy,
1:02
it's pretty easy to install, we just pip install it and then we can say Python -m mypy on what we need to check
1:09
or we can call the mypy executable itself on the file. And again, this is static type checking, it's not going to execute the markov code per se
1:18
but it's going to look at it and try and divine what the types are. And in this case, I'm going to get some errors
1:25
you'll note that the tooling that I just ran previously for pyannotate added some type information to my markov file,
1:33
and now when I run mypy against it, it's going to complain because it's going to start type checking those things.
1:39
So it complained about line 38 and line 57. It says we need type annotations for a variable. These are what those two lines look like.
1:46
We are making the tables attribute in the class instance, and we also have a results attribute this table creation as well.
1:55
So interestingly enough, monkey type and pyannotate, they didn't create the types for these guys internally,
2:04
they created the function and method types, but not the internal types that mypy was looking at in this case.
2:10
Here's an example of removing the errors for the tables guy. I can just do an inline definition here for the type on the variable here.
2:18
It's that nested guy, and if I was being a little bit more user-friendly, I'd probably define this up above as a table result
2:27
and just point this at table result instead, make it a little bit more easy,
2:33
this is a little bit too nested and we're going to use table result in a couple of places in this file, so it makes sense to reuse that code.
2:40
One of the other things you can do is integrate mypy with continuous integration tools so you can run things like --cobertura-xml-report
2:48
and that will give you a line-by-line report on how your type information is. It will give it a ranking of it and you can integrate that
2:57
and if you're interested in tracking these things, this is something that's interesting to you and called quality,
3:03
then you can measure it and keep track of it with your continuous integration. Another nice little feature in mypy is the reveal type function.
3:10
You don't need to import it, but when you're running my type against the module, you can just say reveal type of some variable
3:16
and mypy will try and divine what it is. It does an okay job, sometimes it can't really guess what it is and so it just returns any,
3:23
in this case, I've got a file that takes an integer and a float and it ads those two and it's asking if I add those two,
3:30
what's the type of this res variable, and we know that it's a float if we have used Python for a while, but maybe you don't
3:38
or maybe you've got some other type that you're not quite sure what it is. You run that with mypy and it's going to tell you that this type is a float
3:46
so you can put your type information in there if you want to.