Modern Python Projects Transcripts
Lecture: pre-commit in action
0:00 Let's add pre-commit to our calculator project.
0:02 First, I need to make sure that I have pre-commit installed, and now we
0:10 can generate a sample configuration file by running pre-commit sample-config.
0:18 Okay, so this doesn't actually create the file.
0:22 We have to create the file ourselves.
0:28 Let's do this in the VSCode and let's copy this.
0:37 So, what do we have here?
0:39 We have a hook to remove trailing-whitespace.
0:41 This is good. And the file-fixer.
0:43 This is also probably good because I think this will fix the file endings depending.
0:48 If you're using Linux or Windows,
0:50 check-yaml. We don't really need this and
0:53 Also, we don't need that.
0:55 What I want to have is black and Flake 8.
0:58 So, let's search for those hooks. With Black
1:02 We can actually get it from this example.
1:10 And to find Flake 8, we have to search in the list of all available
1:14 hooks. Let's go here. So here is a page that contains all the hooks
1:20 that you can use in your code,
1:21 and it's actually quite huge. So let's search for flake 8.
1:31 So here it is, but it says use GitLab pycqa flake8
1:35 instead. So let's do that.
1:39 So, let's just copy this line and I don't remember what's the latest version of,
1:47 flake 8. So let's actually check.
1:50 Let's copy this, Okay, 3.8.4 and hopefully that should work.
2:08 What's next? Next we have to run pre-commit installed, to
2:18 set up all the git hooks that we just configured and that's it.
2:25 Let's try to create a new commit.
2:28 We have some changes, so we can commit them.
2:34 And here you can see some differences between using git without pre-commit and with
2:39 pre-commit. As you can see,
2:40 it's now setting up all the pre-commit hooks that we have just configured.
2:44 It might take a moment. Let's add the message.
2:56 And for some reason, all the hooks were skipped.
2:58 I think it's probably because this was the initial run.
3:02 So, let's modify something in the files and let's try to create a new commit, let's
3:11 comment this thing out. Okay,
3:17 that's interesting. Okay now, and now,
3:29 our hooks were finally run. As you can see,
3:32 both the Trim Trailing Whitespace and Fix End of Files have failed,
3:36 and they have modified some files.
3:38 So if we do git status,
3:41 you can see that two files were modified.
3:43 We can see what changes were done by the pre-commit hooks by running.
3:47 Git diff. And it was mostly adding new line at the end of those files
3:50 Well, let's add them again,
3:54 and let's commit. And now everything is passing, Great.
4:04 Now, let's make some modifications that will make black fail.
4:08 Let's remove some white spaces here and there,
4:12 and let's try again. This time,
4:18 Black is complaining. But as you can see,
4:21 it has reformatted the file, and we again have to added by hand.
4:26 If we go back to the file,
4:29 you can see that we have our white spaces back.
4:32 Let's now try to make Flake 8 unhappy by importing a module and not using it.
4:45 So, again, Black has detected some issues,
4:47 but it has automatically fix them.
4:49 However, Flake has detected some problems,
4:52 but it can't fix them on its own.
4:54 So we have to actually go back and remove this line.
5:05 And now again, all our hooks are happy and we can create a new commit
5:08 and that's how you would use pre-commit in your project and As you can
5:16 see, we have this pre-commit config file.
5:18 So, when we push it to the git repository all the other team members on your
5:23 project and download it and use it on their own local computers,
5:27 let's talk about tools that you don't have to run on your local computer in the next lesson.