Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Fixing Linting Errors Automatically

00:00 Now that you know how to check if your code has any linting errors, you can also learn how to use Ruff to fix those errors automatically, or at least try to.

00:10 And you will do that with the subcommand ruff check --fix. So the --fix option will try to fix as many linting errors as possible.

00:20 But actually, if you go back to your terminal

00:24 and if you run ruff check without the option, just ruff check one more time. You will see that at the end of the output with the two errors Ruff says that there’s one fixable linting error with the --fix option.

00:39 So the tip was there all along. In order to use Ruff to fix errors, you just run ruff check --fix.

00:48 And once you do that, Ruff will tell you how many errors it fixed and how many errors were left unfixed. And in this case, there’s one error still to be fixed about the fact that you’re using one name on line nine that is undefined.

01:07 So go ahead, open your code. Once you’ve opened the file one_ring.py in your favorite editor, you’ll want to go to the function ring_bearer(), which would be at line 8 right now.

01:20 And you’ll notice how the argument name is missing. So line 9 uses a variable called name assuming it exists, but that’s supposed to be the parameter to the function.

01:32 So you’ll have to update the function ring_bearer() to include the parameter name.

01:38 Make sure you save your code and open your terminal again.

01:43 Once you open your terminal after fixing the error manually, if you run ruff check, you should now see a success message saying all checks passed, which means that Ruff was not able to find any linting errors in your code.

01:59 So that’s the goal usually you’ll be aiming for. You’ll run ruff check, you will probably run ruff check --fix to fix as many errors automatically as possible.

02:11 And then you’ll have to handle a couple of errors by yourself manually.

02:17 Now this process, you can do it whenever you want. You write some code, you add a couple of functions, maybe some statements, you save your code and then you can run ruff check.

02:28 But there’s also a way to have Ruff, continuously watch your code for linting errors, and you’ll learn how to do that in the next lesson.

Become a Member to join the conversation.