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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Another tool that can automatically run some checks on your code is called pre-commit. Its name actually comes from Git.
0:13 When you open any good repository, you will see a folder called Hooks, so git has a set of hooks that can execute some scripts in different situations.
0:22 For example, before you try to push your code to the repository after you pull your code or before you create a new commit.
0:31 If you open any of those files, you will see that they're full of bar scripts, and they also contain instruction on how to actually turn them in to
0:38 Working hooks. You have to remove the .sample from the end of the file and then git will execute this file in a specific situation.
0:48 But we are not going to write shell scripts by hand. We're going to use a tool called pre-commit.
0:54 This tool lets you automate what checks you run on your code when you create a you commit in your git repository.
1:02 So first you have to install it with pip or pipx. Then you need to create a configuration file.
1:08 In this file, you specify what tools you want to use or what commands you want to run. All those tools are called hooks.
1:16 Then you install those hooks by running pre-commit install command. And, as you can see, this will create a pre-commit file inside our git repository,
1:26 and then you are set. Those hooks will be automatically run when you create a new commit. Also, when you're running pre-commit for the first time,
1:33 you can optionally run it on all the existing files. A very popular way of. Using pre-commit is to run some tasks that will
1:41 format your code to make sure that it matches the code style that the rest of your team is using. So, you might set a plugins like black or isort
1:50 that will check if you're code, has correct style and correctly sort it import statements.
1:56 If it doesn't, they will prevent you from creating a new commit, and they will modify your code.
2:02 Then you can inspect your code to see if everything still looks fine, and then you try to commit again, and this time it should be successful.
2:10 Some other plugins, like Flake 8, will report errors, but they want automatically fix them so you have to fix those
2:17 errors by yourself and then try to commit again. You can also run tests, But unlike Black or Flake 8,
2:23 running tests actually takes longer than a few seconds. So, I usually don't run test in my pre commit because it takes too long. I run my test by hand,
2:33 either using Tox or directly using pytest. And then I configure my CI server to run tests to, you might be thinking
2:40 Why should I run black and Flake8 using pre commit, if I already configured my code editor to run them, when I write code?
2:50 That's because using pre-commit standardizes the settings for Black and Flake 8 between all the
2:56 pip that you work with, you might have configured Flake 8 in your VSCode.
3:00 But your colleague, I, have a different configuration or someone doesn't know how to configure their code editor at all.
3:08 So, they don't use black, and they ride their Python code style in whatever
3:12 fancy way they like. Pre-commit uses a single configuration file that you keep in the git repository of your project, and once you set it up,
3:21 you can forget about it, If you're call, this correct, then pre-commit will allow adding it to the repository. If it's not, it will complain.
3:30 So, it's an automatic gate that prevents people from committing code that doesn't meet the standards of your team. That way,
3:39 you don't have to waste time during code reviews arguing about some incorrect code styles or some easy to spot problems that can be found with Flake 8.

Talk Python's Mastodon Michael Kennedy's Mastodon