Modern Python Projects Transcripts
Chapter: CI
Lecture: pre-commit in action

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

Talk Python's Mastodon Michael Kennedy's Mastodon