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

Widening the Linting Scope

00:00 The Python ecosystem has a variety of different linters that have been implemented by different people and that check different things. And one of the goals of Ruff is to essentially replace them all so that there’s a single linter that checks for everything.

00:15 But because so many people already know their favorite linters and the ones they use regularly, you can use the subcommand ruff linter to check for these different linters that Ruff replaces.

00:27 Once you check whether or not Ruff can be used to replace the linters you typically use, you’ll want to use the subcommand ruff check --select to make sure you turn on those linters because by default, Ruff only checks for a limited subset of the errors you could check for.

00:46 And in particular, linting errors that are typically stylistic and that would clash with a formatter, those are off by default, but you can turn them on. So to see this in action, go ahead and open your terminal.

01:02 And once you open your terminal, you can run the subcommand ruff linter, and this will flood your screen with a very long list of linters that Ruff can be used to replace.

01:12 If you’re in a Unix system, you can run ruff linter and then the vertical bar pipe wc -l, and this will count the lines of the output.

01:24 And you can see that there’s 59 lines in the output, which means Ruff implements 59 different linters, but only a small subset is used by default when you run ruff check. The output of ruff linter has two columns.

01:40 On the left, you see the short code for that linter, and on the right you see the full name of the linter. So you might see things here that look familiar like Pylint and Pyflakes and pycodestyle, and pep8 is mentioned there, isort, flake8 has a whole section.

02:02 Once you go through this output and you find the linter you want to turn on, you just take a look at its code. For example, pycodestyle has the codes E and W.

02:16 So let’s take a look at the code E to see what it does. So now what you can do is you can run ruff check --select, and now you specify the letter E.

02:27 So this will tell Ruff to check your code also using pycodestyle’s E rules. If you press Enter, you now get a new error that says that your code has a line that is too long and no, you’re not going crazy. If you just run ruff check, Ruff tells you that all checks passed because, again, Ruff doesn’t check everything by default.

02:50 Once you selected the E rules from pycodestyle, you got the new error.

02:56 And if you already knew that the line too long error was error E501 and you wanted to check for that thing specifically, you could have written ruff check --select E501.

03:11 So that will check for that rule specifically. Now let’s run ruff linter again because there’s one other linter I want to show you for this particular example.

03:22 At the very top of the screen, you can see the code Q for flake8-quotes, and if you run the command ruff check --select E,Q, you are telling Ruff to check using the rules it uses by default plus the rules E plus the rules Q.

03:42 So if you run this now, you get two errors, the line too long error, but also an error related to quotes.

03:50 And the thing is, at the bottom of the output, Ruff tells you that you have two errors and that one of them is fixable with a --fix option that you already saw.

04:00 But you can actually fix all of these errors because these are stylistic errors if you use Ruff to format your code, which is what you will learn to do in the next lesson.

Become a Member to join the conversation.