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