Visual Studio Code for Python Developers Transcripts
Chapter: Editing Python Code with VS Code
Lecture: Linting and Formatting Code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, in the last video, we installed the Python extension, looked at some of the settings for it.
0:05 And in this video, we're going to dive deeper into enhancing these capabilities and what
0:09 this extension itself provides versus what other extensions you might need to enhance the experience even further.
0:16 Now, one thing I really want to dive into in this video is talking about linting and code formatting. Okay.
0:22 What has changed in the origination of this extension was it used to come bundled with a lot of this stuff with a lot of this stuff already in it.
0:33 But when it comes to linting and code formatting, they've taken a step back essentially and
0:38 allow you to take initiative on your own to choose what type of linter or formatter you want to use when it comes to your Python projects.
0:45 So we're going to dive deep into that now, and then we'll get into some of the other capabilities like refactoring in the next video. Okay.
0:52 So first up, let's understand what's the difference between linting and code formatting. Shield your eyes for a second. It's going to get bright. Okay.
0:59 Going directly to the Visual Studio Code website, it talks about this. And in particular, linting is about highlighting things like the syntax and
1:09 issues with problems in your syntax so that it can help you catch errors and issues in your code before having to realize those at runtime.
1:18 So the idea here is while you're writing the code or you're analyzing a PR or
1:24 something like that, you can catch some of these issues around the syntax that's
1:29 being used in the Python project way before and saving you some time way before runtime,
1:34 which would lead you to having to run and debug and change things over time to get to a resolution.
1:40 This enables you to potentially save yourself from that and save that time that you would go through in that developer loop.
1:46 Now, in particular, you can choose from a number of linters that are separate extensions that
1:52 the Python extension will support for you within Visual Studio Code. So some of the ones that Microsoft publishes. Our PyLint, Flake8, and mypy.
2:01 And then some of the community ones are like Ruff and then a community version of mypy.
2:05 If I were to start today from scratch, I'd probably go with Ruff because Ruff seems to
2:11 be, if we open it up really quick, Ruff seems to be an all-in-one type of solution.
2:17 It's a Visual Studio Code extension with support for the Ruff linter. And what's nice about Ruff is it can also format in addition to that, which I'm
2:25 getting a little bit ahead of us here. We'll get into formatting in just a second. But right now, I just want to talk about linting.
2:31 And Ruff seems to be that kind of end-all solution or bundled solution that replaces
2:36 Flake8, Black, iSort, PyUpgrade, all these different individual tools into one tool.
2:41 And it does it super fast because apparently it's using Rust behind the scenes, which is a super fast and lean programming language.
2:49 Getting back to linting. I have already installed PyLint in VS Code.
2:55 And I kind of want to show the difference of what PyLint will show versus what you get out of the box. With the Python extension.
3:00 So if we head on over to the status router.py file, and if I just type in foo.
3:06 And I hover over it, PyLint is telling me that foo is not defined, which is great.
3:12 That's like a nice linting indication to let me know, like, I might have messed up something here.
3:16 I didn't finish typing this out or whatever I may have intended for foo here.
3:21 PyLint is letting me know it doesn't look like you finished what you're trying to do. Now, if I enable PyLint.
3:26 And I go back to that file, you'll notice it's got a different squiggly line under it.
3:33 So it's no longer the orange or yellow squiggly line, which is more of a warning from PyLint. It's red now, which is more of an error.
3:39 And now it's being driven by PyLint. And PyLint is reporting that statement seems to have no effect. It's undefined variable foo.
3:48 And we can see the rule set that's associated with that that PyLint is reporting on.
3:52 This is one way you can view these types of problems, by the way, by hovering over the problem.
3:56 Or you can bring up the problems panel, which a shortcut for that is control or command shift M.
4:02 And now you can see all the problems for this particular file that we have open right now.
4:08 And again, there's PyLint, which is showing here, and the PyLance one. And you see the difference there.
4:14 PyLance is somewhat helpful in this regard, but it's better to have a standalone linter or separate extension that handles linting for you to give you a better experience and better information on potential errors in your code.
4:26 Now, when does linting happen? You might have noticed that once I enabled PyLint, it just started working right away.
4:30 And essentially the way VS Code is set up is that anytime you open a file or save a file, whatever linter that you have defined and in use will analyze that file and lint it for you immediately.
4:36 The last thing I want to call out when it comes to linters is some of them offer what's called code actions.
4:38 In this case, PyLint isn't really offering a code action here, but just to describe what that means, it's a very simple way to use it.
4:40 So you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
4:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
4:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
4:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
4:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:20 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:24 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:28 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:32 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:36 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:40 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
5:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:20 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:24 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:28 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:32 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:36 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:40 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
6:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:20 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:24 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:28 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:32 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:36 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:40 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
7:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:20 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:24 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:28 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:32 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:36 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:40 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
8:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:20 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:24 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:28 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:32 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:36 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:40 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
9:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:20 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:24 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:28 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:32 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:36 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:40 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
10:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:20 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:24 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:28 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:32 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:36 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:40 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
11:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:20 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:24 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:28 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:32 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:36 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:40 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:44 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:48 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:52 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
12:56 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
13:00 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
13:04 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
13:08 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
13:12 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
13:16 And you can see that it's a very simple way to use it. And you can see that it's a very simple way to use it.
13:20 And you can see that it's a very simple way to use it.


Talk Python's Mastodon Michael Kennedy's Mastodon