Modern Python Projects Transcripts
Chapter: Writing code
Lecture: Black
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The best way to apply PEP 8 rules to your code is to use an automatic tool. There is no point in manually adjusting the indentation. If you're code
0:09
editor can do this automatically. One of the most popular tools that can format your code is called Black. Black is very simple to use.
0:17
It takes your files and formats them according to PEP 8 and pep257. It also has some additional rules. On top of that,
0:24
for example, it will convert all single quotes to double quotes. We can see black in action here, on the left side,
0:31
we have an unformatted code, on the right side We have code formatted with black. As you can see, Black is mostly fixing the indentation.
0:38
But for example, it's also removing the backslashes, when they are not needed.
0:42
It's adding the correct number of white spaces for inline comments and so on. And if you want to preserve the custom formatting of some code,
0:51
you can put a comment above this line saying fmt: off and black will leave alone the next line. Let me show you how to set up black VSCode
1:02
So, When you open the command palette, you can search for format document. If you select format document with, you can choose the formatter.
1:10
Right now we only have a Python, so there is nothing here that we can select, if we try to format it with Python, nothing really changes.
1:19
So, what we have to do is we have to first install black, and then we have to go to settings and use Black as our format.
1:26
First, let's install black. So for that I will use pipx because I want to have black installed globally on my computer.
1:35
Not like that. Okay. And just to be sure that I will be using this black from pipx, I will copy the full path to the black binary
1:45
Yeah, that's the file I want. That's the binary for the black that we can use.
1:53
Now we have to open the settings and set for Python format provider or something like that. Yeah, by default,
2:05
it's PEP 8, but we want to change it to black, and we also need to specify the path to the black executable.
2:12
Lets search for Black, and that's such only in Python by default, Python will just call command black, and that should work. But just to be safe,
2:22
we can replace it with our pipx version. So, when we close it and when we try to format this document.
2:31
Tadaah, now our ugly Python file has been formatted with black, all the indentations are fine. All the white spaces are fine,
2:39
and it looks much easier to read. Black offers almost no customization. The only setting that you should be able to change
2:47
is the line length. If you don't like the default 88 characters, there are some other options likes skip string normalization that disables,
2:55
replacing single quotes with double quotes. But according to the documentation, you should not use it.
3:01
This option is meant for adding black to an existing project. If you want to prevent it from changing almost every string in your code,
3:08
so black is opinionated. Most people like it that way. Some people don't. The main argument against Black for a long time was this single
3:17
quote to double quote conversion. I worked with people who wouldn't merge a pull request
3:22
because they didn't like the code formatting, those discussions should not be part of the code review. You should install a code formatter,
3:29
run it on everyone's code and then focus on the important part during the code reviews
3:34
If you are looking for a code formatter that it's more flexible and actually lets you modify some settings, take a look at yapf,
3:43
yapf stands for yet another Python formatter. It allows you to write a configuration file where you can specify different options.
3:51
So, if you want, you can sit down with your whole team, and you can all agree on a common formatting style so everyone will be happy.
3:58
I personally use black whatever I can. Sometimes I have a bit more complicated data structure,
4:03
so I indent them in my own way to make them easier to read. If I'm not happy with how black formats this code,
4:10
I might disable formatting for that one line.
4:13
But in general I'm quite happy that I don't have to deal with the formatting configuration and everything works fine out of the box.