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