Configuring Ruff
00:00 As you use Ruff more and more, you might want to configure Ruff to use different values as the defaults, and you can do that through a TOML file. So Ruff can be configured through a TOML file.
00:13
Ideally, if you are in a Python project, you’ll add your Ruff configuration to the pyproject
toml
file, in case that file is available, in case you’re using that file.
00:23
But if you’re not, or if for some reason you don’t want your Ruff configuration to go in the pyproject.toml
file, Ruff can also be configured through a ruff.toml
file or through a .ruff.toml
file.
00:39 Ruff has a couple of different configuration options that you can check in the Ruff documentation.
00:46 Now you’ll see a small example of what a Ruff configuration file might look like. So go ahead and open your terminal,
00:56
create your file ruff.toml
.
01:01
Now open your code editor and once you’re in your editor, make sure you open this new empty ruff.toml
file. Now again, all of the Ruff configuration options are available for you to check on the Ruff documentation.
01:17
But something you could do, for example, is say that the line length, you want it to be 81 instead of the default 88. And that for the linting, so in the section [lint]
, you want to select a couple more linters by default.
01:35 So to go in line with what you did in the previous lessons, you might want to also check for the E and Q rule sets, which you specify in a comma-separated list of strings.
01:47
So you will use the letters from the formatters and you put the letters between quotes. And now if you save your file ruff
.toml
, Ruff will pick up the settings from this file.
01:59
Now in order to prove that this is in fact happening, go ahead and open the one_ring.py
file,
02:06
and take a look at the global variable CHARACTERS
. Now all of the different names are spread across multiple lines, so go ahead and make sure they’re in a single long line again.
02:23 So you modify your code, take your time, and save your file. Now if you open your terminal,
02:32
if you run ruff check
without anything else, you should see the line too long error because although Ruff doesn’t check for long lines, it’s picking up your configuration file.
02:44
So not only it’s flagging the rule E501
because you selected the rules with code E, but it’s also telling you that your line is too long because it’s longer than 81 characters, which is the value you specify in your configuration file.
03:02 So this is how you would configure Ruff through a TOML file.
03:08 You are almost done. In the next lesson, I just want to share with you a couple more resources in case you want to explore further, and that will be it.
Become a Member to join the conversation.