Python's black and poetry
TLDR; Use Black and Poetry in Python projects.
In our current state of affairs, we’re stuck in what’s come to be known as the “attention economy”. This is especially true for developers and engineers. The energy for them to focus and do useful work is the most expensive part of any project. “Death by a million paper cuts” is a common problem and if one would like to keep their team sane and productive, a modern platform or codebase should have these features:
Devs are able to setup local programming environments quickly
Devs are able to bounce between repositories quickly
CI/CD pipelines run quickly and fail as early as possible
Devs are able to easily run unit and integration tests locally (this is a subject to be approached in a separate blog post)
The unfortunate issue in justifying the use of Black and Poetry is that they are force multipliers. In other words, it’s impossible to objectively quantify how much time and money they will save. I’ve worked in countless Python repositories and not having Black and Poetry integrated with the DevOps/Agile cycle feels painfully regressive now. If one does not follow the wise words of Shia LaBeouf above, it may be too late to help.
Python Black
https://black.readthedocs.io
Ever have one of those code reviews … you know, one of those code reviews? The one that took a whole sprint to implement and now someone wants to spend another week asynchronously going back and fourth discussing how many chars should be allowed on a line, where the newlines should be, and whether or not the team should use tabs or spaces?? Yup, this is where a manager or tech lead needs to step in and say, “We’re going to use Python Black now so we can have useful code reviews”.
Make Black’s format checker the 1st quick test to run in CI and fail quickly. Devs should be able to locally code whatever style they like, then their last commit before pushing are changes created by Black’s auto-formatter. See the command:
python black .
The days of manually formatting your code are over. No more trimming whitespace at the end of lines or trying to figure out how to stay under the chars-per-line limit. All the code is consistently cleaner automagically, cognitive overhead removed, R.I.P. PEP-8! … Wow, we can start to feel the team moving faster already.
Python Poetry
https://python-poetry.org
Pip has been the standard package management for many years and still works pretty well. It's quick to setup, but requires separate files for development dependencies (see Black above!) , doesn’t figure out dependency resolution, and doesn’t do anything to help with virtual environments.
Pipenv was a great tool that came along to deal with all of the aforementioned issues. Personally, I used this tool for a long time and it works, but always felt extremely clunky. Tangentially, there was a whole bunch of drama and lack of development on what is a great project overall (feel free to go down that rabbit hole if so compelled). The primary issue was its dependency resolution/locking mechanism takes what feels like eons … it’s slowwww.
Welcome to the brave new world of Poetry! Deterministic builds, quick locking when doing dependency resolution, a single source of truth for all dependencies (dev or not), virtual environment management, easy configuration, cherries on top in the form of cute little emojis popping in the command line, and many other great features I won’t mention here. There’s a little learning curve that may take a few days, but once over that hump, your team and pipelines will start flying.
No, what are you waiting for?
With these tools integrated into your DevOps organization, many of those little “paper cuts” start disappearing so the team can focus on creating software efficiently for the business:
Checkout a repository and run poetry install, save time with ramping up
Bouncing between repositories gets easier with poetry’s virtual environment management
CI/CD pipelines can build/test Docker images with poetry in less time
Code reviews focus on business logic, not syntactical minutiae
Coding is a creative process. Make computers do the boring mindless drudgery and let us humans do what we do best.