Latest / Talk Python To Me / #282: pre-commit framework
Transcript
- 0:00Git hook scripts are useful for identifying simple issues before committing your code.
- 0:04Hooks run on every commit to automatically point out issues in your code,
- 0:08such as trailing white space and debug statements.
- 0:11By pointing out these issues before you get to a code review,
- 0:14this allows the code reviewer to focus on the architecture of a change
- 0:18while not wasting time with trivial style nitpicks.
- 0:21As we create more libraries and projects,
- 0:24we recognize that sharing pre-commit hooks across projects is painful.
- 0:29That's why I'm happy to welcome Anthony Sotili to the show to discuss pre-commit,
- 0:33a framework for managing and maintaining multi-language pre-commit hooks.
- 0:38This is Talk Python To Me, episode 282, recorded July 1st, 2020.
- 0:57Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 1:03This is your host, Michael Kennedy.
- 1:05Follow me on Twitter where I'm @mkennedy.
- 1:07Keep up with the show and listen to past episodes at talkpython.fm.
- 1:11And follow the show on Twitter via at Talk Python.
- 1:13This episode is brought to you by Brilliant.org and us.
- 1:18Python's async and parallel programming support is highly underrated.
- 1:22Have you shied away from the amazing new async and await keywords because you've heard it's way too complicated or that it's just not worth the effort?
- 1:29With the right workloads, a hundred times speed up is totally possible with minor changes to your code.
- 1:35But you do need to understand the internals.
- 1:37And that's why our course, Async Techniques and Examples in Python, show you how to write async code successfully as well as how it works.
- 1:45Get started with async and await today with our course at talkpython.fm/async.
- 1:51Anthony, welcome to Talk Python To Me.
- 1:54Yeah, glad to be here.
- 1:55It's great to have you here.
- 1:56You and I have been on together on Python Bytes along with Brian.
- 2:00We had you over there once, I believe, but never on Talk Python.
- 2:04So welcome to the show.
- 2:05Really happy to have you around.
- 2:06And you have this cool project that we're going to talk about.
- 2:09And it's kind of like the old thing is new again, right?
- 2:12It's been around for a little while, but it's some of the other stuff that seems to have boosted its popularity.
- 2:17And to me, it seems like a fantastic idea with pre-commit hooks and a framework for running them and building them.
- 2:23So I'm excited to dig into it with you.
- 2:24Sounds great.
- 2:25Happy to talk about it.
- 2:26Yeah.
- 2:27Before we do, though, let's get to your story.
- 2:28How did you get into programming in Python?
- 2:29My first Python programming was actually at Yelp, where I worked as a software engineer.
- 2:35I was hired as a front-end developer and mostly did CSS and JavaScript.
- 2:40But quickly realized that in order to unblock my own job, it made a lot more sense for me to learn the back-end and write my own APIs instead of waiting for a back-end engineer to implement the same APIs.
- 2:52So I picked up a bunch of Python and made my job a lot easier in that way.
- 2:56Nice.
- 2:56What did you use on the back-end?
- 2:57Was that Flask, Django, something else?
- 3:00So unfortunately, Yelp was created before there were a lot of these larger frameworks.
- 3:06And so Yelp had kind of their own homegrown framework.
- 3:10I eventually shifted from front-end to full-stack to back-end to infrastructure.
- 3:15And part of the infrastructure work that we did there was to move the monolithic code base to be based on Pyramid.
- 3:22So kind of like the Pyramid pylon sort of approach.
- 3:25And it was actually really nice because we could basically cut out parts of the proprietary Yelp main code and stick in bits of Pyramid so that we could offload some of the logic to open source software.
- 3:37Oh, nice.
- 3:38Yeah, that's really cool.
- 3:39Most of my sites are built in Pyramid as well.
- 3:40I like that framework.
- 3:41It gives you a ton of customization.
- 3:43It's a little bit daunting to start with like small projects.
- 3:46I usually prefer to use like Flask or whatever.
- 3:48Yeah.
- 3:48But it definitely lets you do everything.
- 3:50Yeah.
- 3:50Well, if I'm looking at new things that look really sweet, FastAPI is looking sweet.
- 3:55Yeah, I've heard some good things.
- 3:56I haven't had a chance to try it out yet, though.
- 3:57You know what I really like about it is you can define models, classes in Pydantic, and then say your view method takes one of those models.
- 4:06If you do get receiving like a JSON post and it'll do validated pre-population of all the fields using the Pydantic validation, which I'm just like, oh, that's just saved me like two thirds of my whole app right now.
- 4:16That's awesome.
- 4:16Yeah, that's pretty good.
- 4:17Yeah, I feel like I spend most of my time in validation, input validation.
- 4:20So if it's...
- 4:21Exactly.
- 4:21Or converting.
- 4:22I know it's a string, but then we're going to cast it to an int if it can be casted to an int.
- 4:25All that kind of stuff.
- 4:26Like, yeah, it's all built in.
- 4:27So anyway, if I go build some new APIs, that's not just part of the main app.
- 4:32I probably would do some FastAPI stuff, but that's pretty new.
- 4:34It's only been around a year and a half.
- 4:36Nice.
- 4:36Awesome.
- 4:37So you moved from the front end stuff over to the back end at Yelp, and it sounds like you got to do some really cool transformation, sort of empowering things there.
- 4:45What are you doing now?
- 4:46Well, technically, I'm currently unemployed, but my current passion is developer tooling and kind of infrastructure in that space.
- 4:55I was recently at Lyft where I led the developer experience organization and, you know, building tools and infrastructure that make developers productive.
- 5:04The idea being like, I can invest some in tooling and give that to all developers and then they can get their jobs done faster and better and safer and etc.
- 5:14Yeah.
- 5:14Awesome.
- 5:15Are you looking for that consulting stuff?
- 5:16People are out there or are you got plans already?
- 5:20So right now, right now I'm trying to build my own project and see where it goes.
- 5:24But I've kind of time boxed myself to like six to nine months.
- 5:28And if it doesn't turn out well, when it doesn't turn out, I'll be looking for new employment after that.
- 5:33It might turn out.
- 5:34It might turn out.
- 5:35You're a good programmer.
- 5:35I know you can do it.
- 5:36Awesome.
- 5:37There's always a chance.
- 5:38Yeah, for sure.
- 5:39You got to give it a shot.
- 5:40That's awesome.
- 5:40Well, let's talk about this project that you've been working on for a while.
- 5:43But like I said, has gotten a little bit of traction, a lot of traction lately because of tools like Black and other things that have made pre-commit hooks awesome and exciting all of a sudden.
- 5:55But before we talk about what you've been doing, let's just talk about the idea of pre-commit hooks in general.
- 6:00Sure.
- 6:00What is this for a lot of people who are like, yeah, I kind of know what Git is.
- 6:04I kind of use that or maybe have even used like zip as a zip and name of a date as a source control.
- 6:10Final one, one, one.
- 6:12But yeah, the idea behind Git hooks and specifically the pre-commit hook, because I think that's probably the one that most people get the most interaction with.
- 6:20But there are a bunch of commands in Git where you can register callbacks as scripts to either do like validation or like seeing some people use it to like send emails or like close tickets, all sorts of other stuff.
- 6:33The main focus around Git hooks to me is the pre-commit hook where you can do like linting and code validation, code formatting.
- 6:43You can run tests or other stuff like that.
- 6:45I guess the pre-push hook is another one that's also kind of big in that same space where you want to do validation of your changes before you send them off to like a...
- 6:54Right. So you could like maybe reject some kind of reject the Git push if the formatting is wrong or the header is missing or something like that, right?
- 7:03Yep. You can take a lot of those like easy to validate things and do them in a kind of a quick fast manner before you would do your larger test suite or something or catch a syntax error before you spend a bunch of time spinning up your CI systems.
- 7:18Right. Well, speaking of CI, to me, this seems like the next natural progression from having CI do these tests, right?
- 7:27So there's different levels. The developer should probably be writing and running tests and making sure that the tests pass.
- 7:33They should be like formatting their code before they check it in and stuff like that.
- 7:37But when you work on a team, my experience has been there's a wide, wide range of how much people are willing to do that, how much they care about those kinds of things.
- 7:48And what that means is maybe you have CI continuous integration that runs automatically during check-in or once check-in is done.
- 7:55And so then they might check in something and might say, oh, the build is now broken because you didn't bother to run the test, but you broke the test.
- 8:03But because you didn't run them, you didn't know it.
- 8:05So, you know, a tree falls in a forest. No one hears it, right?
- 8:07That sort of thing.
- 8:10And so then you end up in the situation, the people that care about the build working have to track down the person who broke it, who didn't actually care.
- 8:17Like, it's just these layers of like annoying type of thing.
- 8:19Yeah.
- 8:20And if you can make the sort of push that validation to the location where the person is and all the people, right?
- 8:26So even if you care, like you might not want to break the build, you might rather just get a warning or just automatically have it fixed.
- 8:32And pre-commit hooks seem like that's the natural place for that.
- 8:35Yeah. Kind of the mentality that I always had is like, if I'm waiting till CI to get feedback on like nitpicks around commas or white space or syntax or whatever, that to me is way too late in the process.
- 8:49Because I've already like, you know, I've pushed, I've already gone off to the next thing.
- 8:53I'm already answering my email or looking at GitHub issues or talking in Slack or whatever.
- 8:59And like, I've already context switched to a completely different situation.
- 9:02I'm like, I could have been, I assumed it was good because I already committed and pushed, right?
- 9:07I never make mistakes.
- 9:08So yeah, exactly.
- 9:09Zero fault code.
- 9:10Git push is like the final action.
- 9:12Now you're done, right?
- 9:14Yeah.
- 9:15But like, it was incredibly frustrating to like make a push and then have some either build system telling me that something was wrong or in code review, someone was like, oh, well, you could have reordered these imports.
- 9:26So they're alphabetical or something.
- 9:27It's just like, yeah, this is a big waste of time.
- 9:30Let's kind of push this as far towards the developer as possible such that we can make that a better, a better situation.
- 9:36The other thing I think is interesting around these ideas is there's been studies that have shown that people are more willing to take nitpicky advice from a computer than from a human.
- 9:47There's like, oh, okay, well, the computer requires that I have this kind of white space or this kind of indentation or like this type of fort, like you said, order alphabetical ordering or whatever.
- 9:56And when it comes from a person in a code review, it's like, well, that person is just a jerk, right?
- 10:00I wrote good code.
- 10:01Here I am taking this flack for this thing.
- 10:03So having this happen like automatically, I think takes away the need to review that kind of stuff.
- 10:09It takes away the need to complain and be that grumpy person that does that.
- 10:14And you can even go farther with tools like Black, where it doesn't just complain.
- 10:18It just goes, I fixed it for you.
- 10:20Yeah, actually, the way that I usually talk about this is like the absolute, the worst situation is that a human tells you that something is wrong.
- 10:26The next worst situation is that like a CI system tells you that something's wrong.
- 10:30Better than that is that an automated local tool tells you that something is wrong.
- 10:35And the like golden standard is that an automated tool just fixes it for me.
- 10:39Like I don't, don't have to worry about it all.
- 10:41It just makes it happen.
- 10:42Yeah.
- 10:42And it also results, you don't have this sort of like dueling alternate format style, right?
- 10:49Like if I like to work in PyCharm, you like to work in VS Code and our formatting rules vary ever so slightly, you know, like commas between parameters or there are two spaces between the colon on a type annotation.
- 11:01You know, if we both keep reformatting document, they cycle back and forth.
- 11:04Right.
- 11:05And this way you can sort of just hit it with the same formatter right before it goes every time.
- 11:09Of course.
- 11:09Yeah.
- 11:10And that also helps with things like get blame or like noticing when patches are minimal or whatever.
- 11:15And like being able to triage a change way down the road, not be distracted by two different editors reformatting in a different way.
- 11:26This portion of Talk Python To Me is brought to you by Brilliant.org.
- 11:29Brilliant has digestible courses in topics from the basics of scientific thinking all the way up to high-end science like quantum computing.
- 11:36And while quantum computing may sound complicated, Brilliant makes complex learning uncomplicated and fun.
- 11:42It's super easy to get started and they've got so many science and math courses to choose from.
- 11:47I recently used Brilliant to get into rocket science for an upcoming episode and it was a blast.
- 11:51The interactive courses are presented in a clean and accessible way and you could go from knowing nothing about a topic to having a deep understanding.
- 11:59Put your spare time to good use and hugely improve your critical thinking skills.
- 12:03Go to talkpython.fm/brilliant and sign up for free.
- 12:07The first 200 people that use that link get 20% off the premium subscription.
- 12:12That's talkpython.fm/brilliant or just click the link in the show notes.
- 12:17So that brings us to your project, pre-commit.com, which to me, I thought this was kind of something new because I hadn't heard of it before.
- 12:28And I feel like with all the excitement around Black being one of these auto-formatters that we just discussed, some of the other tooling around get pre-commit hooks,
- 12:38that this was like this big new thing, but we were talking and you told me it's been around for a little while and it's just gaining a lot of momentum now.
- 12:44Yeah.
- 12:44The original project actually came out of my want to enforce my partners in group projects in college to make sure that their white space was nice and that formatting was good.
- 12:57But it was originally just like a several hundred line Python script pre-commit.py way back in 2012.
- 13:04The first public version of pre-commit, I believe, was May of 2014, which means that it just turned six years old a little while ago.
- 13:13And for the most part, the framework's idea has remained basically the same since the beginning.
- 13:19Most of what's happened since 2014 has been a lot of bug fixes, a lot more platform support.
- 13:26So like making things work well on Windows and other exotic platforms like macOS, which is basically the Wild West, right?
- 13:34These uncommon.
- 13:35Yeah, exactly.
- 13:37Exactly.
- 13:38But also like support for other programming languages.
- 13:40So even though pre-commit is written in Python, it aims to target a bunch of different programming languages.
- 13:45It can both lint and run tools in, I think it's like nine or 10 or 11 different programming languages now and continues to support more going forward.
- 13:55It's pretty easy to add support for another language.
- 13:58Yeah, very cool.
- 13:59So the idea is this is a framework for creating pre-commit hooks and some tooling to install and initialize some of the various plugins.
- 14:08And one of the things that's like you just covered is pretty unique to it, I think, is that it happens to be implemented in Python, but it'll let you both install linters that require different runtimes like Ruby or Java.
- 14:20I'm not sure about Java.
- 14:21I don't remember.
- 14:22It doesn't quite have Java support yet, but it's in the works.
- 14:25But the cool thing about it is that it aims to make it so you don't have to set up anything locally.
- 14:30You just install pre-commit, you have a configuration file, and it manages installing and running all those tools for you.
- 14:38So you don't have to worry about, like as a sysadmin, you don't have to worry about distributing Ruby to your machines or like installing some weird Node.js package on your system or like maintaining the version of that or updating and downgrading or whatever you need to do with that.
- 14:53And like pre-commit will just install those tools for you.
- 14:56Right.
- 14:57And there's a lot of tools that we may care about, even if we're just Python developers that are not in Python.
- 15:02For example, we might be creating a Pyramid web app that has a SCSS, like a less SAS type of programming language for CSS.
- 15:12There's a nice linter in Ruby, but we got to have Ruby in order to do that, right?
- 15:16Yeah.
- 15:16Unfortunately, pre-commit makes setting up SCSS lint in particular, if that's the one you're referring to, makes that pretty easy to set up and go.
- 15:24You just add a little configuration and it will set up a Ruby environment, even if you don't have Ruby installed on your machine, and install SCSS lint and make that easy to go.
- 15:34That's pretty cool.
- 15:35The weird thing about SCSS lint, though, is it's kind of stopped development as SCSS has moved from either the Ruby implementation to the C++ implementation or the Dart implementation.
- 15:47And there are actually linters for SCSS in JavaScript as well, which is another popular programming language that pre-commit supports.
- 15:55And a lot of people have been moving to those other ones as well.
- 15:58But again, like pre-commit will just install that and make that easy to run for you.
- 16:02So how's that work? Talk me through the, I don't have Ruby on my machine. I want to run some tool as a pre-commit hook that needs the Ruby runtime. Where's the magic?
- 16:12Yeah, so I actually think that's probably the biggest selling point of pre-commit is all of the language smarts where it knows how to install and run things.
- 16:21But let's talk about SCSS lint specifically. So what you would do is you would install pre-commit on your machine.
- 16:27And there's directions in like the quick start guide on pre-commit.com.
- 16:31You would set up a configuration file, a small YAML file, which points at some repositories which provide hooks.
- 16:38In this case for SCSS lint, I believe it's pre-commit slash mirrors SCSS lint.
- 16:43There are some repositories where we have to mirror them because the upstreams are either dead or, or like, don't want to add a small bit of metadata to their repository.
- 16:52And once you have the configuration file, you can run pre-commit run or pre-commit install to make it part of your Git hooks.
- 16:59And pre-commit run will, in the case of Ruby, it uses several different approaches to try to set up a Ruby environment.
- 17:07So the first thing it'll attempt is if you already have Ruby installed, it'll just kind of skip all of the environment setup and it'll just reuse the Ruby that you have.
- 17:15But it'll install SCSS lint in an isolated fashion.
- 17:19So it tries to stay away from everything else that's running on your system.
- 17:22Basically, the idea is like pre-commit provides virtual environments, but for every other program.
- 17:26Right, right.
- 17:27That's cool.
- 17:28And when you don't have Ruby, it will either download it using RVM or RB env, which I love.
- 17:36I love the Ruby tooling landscape.
- 17:38It's like impossible to talk about either of the tools because they both sound the same if you slur your words together like I do sometimes.
- 17:44Yeah.
- 17:45But RVM provides a bunch of pre-built binaries for a bunch of platforms.
- 17:50So it makes it easy to just like download and run Ruby.
- 17:52And R-B-E-N-V, the other tool, makes it really easy to build Ruby from source.
- 17:58And so pre-commit uses both of those tools to provide Ruby environments.
- 18:02I see.
- 18:02So there's this background thing that just, if you don't have the right runtime, it'll go and get it.
- 18:08Yep, it'll make it happen.
- 18:09Yeah.
- 18:09And there's a similar tool called NodeM or JavaScript that pre-commit leans on as well to set up those environments.
- 18:15NodeM is kind of a mesh between the ideas of like PyEnv and VirtualEnv.
- 18:21It actually has some like direct integration with VirtualEnv, but it basically allows you to go from nothing to a working JavaScript environment without having to install anything on your system.
- 18:31Nice.
- 18:32Yeah, yeah.
- 18:33That's super cool.
- 18:33I do think that that's the magic, right?
- 18:35You can use all these different pre-commit hooks without a setup beyond just pip install or pipx install pre-commit.
- 18:43By the way, does pipx make sense?
- 18:45Are you familiar with pipx?
- 18:46Yeah, I'm familiar with pipx.
- 18:47I think it works for pre-commit.
- 18:49I haven't tried it myself, mostly because I manage stuff with virtual environments, but it should work.
- 18:54There's nothing super special about it.
- 18:55Yeah, it seems like it would as well to me.
- 18:58Yeah, yeah.
- 18:58So you install it and then that's all you got to worry about.
- 19:01You don't have to worry about having Ruby or Node or how about even Python?
- 19:05You do need Python.
- 19:06That's the one dependency.
- 19:08Right, because it's implemented in Python, right?
- 19:10Yeah.
- 19:11There's plans to build a, what is it called?
- 19:14Py installer?
- 19:15Is that the one?
- 19:16I don't know.
- 19:16There's a bunch of different tools.
- 19:17Yeah, yeah.
- 19:18Py installer or PyToApp or yeah.
- 19:20Like one of those or CXFreeze.
- 19:22So you've been thinking about potentially like turn it into one of these raw executables?
- 19:27Yeah.
- 19:27Unfortunately, I know almost nothing about them.
- 19:29So I haven't had a chance to look into that.
- 19:32But it should be pretty straightforward to make that happen.
- 19:34Yeah, that'd be cool.
- 19:35But yeah, one other thing before we move on, like I think the two, there's a bunch of these
- 19:40GitHub frameworks.
- 19:41And like, I think pre-emit is pretty unique in this installs the tools for you sort of aspect.
- 19:46I think like a lot of these other frameworks make two main mistakes.
- 19:49One of them is like, well, the first mistake that everyone seems to make is like you check
- 19:54in this 2000 blind bash script and that's how you manage your GitHub.
- 19:58And like anytime someone needs to change that, they've got to dive into this terrible script
- 20:03that always breaks and makes people's workflows really frustrating, which is actually what we
- 20:07had at Yelp before Yelp moved to pre-commit.
- 20:10But the other thing is managing tools.
- 20:13I find that a lot of shops will install these linters globally and their get hooks will assume
- 20:21that they're installed globally.
- 20:23And you instantly have like three different problems from that.
- 20:27One is version drift, both that all of your developers will need to like make sure that
- 20:32they're on a specific version.
- 20:33The other is you need environment setups.
- 20:36You can't just like switch from your laptop to your personal machine or, you know, maybe
- 20:42I'm working on a netbook in a coffee shop or whatever.
- 20:44You would need to make sure that you have your very specific global environment setup.
- 20:48Right.
- 20:49That can always kind of drift and be painful.
- 20:51And the third thing is if tools are managed globally, upgrading is impossible because I have
- 20:59an old version of the code checked out and a new version of linter is now on the machine.
- 21:04And so my code doesn't pass the new linter and the new code doesn't pass the old linter.
- 21:08And like you have this, you kind of have this like lockstep deployment problem.
- 21:12That's really hard.
- 21:13I see.
- 21:13Yeah.
- 21:14Right.
- 21:14And then if you have a global, you have two projects and they're out of sync, then it's
- 21:18even worse.
- 21:18Oh, and yeah.
- 21:19The multi-project problem gets even harder.
- 21:21Like microservices become almost impossible.
- 21:23Right.
- 21:24But the cool thing with pre-commit is the tools are all versioned directly in the configuration
- 21:29file.
- 21:30And so upgrading a linter can be done in one atomic commit to move forward or backward.
- 21:34And so everyone is always at like a consistent state there.
- 21:38Yeah.
- 21:39That's really a good philosophy or good design principle there.
- 21:42So you spoke a little bit about the steps to get going, but maybe just walk us really quickly
- 21:47through a quick start just to what's it like if I've got a machine with no git commit hooks,
- 21:52no pre-commit installed, how do I make my project start doing some of these cool validations?
- 21:58Yeah, sounds good.
- 21:59So there is a quick start guide on pre-commit.com.
- 22:02So all of this stuff that I'm about to say here is pretty easy to, you know, follow the
- 22:06instructions there and get the same thing.
- 22:07But I'm just going to run through it quickly here as well.
- 22:10So the first step is acquiring the pre-commit tool.
- 22:14And you can do this in a number of different ways.
- 22:16Some of the operating system package managers have packaged it.
- 22:20So sometimes you can, you know, apt install or DNF install pre-commit and get it that way.
- 22:26I usually don't suggest that because the operating system package managers are usually like six
- 22:31to eight months behind and you won't get the newest features and stuff.
- 22:34But, you know, if you're on macOS, like brew install pre-commit is usually up to date.
- 22:38And if you're a Python project, you can just use pip install.
- 22:41I mean, even if you're not a Python project, you can also use pip install.
- 22:44There's also a condo package and a few other ways you can acquire pre-commit.
- 22:49But anyway, the first step is to somehow install the pre-commit tool.
- 22:52Right.
- 22:53Pick your favorite pick.
- 22:54Pick your favorite.
- 22:55Go with it.
- 22:55Yeah.
- 22:56Yep, for sure.
- 22:57The next step is to set up some amount of pre-commit configuration file.
- 23:01You can usually find one in a project that you use and know uses pre-commit.
- 23:06So I often copy and paste from another project that I've done or a project that someone else
- 23:11has done that involves pre-commit.
- 23:12But pre-commit also comes with a pre-commit sample config command.
- 23:16So you can get kind of a very, very basic configuration that you can expand out further.
- 23:20I think the default configuration comes with a YAML checker and some whitespace fixers.
- 23:25And you can always add your favorite code formatter or linter to that.
- 23:29There's a list of supported hooks on pre-commit.com slash hooks.html.
- 23:35And there's hundreds of different tools at this point that can be installed and run directly
- 23:40without any setup.
- 23:41But yeah, the second step is setting up a configuration file.
- 23:44The next step after that is to opt into the git hooks.
- 23:48So the git hooks are actually, you know, it was originally the point of pre-commit to be
- 23:53just like a git hooks manager.
- 23:54But at this point, I think pre-commit works almost better as just like a linter code formatter
- 23:59runner.
- 23:59If I would, honestly, if I went back in time, I would probably be like, Anthony, don't call
- 24:04it pre-commit.
- 24:04And Anthony, maybe don't use YAML.
- 24:07But yeah, because pre-commit now supports like a bunch of different git hooks, like pre-push,
- 24:13commit message, prepare commit message, post checkout, a bunch of other ones.
- 24:17Right.
- 24:17But also like, it's useful even if you're not using it in Git workflow.
- 24:21But yeah, anyway, the next step is to install the git hook scripts.
- 24:24And you can do that by doing pre-commit install.
- 24:26You need to install it for a particular hook type.
- 24:29It defaults to pre-commit, but you can install like pre-commit install --hook type.
- 24:33I don't know, like post checkout or whatever.
- 24:36Right, right.
- 24:37Prepare commit message.
- 24:38And so this looks in the YAML file where you specify the hooks that you want.
- 24:42And so when you install it, it goes, okay, these are the three I need.
- 24:44So it defaults.
- 24:45So the YAML file can be used for a bunch of different hooks.
- 24:48So the hooks in the YAML file specify a stages property, and that will decide which of the
- 24:54git hooks they fire for.
- 24:55You might, you know, have like a trailing whitespace hook, and it might run on commit, push, and post
- 25:03checkout, or I don't know.
- 25:04And so like, the specific git hook that you opt into is only part of the install setting.
- 25:10And so like, if you wanted to use pre-push with pre-commit, you would do pre-commit install
- 25:14hook type pre-push.
- 25:16But yeah, once you've set up the git hooks, it's usually a good idea to run pre-commit against
- 25:21your entire project, especially when you're just starting out with a new code formatter or
- 25:24something.
- 25:25And pre-commit makes this really easy with pre-commit run --all files.
- 25:29This will kind of take all of the tools, install all of them, and then run them against every
- 25:34file in your repository.
- 25:35Right.
- 25:35So this is as if you had done, you basically added everything, and then somehow triggered
- 25:41a git push to evaluate those files, but you can just make it happen to sort of set the baseline.
- 25:46Yeah, it's for setting a baseline.
- 25:47Yeah, because your first commit isn't going to touch every file in your code base.
- 25:50And pre-commit tries to be very smart about the files that it passes to linters to make
- 25:57sure that your git hooks are as fast as possible.
- 25:59Because you could lynch your entire code base every commit, but no one has time for that.
- 26:03Yeah, exactly.
- 26:05And yeah, once you've kind of got set up there, you can kind of iterate on your pre-commit
- 26:09configuration, like add more tools that you might want to see or might want to use things
- 26:13like FlakeGate or Black or your favorite import reorder or your SCSS formatter or whatever
- 26:20tools that you might want to do.
- 26:22You can also add your own custom tools as well.
- 26:25If you have existing scripts that you want to migrate to pre-commit, pre-commit makes it
- 26:29pretty easy to run stuff directly in your repository as local hooks or things that happen to
- 26:34be globally installed as you migrate to more of a managed hook situation.
- 26:39And kind of the last step that I usually suggest is if you're going to add this pre-commit validation.
- 26:44So pre-commit validation can always be skipped.
- 26:46There's --no verify in git commit, which allows you to just skip all the git hooks.
- 26:51So you could just turn it off if you wanted.
- 26:52Pre-commit also provides a skip environment variable that lets you skip individual hooks.
- 26:57So if you have like some buggy hook that you don't want to check or actually we had this one hook at Yelp that was hilariously slow.
- 27:06And so I just never wanted to run that hook.
- 27:09So I would just always skip it and make sure that I was adhering to the policy manually.
- 27:15Right.
- 27:16But you don't have to skip hooks.
- 27:19But anyway, they're always just client validation and client validation can always be bypassed.
- 27:24So it's usually a good idea to set up some amount of this linting validation in your continuous integration.
- 27:29Right.
- 27:30And fortunately...
- 27:31Hold on.
- 27:32First, it's probably a good idea to have continuous integration and then to set this up for it, right?
- 27:38Yeah, that's true.
- 27:39You have tests, right?
- 27:40Right.
- 27:42But yeah, it's always a good idea to have some amount of continuous integration.
- 27:45AKA users?
- 27:47No, I'm just kidding.
- 27:50Just kidding.
- 27:51I'm just going to hide because my chat in my stream the other day was hounding me for not having tests on my most recent project.
- 27:57And I was like, oh, yeah.
- 27:59I did write some yesterday, though.
- 28:01So I have non-zero tests now.
- 28:02That's good.
- 28:03Sorry, I didn't mean to derail you.
- 28:05You're saying you should also set this up at CI.
- 28:07Oh, yeah, yeah.
- 28:08And pre-commit makes that really easy to get set up in CI.
- 28:11You can essentially run the same command we ran earlier, which is pre-commit run --all files, and set that up as something that validates all of your code changes.
- 28:19How does it get set up in CI?
- 28:21Like, do you have to do the pre-commit dash install on every run?
- 28:25Does that get cached and only do the delta?
- 28:27Or what's the CI look like there?
- 28:29So, unfortunately, it's kind of a little bit fiddly to set up right now in CI, which is actually part of the project that I'm building right now.
- 28:38I'm actually working on building a kind of generic continuous integration solution that's aimed only at pre-commit.
- 28:46Oh, nice.
- 28:46So if you want to check out more information on that, it's pre-commit.ci.
- 28:49It's kind of a work in progress right now.
- 28:51But you can set up pre-commit with basically any CI provider that you have right now.
- 28:55But you have to manage the cache yourself.
- 28:58You have to figure out what command you want to run.
- 29:00You have to figure out, like, what change set boundaries you want to run against.
- 29:03So, like, maybe on a pull request, you only want to validate the files that changed between master and your branch.
- 29:09And, yeah, it's all pretty manual setup right now.
- 29:12And for Travis CI, it's five to ten lines of YAML.
- 29:16For Azure Pipelines, it's, like, 30 lines of YAML.
- 29:19And, like, if you don't get it quite right or if the cache is invalidated for weird reasons, like, your run will be significantly slower.
- 29:26And that's actually one of the big selling points that I'm trying to push for on pre-commit CI, which is that everything will basically always be cached because it can share all these tools amongst other developers, which is pretty cool.
- 29:38Right, right.
- 29:39Yeah, that's really cool.
- 29:40Yeah, but you've got a lot of examples, and I'll try to link to them in the show notes, of how to set it up for Travis, Azure Pipelines, all the various things, right?
- 29:49So that's pretty straightforward.
- 29:50Yeah, I think most popular ones are covered there.
- 29:53And if there's others that are missing, like, feel free to send a pull request.
- 29:56Yeah.
- 29:57Another cool thing that I'm trying to build into my CI tool is that, like, and this happens probably, like, ten or so times a month, where someone will come to one of my projects and submit a pull request.
- 30:09And they haven't set up the client tooling locally, so they aren't running the pre-commit hooks.
- 30:13They haven't run the code formatters or any of these things.
- 30:17And so they'll get kind of that middle to worst case of the CI system is telling them, oh, your code isn't formatted this way.
- 30:24Like, here's a patch that would fix it.
- 30:26But one of the features that I'm planning to build is that pre-commit CI will just automatically fix pull requests for you.
- 30:33So you won't have to, even if you forgot to set up the local tooling, the remote tooling will just auto fix it for you.
- 30:39Oh, that'd be nice.
- 30:40Instead of just saying, oh, it's wrong, you have to fix it.
- 30:44It just, it always comes in right.
- 30:45Yeah, it just fixes it for you.
- 30:46There's actually a tool.
- 30:48I guess it doesn't always come in right, because it could be, like, actually broken Python, and you could be validating.
- 30:53Like, there's things that can't be fixed.
- 30:54But if it could be fixed, it should be auto fix, right?
- 30:57Yeah, it tries to auto fix things that are auto fixable.
- 30:59There's actually a tool, I think Mariota made it.
- 31:02I believe it's called Blackout, which is a Black-specific tool that kind of has this same idea of, like, just fix the pull request for me.
- 31:11Sorry, I didn't set up Black locally.
- 31:13Yeah.
- 31:13But Pre-Commit CI kind of aims to be a generic tool for these sort of auto fixing things.
- 31:18Yeah, that's really cool.
- 31:19So you talked about, if I didn't set this up locally, the way this works is all of these get, in general, get pre-commit hooks, they don't stick to the repository.
- 31:29They're not, like, now part of the repo because I set them up.
- 31:33Like, every person on the team has to go through this three or four step process.
- 31:37Yeah, so they won't have to set up the configuration because usually you check that in so that it's shared amongst all your peers.
- 31:42But yeah, setting up the tool would be something that everyone else has to do.
- 31:45Okay.
- 31:46There are ways.
- 31:48So there are ways to kind of automate this among peers.
- 31:51So, like, one thing that we did at Yelp was we made pre-commit one of the Python dependencies of the application.
- 31:57So the installation part was skipped.
- 32:00And we also made the get hook set up part of the common make targets.
- 32:05So, like, you used to run, like, make minimal to get Yelp application working or whatever.
- 32:10And make minimal would install the hooks so that you didn't have to think about that sort of thing.
- 32:14Yeah, that's cool.
- 32:15In other, in managed environments, there are other things you can do as well.
- 32:18Like, you can set a get init template.
- 32:21And the init template could already contain the get.
- 32:24Pre-commit actually has a command that makes it easy to set up an init template.
- 32:27But yeah, if you're working just, like, on vanilla laptops or, like, on open source projects,
- 32:33like, you would need to consciously opt into this behavior.
- 32:36And actually, that's one of the bigger design principles of Pre-commit is, like,
- 32:39none of this stuff happens super automatically.
- 32:42Like, you're always opting into the behavior.
- 32:44Right.
- 32:45It's all optional.
- 32:46Okay.
- 32:46Yeah, I think that's positive, right?
- 32:48That's a good thing that you're not forcing it on people.
- 32:50But with the CI, you're kind of catching the mist.
- 32:53Yeah.
- 32:54The other part is, like, if stuff runs too automatically, people get concerned about security.
- 32:58And so it tries to not be, you know, an arbitrary code execution engine, even though it is with opt-in.
- 33:04Yeah.
- 33:05Well, people do get a little touchy when you automatically run tools against their source code.
- 33:09Yes, I've received quite a few issues that are like,
- 33:14why does this thing exist?
- 33:16You should delete it now.
- 33:17And it's like, oh, well, you don't have to use it.
- 33:20Exactly.
- 33:21Talk Python To Me is partially supported by our training courses.
- 33:27How does your team keep their Python skills sharp?
- 33:30How do you make sure new hires get started fast and learn the Pythonic way?
- 33:34If the answer is a series of boring videos that don't inspire,
- 33:38or a subscription service you pay way too much for and use way too little, listen up.
- 33:43At Talk Python Training, we have enterprise tiers for all of our courses.
- 33:47Get just the one course you need for your team with full reporting and monitoring.
- 33:51Or ditch that unused subscription for our course bundles,
- 33:54which include all the courses, and you pay about the same price as a subscription once.
- 33:59For details, visit training. talkpython.fm/business,
- 34:03or just email sales at talkpython.fm.
- 34:06Let's talk about some of the tools.
- 34:10You know, we've set the groundwork, right?
- 34:12It's like this framework for bringing in all these different types of pre-commit hooks.
- 34:16And when you say there's a lot that you can bring in, there are a ton.
- 34:19So maybe we could talk through some of the ones that come built in that you think are cool.
- 34:23I'll grab a couple as well.
- 34:24And then there's a bunch of others that are sort of external, but can be loaded.
- 34:28Yeah, sounds great.
- 34:29So pre-commit itself is a framework,
- 34:30but there's also an official set of pre-commit hooks that I and other contributors have written.
- 34:36And those are available at pre-commit slash pre-commit hooks on GitHub.
- 34:39And there's, what, like 29 of them that are provided kind of out of the box there.
- 34:47The original intent of the pre-commit hooks repository was to be Python specific.
- 34:53However, it's more shifted to like take the Python parts and split them out
- 34:57and kind of aim to be language agnostic.
- 34:59So there's a lot of checks in there for like your common configuration formats.
- 35:03So like there's checkers for like JSON, Toml, YAML, et cetera.
- 35:06There's actually some really cool ones for Python specific stuff.
- 35:10Like there's one that checks that you don't check in breakpoints or debugger statements.
- 35:15So like you accidentally put import PDB.
- 35:17It'll flag that and make sure that that doesn't end up in production.
- 35:21Right, right.
- 35:22You definitely do not want to leave a import PDB set breakpoint sort of thing because I don't know why the server is locked up,
- 35:30but it just seems like it's, there's not, it's not using the CPU, but it's just stuff's timing out.
- 35:34What's going on?
- 35:34Yeah.
- 35:35The number of times I've seen PDB quit in production, I'm like, oh, damn it.
- 35:40This is entirely my fault.
- 35:42But yeah, this kind of helps prevent that.
- 35:46Or like, you know, I left a breakpoint and my test suite exploded because there was a breakpoint in it or stuff like that.
- 35:51Right.
- 35:51So some of the stand out to me, like one was check AST, which checks whether or not Python files can be parsed.
- 35:58That's pretty cool because Python doesn't have a compiler, not in the command line version sense.
- 36:04I know it sort of, it does generate the PYC, but kind, kind does.
- 36:08There's not a build step is what I'm saying.
- 36:10Right.
- 36:10For sure.
- 36:11Right.
- 36:11Which goes through and verifies all the files.
- 36:13So this is a little bit like a build step for like really basic stuff.
- 36:16Yeah.
- 36:17Check AST kind of aims to be like a first line of defense.
- 36:21In a lot of ways, like other linters like Flick 8 could be a better choice than check AST.
- 36:26But yeah, check AST is kind of like the most basic, is this valid syntax check?
- 36:30We actually use this as part of moving to Python 3 at Lyft.
- 36:35We kind of had this like three to four stage process.
- 36:38And like the first stage was make sure all the syntax is valid Python 3 syntax.
- 36:42And like make sure that linters pass in Python 3.
- 36:45And then like make sure that Python 3 specific linters pass in Python 3.
- 36:49And then run the test in Python 3.
- 36:51And then go to staging and then production.
- 36:52And then delete all the Python 2 code.
- 36:54And that was kind of our, check AST was kind of our first step there in that process.
- 36:58I see.
- 36:59Yeah, that's really cool.
- 37:00Another is about checking for AWS secrets.
- 37:04Ah, yes.
- 37:06How many times have you accidentally your entire AWS account to the public on GitHub?
- 37:12Actually, GitHub has some special checking for this now as well to make sure that your AWS
- 37:17secrets or whatever are not leaked on commits.
- 37:20And Amazon actually has invested a lot in scanning public source code repositories to
- 37:25invalidate these tokens as quick as possible.
- 37:28Oh, yeah, that's cool.
- 37:30Because it costs, you know, it costs individuals a lot of money, but it also, Amazon spends CPU
- 37:35and other resources finding Bitcoin or whatever, whatever people are doing nefariously with
- 37:40these leaked tokens.
- 37:41But yeah, this is kind of like, again, a first line of defense for not checking in.
- 37:45Yeah, you don't want to do it.
- 37:46It's not like eventually someone's going to find it.
- 37:48It's bad.
- 37:49So there's systems like...
- 37:50It's like seconds.
- 37:52Yeah, it's like shh git is a project.
- 37:57There's a bunch of these.
- 37:58But this one is, should git find secrets and sensitive files across GitHub, including gist,
- 38:03git lab and bit bucket in near real time.
- 38:06And it does so by hooking up to like the public stream of activity.
- 38:11And like, as soon as there's a check in, it's just after it, right?
- 38:14And so there's three or four of these types of tools.
- 38:16And basically, it would be really nice if something like this detect AWS credentials,
- 38:21pre-commit hook, didn't let that get into the mix.
- 38:24For sure.
- 38:25Yeah.
- 38:25And there's actually a lot of other tools around detecting credentials in the pre-commit space.
- 38:30So there's direct integration for, I believe the tool is called Bandit, which is a Python
- 38:36specific...
- 38:37Yeah.
- 38:38Bandit's cool.
- 38:39...tool.
- 38:39And I believe they have direct pre-commit integration.
- 38:41So you can just set up Bandit as a pre-commit hook.
- 38:43I don't know.
- 38:44There's a Go project that's fairly popular that does the same thing.
- 38:47And pre-commit has direct support for Go in that particular tool.
- 38:51But there's a bunch of tools in this space that make it really easy to prevent check-in
- 38:55of sensitive credentials.
- 38:56Another one that stood out to me was no commit to branch.
- 39:00Yeah.
- 39:01This one actually was a community-contributed hook.
- 39:04The idea being that you don't accidentally commit directly to your master branch or to
- 39:10your particular development branch.
- 39:12And this helps you enforce your...
- 39:14Production or whatever.
- 39:15Yeah.
- 39:15Yeah.
- 39:15It helps you enforce your specific Git workflow that you want to do.
- 39:18So you're specifically targeting your own feature branch, or you could use this to
- 39:23enforce a branch naming scheme or a bunch of other stuff like that.
- 39:27But yeah, that one was actually contributed externally.
- 39:29Yeah, exactly.
- 39:30Basically, you require people to use a proper PR workflow, like a Git flow type of thing,
- 39:35rather than just jam it straight in.
- 39:37Yeet it straight to master.
- 39:38Heck, just edit it on production.
- 39:40That's even quicker.
- 39:41I deploy via SSH.
- 39:45Exactly.
- 39:46Yeah.
- 39:47Another cool one that we used at Yelp was forbid new submodules.
- 39:51We went through this.
- 39:52This is actually kind of a long story, so I'll keep it a little bit short.
- 39:56But Yelp went through a migration of installing all Python packages globally on the system to
- 40:02virtual environments.
- 40:03And there are some grumpy sysadmins that were super against virtual environments.
- 40:08And so we kind of cheesed our way to getting virtual environments by using submodules instead.
- 40:13And Git submodules, not Python submodules, Git submodules.
- 40:17And so there was one point where Yelp main had 98 Git submodules.
- 40:22And we were doing this fancy pip install workflow that allowed us to locally install all of these tools so that we didn't have to manage them at the system.
- 40:30But eventually, the grumpy people left the company and we moved everything to virtual environments.
- 40:36And part of moving to virtual environments was to burn down the tech debt of submodules.
- 40:41And so we added this forbid new submodules hook, which prevents newly introduced submodules while allowing existing ones such that we could kind of do some graph driven development and bring that down to zero while we migrated towards the new world.
- 40:55That seems like a good path there.
- 40:57Very nice.
- 40:58One that I was wondering about is check executables have shebangs, the little hash exclamation point, something at the top, which is fine.
- 41:07But how do you know it's an executable?
- 41:09Is it because it has a dunder main equals dunder name thing in it?
- 41:12Or what counts as an executable?
- 41:13So this is more about file permissions and accidentally checking in files that are not executable.
- 41:20So one example where this commonly gets triggered is when you copy files from a thumb drive, they'll commonly be on a FAT32 file system, which doesn't have full support for all of the permission bits.
- 41:33And so almost always the permission bits will be 777 and you'll end up checking in all sorts of non-executable and non-script files with executable bit permissions.
- 41:43And depending on your particular system that you're using, this can often trigger lint errors.
- 41:47And so this is kind of a first defense against that.
- 41:50One particular system is like Debian is real grumpy about, oh, this PNG file has the executable bit set.
- 41:56And so I'm going to reject this package or whatever.
- 41:59Right, right.
- 42:00But this also helps you if you've forgotten to put a shebang.
- 42:03So like sometimes you'll have like an entry point to your application that you intend to be executable, but you've forgotten to put user bin env Python 3 or whatever.
- 42:11Right.
- 42:12So this kind of checks for that.
- 42:13Okay, cool.
- 42:13So those are a bunch of the built-in ones.
- 42:16Any of the other external ones you want to give a shout out to?
- 42:18Yeah.
- 42:19So if you're setting up a Python project, you probably want an import sorter.
- 42:23And there are two, the two most popular ones.
- 42:27One of them I wrote, which is called Reorder Python Imports.
- 42:30It's very basic in what its name is.
- 42:32But the idea behind Reorder Python Imports is you set up essentially no configuration and it just does the right thing all the time.
- 42:39And if you kind of want the opposite of that spectrum, there's a tool called iSort, which has direct support in pre-commit.
- 42:44iSort has 50 or 60 configuration options, so you can customize your import sorting to whatever thing you want.
- 42:51Some others that I would suggest is getting a linter like Flake 8 set up.
- 42:56And Flake 8 has direct support for pre-commit as well.
- 42:59I'm also the maintainer of Flake 8, so of course it has pre-commit support.
- 43:03Of course. Awesome.
- 43:04And perhaps the most popular and probably one of the reasons that pre-commit has taken off a lot recently is Black, which is, we've talked a couple of times on this already about Black.
- 43:14But Black is a code formatter that there's one way to do it right, and Black does it in a very specific way.
- 43:20And Black has direct integration with pre-commit.
- 43:23You can also set up things like type checkers, like there's mypy integration with pre-commit.
- 43:28There's other sorts of stuff like that.
- 43:30And one more that I'll shout out, which is one that I've written, a tool called PyUpgrade, which allows you to kind of upgrade your syntax to newer versions of the language.
- 43:41So things like automatically making f-strings, or like removing Python 2 syntax constructs, or unsixing your code, so to speak.
- 43:49And that's another one that has decent pre-commit integration as well.
- 43:53I see. It looks for old idioms and styles and says, stop doing that.
- 43:58Yeah, well, not only does it say stop doing that, it just auto-fixes it for you.
- 44:01Oh, that's even better.
- 44:02But yeah, I really like my code formatters.
- 44:04Like, if a linter is grumbling at me, I'd rather have formatter just auto-fix it for me.
- 44:09Yeah, absolutely.
- 44:10All right, so that's the big list of things that you could use.
- 44:13But what if you have an idea for a new one?
- 44:15Part of the whole idea of pre-commit is it's a framework for building and distributing these things, right?
- 44:21Yep, and it's really easy to make your own set of hooks.
- 44:24That's actually another problem with the other sort of frameworks, is that if you want to add tools to it, you kind of have to fork their framework and, like, inject your code directly into their framework.
- 44:33But pre-commit takes kind of a distributed model to this.
- 44:38Basically, any git repository that has a little bit of metadata in it can provide a git hook, as long as it's installable in some way.
- 44:45And the process is outlined on the pre-commit documentation, but there's a bunch of different programming languages that this works with.
- 44:52And you basically set up a pre-commit hooks.yaml to provide hooks to other repositories.
- 44:58And there's a, you know, you can always follow an example on any of the other repositories that already provide stuff.
- 45:03And it's also pretty easy to take an existing tool and add the small metadata file to it that can be used directly with pre-commit.
- 45:11Of course, there's also escape hatches.
- 45:13Like, if you don't want to use the repository-managed approach, you can set up local hooks that go directly to PyPI or, like, use a Docker image or other stuff like that instead of going through the managed approach.
- 45:23Although there's some disadvantages to that around, like, automatic updates and workflow management and that sort of deal.
- 45:30Right. Yeah, that looks really cool.
- 45:32So it seems pretty easy to make them.
- 45:34And as long as it's installable via IPI or gem or npm or is it executable, it's more or less ready to go along with that metadata.
- 45:42Yep. Essentially, like, if you can be git cloned and something can run, like, pip install or the equivalent, you're golden.
- 45:48You're ready to go.
- 45:48Yeah. And then under supported languages, where maybe languages is in, like, quotes or something, you've got Conda, Docker, Docker images, fail.
- 45:57What is fail?
- 45:58So there's a special hook.
- 45:59This was actually added specifically for pytest, which I'm a maintainer of.
- 46:03Fail takes a file name regular expression and will always return one if anything matches that.
- 46:10So one example is, like, we wanted to enforce that our changelogs were a specific file name pattern because it's very common for somebody to write a changelog but forget the RST extension.
- 46:21And when they forgot the RST extension, then would go to run a release, we would forget their changelog fragment.
- 46:27And so their change wouldn't be called out.
- 46:29And so this was a way to enforce with a particular message that a file name matches specifically.
- 46:35Okay. Yeah, cool.
- 46:37And then you got Golang, Node, Perl, Python, Ruby, Rust, Swift, Script, I'm guessing Bash and whatnot.
- 46:44So that's a lot of options.
- 46:45And with Docker, like, that's a pretty wide open.
- 46:48Yeah, with Docker, you can basically do anything.
- 46:50Yeah, exactly.
- 46:51Exactly.
- 46:52Yeah, and it's actually pretty easy to add a new language.
- 46:54So if there's another language that you would like to see pre-commit support, there's actually a little guide in the contributing documentation for pre-commit on how to set up another one.
- 47:02Someone's actually working on support for Crystal right now, which is a language that I had not heard of until relatively recently, but it's basically like compiled Ruby.
- 47:12It's pretty cool.
- 47:13Okay.
- 47:14Yeah, there may be support for that.
- 47:15Yeah, nice.
- 47:16Something that I also saw in the docs was talking about automatically enabling pre-commit on repositories.
- 47:23Can you, like, register it with Git so when you get a new repo or clone something, it'll just be part of it?
- 47:28Yeah, so there's this template directory concept in Git.
- 47:32Basically, you can run pre-commit init templater and then set a specific configuration value in your Git config.
- 47:38And Git will splat out files in a particular way in your clone such that you'll automatically be enabled.
- 47:46Okay.
- 47:46Yeah, that's nice.
- 47:47This usually involves just, like, writing the Git hooks pre-commit file magically.
- 47:51Right, yeah.
- 47:52So if you really like this whole idea, then just make it automatic, right?
- 47:57Yep.
- 47:57We use this at Lyft so that whenever you would clone a Lyft repository, you would automatically have everything set up, which worked pretty well.
- 48:04Yeah.
- 48:05And one of the things that all the cool repositories are doing these days is they have those little badges, like, on their readme.
- 48:10Mm-hmm.
- 48:11Right?
- 48:11So it's Python 3, or it has this many downloads on PyPI.org, installs via pip, and so on, or whatever.
- 48:18And so you guys have a nice way to badge your project as well to say that it's powered by pre-commit to sort of spread the word.
- 48:26Yep.
- 48:26Actually, this one's a really cool thing that I want to shout out.
- 48:29This was actually contributed by an external contributor, which was like, hey, I want a way to, like, show other people, like, what tools I'm using.
- 48:36And, like, pre-commit is a really cool tool, and I want to see it succeed.
- 48:39And so they made SVG badge and, like, made it really easy to set up a little bit of markdown or RST or whatever text document format you want to use to badge your repository and spread the word.
- 48:52Yeah, awesome.
- 48:53That's really nice.
- 48:54And then last question on this, I guess, is are you looking for contributors to the project?
- 48:59There's a lot of people who ask me, hey, I want to get started in open source.
- 49:03Could you recommend something I work on?
- 49:05Would this project count as one of those?
- 49:06Yeah, we're always looking for new contributors.
- 49:08The thing with pre-commit is, I don't want to say it's feature complete, but it hasn't really gained any large features in quite a long time.
- 49:14Most of the places to expand pre-commit is to add more tooling support or to add more programming language support.
- 49:21And I'm always looking for expansions in those spaces.
- 49:24Or, you know, new ideas that I just haven't really thought of yet.
- 49:28Yeah, maybe there's some cool tool out there that's not integrated with you guys.
- 49:33Maybe they could commit to pre-commit by actually committing to that other project to get the right metadata.
- 49:38For sure.
- 49:39Yeah.
- 49:39There are a few like help wanted getting started smaller issues that are in the issue tracker.
- 49:45But I usually try and keep the backlog pretty well trimmed.
- 49:48Yeah.
- 49:49Nice.
- 49:49All right.
- 49:50Well, what a cool project.
- 49:52And it's definitely something I'm going to be checking out as well because it looks neat.
- 49:56Thanks.
- 49:56I'm really proud of it.
- 49:57Yeah, you should be.
- 49:58It's great.
- 49:59All right.
- 49:59Before we get out of here, though, got to ask you the two final questions.
- 50:03You're going to write some Python code.
- 50:05And I know you write a lot.
- 50:07What editor do you use?
- 50:08Oh, no, this is always a fun one.
- 50:10So if you would have asked me this six months ago, I would have been a little bit embarrassed
- 50:16about my answer, which maybe I'm still embarrassed about my final answer.
- 50:19But six months ago, I would have said nano as my text editor of choice.
- 50:23I've actually I'm actually really good at using nano.
- 50:27Before that, I was a IntelliJ user and I used that for the longest time.
- 50:31But I found that like context switching between the terminal and not was was a lot of work.
- 50:35But anyway, my real answer to this question, I have actually written my own text editor,
- 50:39which in retrospect, I would not suggest anyone to do this.
- 50:43It's been quite a roller coaster.
- 50:45But my texter is called Babby.
- 50:47The name is a little bit silly.
- 50:49It's actually nano.
- 50:50But if on a QWERTY keyboard, if you shift your hand over one and typo nano, you end up with
- 50:55Babby.
- 50:55And this was like a common typo that I would I would make pretty frequently.
- 50:59But yeah, it's open source.
- 51:01It's written in Python.
- 51:02Some of the ideas behind it were like, make it really easy to hack on and like add features
- 51:06to and like I wrote a lot of it on my Twitch stream.
- 51:09And interestingly, a bunch of the features in the text editor were actually contributed by
- 51:15just like people in chat, which were like, hey, this would be cool if Babby did this and
- 51:19like run off for 15 or 20 minutes and come back and have a working patch with the
- 51:24new command or new cool way to do something.
- 51:28That's awesome.
- 51:29But yeah, it's pretty easy to hack on.
- 51:30And that's part of the reason that I kind of like it.
- 51:33Is it pip install Babby?
- 51:34Yep.
- 51:35Pip install Babby.
- 51:36And that should work on most platforms.
- 51:38It works on Windows, oddly enough, which was actually also another thing that chat contributed.
- 51:44Like I developed it entirely on Linux and it's based on curses.
- 51:47Windows doesn't quite have native curses support, but you can install it like Windows curses package
- 51:53to make that work.
- 51:54Okay.
- 51:54Yeah.
- 51:54It should just be pip install Babby and start hacking on it.
- 51:58It's not feature complete in any way.
- 51:59So like there's some stuff that's, you know, like horribly broken.
- 52:02Like if you try and rename a file in Babby, it's just like, no, I don't I don't know how
- 52:06to do that.
- 52:07Sorry.
- 52:08But I use it as my daily driver now.
- 52:10So it's I know the shortcomings and I'll eventually get around to implementing those.
- 52:14But yeah, cool.
- 52:15It's pretty neat.
- 52:16Awesome.
- 52:16All right.
- 52:16That is the first time anyone has ever said Babby.
- 52:19So very cool.
- 52:19Now we're getting the word out about it.
- 52:21I'd be surprised if anyone else uses it, to be honest.
- 52:25All right, cool.
- 52:27Well, very interesting.
- 52:28And then notable PyPI package, something that maybe people haven't heard about.
- 52:31Ooh, something that people haven't heard about.
- 52:33Or just that you came across, you're like, it's, you know, it's not requests.
- 52:37It's not Django.
- 52:37It's like, oh, this thing I found is pretty awesome.
- 52:39People should know about it.
- 52:40It's not requests.
- 52:41You mean, you mean HTTPX?
- 52:43HTTPX is also cool.
- 52:45But it's just, you know, it's like the most common answer, I would say, is requests.
- 52:47That's true.
- 52:48Yeah.
- 52:49I actually, controversial opinion, I really don't like requests.
- 52:52So can we do an unpackage for this one?
- 52:55You can.
- 52:56Well, how about it?
- 52:57What do you use instead of requests?
- 52:58So if I get away with it, like I don't need H2 support or I don't need like special streaming
- 53:04APIs, I'll mostly just use standard library.
- 53:07Yeah.
- 53:08Although for like newer stuff where I need like streaming APIs or H2 or I need async support,
- 53:14I'll reach for like HTTPX or AIO.
- 53:16Yeah.
- 53:17AIO client.
- 53:18Yeah.
- 53:18In there.
- 53:19Yep.
- 53:19That's nice.
- 53:20I've actually like, I've not done a lot of asyncio yet.
- 53:23I've been dabbling with it a little bit.
- 53:25I'm writing a chatbot for Twitch and I've kind of started sprinkling around some of the AIO
- 53:31libraries.
- 53:32What do you think about async in Python?
- 53:34I think it's pretty good.
- 53:36There's a few like get it set up and get it moving things that kind of bug me.
- 53:41It bothers me that I can't just fire and forget async stuff.
- 53:44Yeah.
- 53:45You have to await it eventually.
- 53:46Yeah.
- 53:47Yeah.
- 53:47Or like, like if you just call the async function, you get a coroutine, but the coroutine isn't
- 53:52started until you like turn it into a task or you give it to the loop to run.
- 53:56Yeah.
- 53:57So that bugs me a little bit.
- 53:59The fact that like the threading module doesn't support async and await.
- 54:03Yeah.
- 54:04But the IO module does, you know, the multiple, like all of them should.
- 54:08So I actually am a huge fan of unsync, which is a library that puts a unified API on top of
- 54:14those and gives you the fire and forget.
- 54:16And it has a background thread that manages the runtime loop for you.
- 54:19So, oh yeah, I've played a little bit with unsync.
- 54:21It seems really cool.
- 54:22So I think the, what we have in Python is really close to right.
- 54:26There's just like a few rough edges.
- 54:28And I think it's not necessarily unsync is the right answer, but something like that is
- 54:31really close to like the final polish.
- 54:34It needs to take it from like a, a B to an A minus or something level like that.
- 54:38Right.
- 54:38Yeah.
- 54:39And then maybe once we get sub interpreters dropping the guild, you can take it up a notch
- 54:43as well.
- 54:43Oh yeah.
- 54:44Sub interpreters can be really cool.
- 54:45Yeah.
- 54:46My problem with async is I feel like I have to rewrite all my code.
- 54:49Sometimes that barrier to entry.
- 54:50Yeah.
- 54:51The challenge can be like it propagates, right?
- 54:53You want to do it way down and well, that becomes async.
- 54:56And then the things that call it become async.
- 54:58And the things that call it become async.
- 54:59And yeah, that can be a challenge.
- 55:01And your testing library has to be async.
- 55:03And then like all this, all this other stuff.
- 55:05Yep.
- 55:06Yep.
- 55:06I think it'll get there though.
- 55:08I think it's just like Python has 20 years of history and async was introduced pretty late
- 55:13into that history.
- 55:14And there's going to be some growth pains as it gets better, so to speak.
- 55:19Yeah.
- 55:19But places where I think that really shines is where you don't have to think about it.
- 55:23Like where the thing that you're doing as async is on the boundary.
- 55:26So for example, FastAPI, right?
- 55:28You can just make your view method async or not make it async, right?
- 55:32It's up to you.
- 55:32And then if you want to do that part of your app async, you can.
- 55:36But it's not from the bottom up, but it's kind of from the outside in.
- 55:39And you can pick on the entry points from the outside where you want to adopt it.
- 55:44And then I think that that makes a lot of sense there.
- 55:46Yeah.
- 55:46Especially like because you have a framework calling you, so you don't necessarily have
- 55:49to do all async or all not.
- 55:51Exactly.
- 55:52Exactly.
- 55:52You get a plug into it or opt into it.
- 55:54Yeah.
- 55:54But as a library designer, it's more challenging, I think.
- 55:57How do you expose an async version and a non-async version and so on?
- 56:01Definitely.
- 56:01Cool.
- 56:02All right.
- 56:03Well, that's not exactly one package, but that's kind of a conversation around packages.
- 56:06So let's maybe just put an answer to HTTPX or AIO, HTTP client.
- 56:11Those are two cool HTTP libraries that kind of fit in that world.
- 56:14I like them.
- 56:15Yeah.
- 56:15Sounds good.
- 56:16Cool.
- 56:16Yeah.
- 56:17I mean, usually my answers would be around like a code formatter or linter.
- 56:19I'll actually shout out like two of my favorite linters that are code formatters that I tend
- 56:26to use.
- 56:26I do really love Black, but I actually don't use it myself.
- 56:29I actually tend to use a combination of two formatters.
- 56:33One of them is called Autopep8, which is very similar to Black and some of its ideas and
- 56:38like fixing lint errors.
- 56:40And the other one, which is very similar to Black, but has existed for like four or five
- 56:45years, is a tool called AddTrailingComma, which literally does what it says on the tin.
- 56:49It tries to enforce trailing commas in the same way that Black does.
- 56:54Right.
- 56:55So the final element in like a list format, it would have that so that that line doesn't
- 57:00get a diff when you add the next element.
- 57:03Yep.
- 57:03It tries to make minimal diffs when adding or removing like parameters and like lists or
- 57:08tuples or function signatures or class signatures or type annotations or the whole gamut of
- 57:14places where you can have braces and commas.
- 57:17Cool.
- 57:17All right.
- 57:18That's a bunch of good ones.
- 57:19Okay.
- 57:19Final call to action.
- 57:20People are interested in pre-commit.
- 57:22They want to check it out.
- 57:23They want to start using it.
- 57:24What do you tell them?
- 57:24So I would tell them to go to pre-commit.com.
- 57:27That's probably the place where you can get the most.
- 57:29It's pre-commit.com, right?
- 57:31Correct.
- 57:32Yeah.
- 57:32With a dash.
- 57:33The one without a dash is like some weird real estate website that I've been trying to
- 57:37bug the guy to like give up his domain for years, but I don't know that he actually reads
- 57:41his email at all.
- 57:42But yeah, I would check out pre-commit.com and that's where you can get all the information
- 57:46about that.
- 57:47And pretty soon, pre-commit.ci will have more information about the CI system that I'm building
- 57:53around pre-commit.
- 57:54Oh yeah.
- 57:54That's really exciting.
- 57:55That's going to be cool as well.
- 57:56I'll put a link to that in the show notes also.
- 57:58Cool.
- 57:58All right.
- 57:59Thank you, Anthony.
- 57:59It's great to chat with you and see all these amazing things you're creating.
- 58:03Yep.
- 58:03Always glad to chat.
- 58:05Yep.
- 58:05But see you later.
- 58:06Bye.
- 58:06This has been another episode of Talk Python To Me.
- 58:10Our guest on this episode was Anthony Sotili and it's been brought to you by Brilliant.org
- 58:15and us over at Talk Python Training.
- 58:17Brilliant.org encourages you to level up your analytical skills and knowledge.
- 58:22Visit talkpython.fm/brilliant and get Brilliant Premium to learn something new every
- 58:27day.
- 58:29Want to level up your Python?
- 58:30If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 58:35Or if you're looking for something more advanced, check out our new Async course that digs into
- 58:40all the different types of async programming you can do in Python.
- 58:43And of course, if you're interested in more than one of these, be sure to check out our
- 58:47Everything Bundle.
- 58:48It's like a subscription that never expires.
- 58:50Be sure to subscribe to the show.
- 58:52Open your favorite podcatcher and search for Python.
- 58:54We should be right at the top.
- 58:55You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the
- 59:01direct RSS feed at /rss on talkpython.fm.
- 59:05This is your host, Michael Kennedy.
- 59:06Thanks so much for listening.
- 59:08I really appreciate it.
- 59:09Now get out there and write some Python code.
- 59:11Thank you.