When you first encounter Black, a few things about it might surprise you. One of the those things might be

Why the sad face? - Łukasz Langa

submited by
Style Pass
2021-05-24 18:00:02

When you first encounter Black, a few things about it might surprise you. One of the those things might be "sadface dedent", the style in which closing parentheses in function signatures and other block headers are put on its own line. I arrived at this formatting style long before creating the auto-formatter. It’s got a few objective advantages.

The argument here goes like this: you already close all other bracket pairs in this manner. You do it for multi-line list and dictionary literals:

Did you notice? All of the code snippets above come from real-world Python projects. None were written by me and none are auto-formatted by Black. The point I’m making here is that this style isn’t some Black-specific eccentricity. And while I came up with it on my own in my programming journey, so did many other developers.

Due to the number of parentheses in the if test, it’s not obvious visually what the closing parenthesis right before the colon pairs with. It would also be impossible to see where the if test ends without awkwardly 1-indenting the second line of the test to hint that this is not part of the body. Can we do better? Let’s see what Black would do:

Leave a Comment