- Small Batches
- Posts
- It looks so puuurrtyy! đ
It looks so puuurrtyy! đ
Using `isort` & `black` formatting tools
[rating: beginner]
A couple of weeks ago I wrote about the PEP-8 Python style guide. Trying to keep track of all your code & making sure it is style aligned can be overwhelming.
So donât do that!
Use tools that apply the formatting for you.
Fortunately Pythonâs formatting tools are very simple to use, and nominally thereâs only two of them - isort
& black
. This means that itâs very easy to integrate them into a developers desktop environment, or into a pipeline run.
Whatâs the point?
Having uniform code style really does help everyone become familiar with what reading the code should look like. There is a flow state where the code can be skimmed to quickly acquire a sense of what the code is doing, & where to find relevant information. Maintaining code is typically a combination of this flow state - quickly acquiring information & then slowing down to think carefully about specific sections.
Code that is inconsistent in style breaks that flow & forces the reader to think consciously & slowly about all of it, instead of just some of it.
The cognitive load of trying to maintain consistent style is really high, & cognitive load is the development killer. So using tools to take care of style formatting is important.
How does it work?
Because black
isnât so picky about organization & formatting of import statements, I always recommend using the isort
tool as well.
And herein lie some of the subtleties. You know, There be Dragons & all that.
Not all that complicated once you know about them. But important to know, to avoid annoying errors.
With default settings, isort
& black
will compete over import statement formatting. Overwriting changes of the other. So the order & configuration options of the tools becomes important.
Hereâs my recommendation:
# on desktop to actually *apply* formatting changes before commit/push
isort \\
--profile black \\
build_harness/ \\
src/
black \\
build_harness/ \\
src/
# in a pipeline to *check* if formatting changes are needed
# note the `--check` option for each command.
isort \\
--check \\
--profile black \\
build_harness/ \\
src/
black \\
--check \\
build_harness/ \\
src/
Use
isort
first because its changes are not as extensive asblack
, plus it has an option to better accommodateblack
.black
does not have a similar option to supportisort
.Use the tools with as little customization as possible. Customization is simply a maintenance issue to be avoided. Someone will forget to replicate custom configuraiton accurately between projects, or simply decide they donât like one of them, or add their own.
For pipeline automation, only check if formatting is needed. The pipeline fails if it is because this means the developer needs to make a change.
Finally, thinking ahead a little to using static analysis tools:
Always apply formatting before static analysis as some of these tools also check formatting
The static analysis tool
flake8
support plugins & has one forblack
. Theflake8-black
plugin applies different formatting than defaultblack
& they interfere with each. So I recommend using eitherblack
orflake8-black
, not both. And preferablyblack
, not the plugin.
References
Want to modernise your Python workflow?
How about a 90 min session going over code youâre working on right now. Actionable recommendations on how to improve & make the development process faster & more effective. Get in touch!
Just a reminderâŚYouâre probably receiving this email because you subscribed to the Small Batches newsletter. Donât want to receive it anymore? Donât file it to spam. Use the unsubscribe link below! đ | Forwarded this post? Liked what you read & want more? |
I'm Russell đżď¸. A long time engineer discovering new life as a first-time solopreneur.
I build software delivery systems using Docker & Python automation that increase speed & reduce errors - saving money & improving profitability.
Reply