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