Loading video player…

Linting Your Code

00:00 Now that you’ve installed Ruff, you are ready to start using it to check your code for linting errors. And you will do that with a subcommand. ruff check. Not necessarily often, but it might happen that you will lint your code and Ruff reports a linting error that you’ve never seen before and that you don’t really understand.

00:18 And so you will also learn how to use the subcommand ruff rule to get more information about that error. And you’ll see everything in action in just a second.

00:27 You just need some code to work with, right? So go ahead and download the file one_ring.py. From the course materials you should be able to download this file and move it to a directory where you’re comfortable working in.

00:44 Once you do that, let’s just take a quick look at what the file looks like.

00:49 So the file one_ring.py should have roughly 20 lines of Python code. Each should have a global variable called CHARACTERS, which is very long, doesn’t fit on the screen.

01:01 It has a couple of functions and then a couple of if statements at the bottom of the file. And this is the code you’re going to be working with. So the first thing you’ll do is open your terminal and make sure you’ve navigated to the directory where the file one_ring.py is. Right now, if you use the command ls, you can see that you are in the correct directory.

01:25 And once you’re there, you can run the subcommand ruff check to check if there are any errors in your code.

01:32 And once you run ruff check, you get two different errors on the screen. Now when Ruff presents an error, the error will start with an error code, which is typically one or a couple of letters followed by some numbers.

01:49 And then one line, a short sentence describing the error. Ruff will also show you the context, the file, and the line where the error is found and also the surrounding code.

02:02 And sometimes it’ll also show you some help text, giving hints or suggestions as to what you could do to fix the error.

02:10 Now the thing is, sometimes these errors aren’t very easy to understand in the first place. And when that’s the case, you can use the subcommand ruff rule to get more information about that error.

02:22 For example, if you look at the second error, it has the error code F821. So what you can do is you can type the subcommand ruff rule and then F821.

02:35 And if you press Enter, you get some Markdown text describing the error that you got, what the error does, how to fix it, or some examples of how you can fix the error, options that might be relevant, references in case you want to learn more.

02:53 So Ruff will give you a lot of information about different errors, and this is very helpful if you get an error that you’ve never seen, or an error that you seldom see and that you don’t really remember what it means.

03:07 Now when you check your code, you run the subcommand ruff check. And what this subcommand does by default is go through all files in the current directory, traverse the directory recursively, and check every single file.

03:21 But it can also provide a subfolder or a specific file to check. So if you ran ruff check one_ring.py,

03:30 you check explicitly only the file that you specify. So that’s very helpful. And another thing that might be of interest is that the subcommand ruff rule accepts an option -- all, which lets you take a look at all errors that Ruff could possibly report.

03:51 You can’t see it on the screen because it was a lot of text and it scrolled past really, really fast. But this is a lot of output. It’s not convenient to scroll up to the top to find the beginning.

04:01 You might not even have enough history length to be able to scroll to the beginning. So one thing you might want to do is run ruff rule -- all and then pipe, so the vertical bar character, and pipe that output to the command less, which will let you page through the output of all of the rules, and therefore making it easier to take a look at all of the rules if you wanted to just read about them from start to finish.

04:29 So you can use the up and down arrows to go up and down the output. And when you want to quit, just press Q.

04:36 Now this is just a curiosity. It’s unlikely that you’ll want to read the whole document from start to finish, but at least you know the information is there.

04:44 If the internet goes down, you can still access the documentation about each rule that Ruff can check for. And now that you know how to check for linting issues, it’s time to learn how to fix them with Ruff.

04:59 And that’s what you’ll do in the next lesson.

Become a Member to join the conversation.