Rock Solid Python with Python Typing Transcripts
Chapter: Tools Based on Typing
Lecture: Full Project Inspection

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here we are in our source code from what we've got so far. Notice I have a tooling chapter that we're going to be working with.
0:08 And I'll go ahead and mark that as sources root here. And we're going to be working with this. We're not doing it yet, but that's to come.
0:17 The first thing we want to do is just inspect this. And now keep in mind, some of these may have errors. I put in here on purpose to show you
0:24 here's a thing where the error is being caught. So if you see errors, that doesn't mean something's wrong,
0:28 But the goal is to say, I want to, in this editor, we've got many, many files. In real projects, you've got hundreds of files,
0:36 hundreds of Python files that are interconnected through implicit links via imports and that kind of thing. We're going to use PyCharm to say,
0:48 okay, tell me about that. And notice here, we've got like our exercises and potential solutions and stuff like that.
0:56 So I'm just gonna run it on this folder, but you could run it just one step higher if you'd like. So what you can do is you can right click over here
1:04 and you can say inspect code, or you can hit the hotkey to do that if you like, but I'll just say inspect code.
1:11 It says, like I said, you can do a whole project. Just because this is kind of weird, I'm gonna do just the code project.
1:17 And if I hit this, it's gonna tell me all the errors no matter what, like even PEP 8 violations and the line's too long.
1:24 And sure, you may care about that again, but with regard to typing, you don't. Okay, so let's configure this.
1:31 Come over here and say, we can configure our inspections. So notice we have our default, right? You don't wanna, don't mess with your default, okay?
1:42 So what I wanna do is I want to go and say I want to duplicate this. First of, yeah, we'll say duplicate.
1:51 And notice it's being stored in the project, not in the IDE. The ID is a machine setting. The project is something that could potentially
1:59 be committed back to GitHub. So I'll call this project type checking only. Now that's not true because it's checking CSS stuff and so on.
2:12 So let's collapse these like this and turn a bunch of these off. And this is why you don't want to do this on the default one. So everything is off.
2:24 And now what I'm gonna do is I'm gonna go to Python. Well, first I'm gonna search for type. Now this is a bit of a heuristic.
2:30 I think what you should really do, you wanna be very serious about that, is go through and read each one of these here and say,
2:38 okay, does that seem like something that applies to typing? But I'm gonna take a shortcut here and just say type, and we'll go down to the Python one.
2:46 Now we have these. Now let's see if this will work. If I check that, and then I clear it, We have all the Python stuff selected.
2:55 Now it worked perfectly. So look, here we've got incorrect CLI syntax, incompatible stub, that's typed, incorrect type, sure, that's a good one.
3:05 Invalid definition of typing named tuple, we'll go and throw that in there. We've got protocol usages,
3:12 haven't talked about protocols yet, coming up soon. Invalid use of class vars. This, I mean, this kind of looks like type,
3:19 as far as kind of a type thing. But even if you just left it with just what you got from the types, I think that's good.
3:30 So these are all looking pretty good to me. So I'm just gonna go with that and leave it. I'll hit OK.
3:38 Now notice we're going to do type checking only, which is stored in the project against the directory code and all the sub directory things. Hit it.
3:49 So awesome. Here's the report section. We got 20 things going on. Let's look at this one. Remember I told you some are supposed to be errors
3:58 and notice right here that it says error in editor right there. Awesome, right? It is an error. The editor does say it's an error.
4:07 If I hover over it, does it tell me? Yes, expected and got a string. But I didn't have that file up and I have zero files open.
4:13 It's just one of many files and it went through and inspected all of them. So here's another one. Many of these are in this P1 variables
4:21 'cause I was showing you like that's supposed to be an error, another error, right below another error, right?
4:27 I told you those are like there to kind of highlight that the error is being caught. All right, so close that up. And what about collections?
4:34 Again, error, wrong key type, error, not a string. This is an error 'cause it's supposed to be a string and it's not, same thing there.
4:45 And you can come over here, you can even suppress, you know, suppress these with, as you could normally do like up here,
4:52 I can hit enter and say, suppress first statement. And it'll just add a comment that'll tell the type checker to not do that.
4:59 So you've seen already that when you have a file open, we get these types of things that are awesome, but it's super common.
5:07 You open up the project for your day. It's got 500 files in it, Python files, perfect. One of your colleagues makes a change.
5:17 They're using Vim, they don't have a type checker, they don't care about this stuff, they just check it in.
5:23 You don't even open that file, you're carrying on. What do you do? How do you know that there might be a problem that was introduced?
5:30 You certainly don't wanna open up all these files. So here you go. Inspect code, type checking only against the relevant section, go.
5:38 And then it's off to the races. So editors, editors are awesome. And this total project inspection,
5:47 It's like kind of a next level thing that you should definitely know about.
5:49 If it's not something I run frequently, but every now and then I'll be like, well, how's the whole thing look. Right.
5:56 But usually, usually I'm just getting it as I go. Cause I'm not writing too much new stuff or not, not opening it.
6:03 If it is new from some other place, but this is a really cool feature in a way to take your type checking to the next level in your editors.


Talk Python's Mastodon Michael Kennedy's Mastodon