Rock Solid Python with Python Typing Transcripts
Chapter: Tools Based on Typing
Lecture: mypy in Action
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Here we are back in our code. And I'm just going to copy in a couple of things here so we don't have to write them because they're not super relevant.
0:09
First of all, you can configure my pie through this ini file, any file, you can set certain settings like this.
0:17
So when you run it, it's going to look at this config file and decide what it should do what it should skip what it shouldn't skip. That's nice.
0:26
We have this basic program here that just does stuff like has a string and works with it. Sometimes incorrectly, sometimes correctly.
0:34
It has a constant under the final category and sometimes it treats it right and sometimes it abuses it by trying to change the constant.
0:43
Notice before I think final wasn't caught as an error in the earlier videos I did, but somehow now I PyCharm updated itself along the way.
0:53
now PyCharm also catches this lack of a constant. I actually submitted this to them as a bug and wow, if they fixed it that quick,
1:01
well done PyCharm team. So really, really cool. But yeah, that's a little bit of a bonus, but let's assume we don't have an editor like this.
1:09
Most importantly, around continuous integration and GitHub actions and those kinds of things, not so much like I just don't want to use this editor,
1:18
but there are really valid locations like CI/CD that you just don't have editors in the mix. So we're gonna use mypy,
1:26
and to do that, we're going to need to install mypy. So mypy, like that. Remember, you don't have to do this step to generate the TXT file,
1:34
you just have to install them. So I'll say pip compile. There we go, requirements, pip tools, upgrade, might as well upgrade all the other things
1:44
if there's like, say, a new beanie that just came out. And here's the thing you gotta do, pip install -r requirements.
1:51
Sure enough, we got our mypy, but Pydantic also got updated and so did FastAPI. Good stuff.
2:03
So now if we go and open this in the terminal and we just type mypy, the very, very first run of it, it's a little bit slow or whatever.
2:17
I think that's just because maybe the Apple Silicon thing. Let's try again. Yeah, notice it's super, super fast here.
2:23
So what we do is we say mypy test, you can hit the T, we're gonna run it against this. You can see that there's three errors that PyCharm knows about.
2:33
List none and str z is a constant. Let's see what we get. Awesome, mypy found all of those problems. So there's an incompatible return type.
2:46
We got none right there on line 10 when we were supposed to be returning list, but we didn't specify, so it could be list of any.
2:54
Right, so the type of error is invalid return value. And sure enough, that's true, and PyCharm is also catching it.
3:00
Down here on line 13, this print a number takes an int, but if you look at what x is, it's a string. So those don't go together, right?
3:08
Here's a little error. And down here it says, print a number has an incompatible type being passed to it, string, where it's supposed to be an integer,
3:18
that's an arg type error. And finally, z, I wish this was phrased differently, z is a constant and should not be changed.
3:27
Well, okay, cannot assign to vinyl name z, that means it's a constant, don't change it. Python, its language doesn't have support
3:37
for a true constant that cannot be changed, but the type checker as well as your editor can. So what you can do is you can set this up
3:46
as one of the steps in your continuous integration, somebody checks in something that has an invalid type. This is kind of like a compile step.
3:55
Now, we also have, we could say like this, we could say def other, go down here and just go other x. If something about this is, let's see,
4:09
like upper, call upper, and put a z here. Wow, look at that, PyCharm did find it. But there's nothing about this description here. Let's see if mypy.
4:22
No, mypy doesn't actually find it. So from a mypy perspective, only when the type information is present does it try to check.
4:30
PyCharm on the other hand is like, Hey, I got your back. This isn't gonna work. I know ints don't have an upper.
4:36
Yeah, so mypy is super easy to use, super easy to set up. I basically pip installed it. I dropped in an ini config file
4:43
so I can control some of the settings if I want. And then I just said, mypy the name, I could do like this, I think.
4:50
Yep, just apply it to the directory here and say, get all of them. I'm sure you can do it to recursively as well.
5:00
So this is a really awesome tool to have for when you're working with things like continuous integration, get pre-commit hooks,
5:07
you name it, or you could even just run it yourself and just use it as a tool that's kind of like PyCharm, But not PyCharm.