psf/black

Make Readability your main focus.

maximilian22x opened this issue · 2 comments

Hello,

Why did you place the imports in different positions? The goal of the code is readability, which is lacking in your current format. Imports should be at the same position for better readability. This way, I can quickly recognize the imported modules. Compare my code style on the left with the Black code style on the right.

Which do you think offers better readability? I see no structure on the right Black site.

image

There has been discussion on similar things in the past, see #3341, #2385, and #682, but in general alignment is not something that Black seems to want to handle. This makes sense to me, as so much of Black is based around maximum line length limits, and the exact semantics of how to align code while respecting length limits is not trivial to decide or implement.

For existing code, you can always use # fmt: off and # fmt: on, playground example

Warning: shameless self promotion ahead I have recently released a tool called cargo-align that does fit this use case very well, by simply adding some alignment statements.
#fmt: off align_by "as"
import a as a
# align_by "import"
from b import b
# fmt: on

Currently it is fairly rust specific, however it does support passing in a path to be aligned, and I chose the syntax to be language agnostic.

Alignment the way you propose it interacts poorly with diffs. What happens for example, if you add an as import that is longer than import matplotlib.pyplot as plt? If we realign all the other ones to fit the new length, then your diff becomes very noisy. But if we don't do that, then the alignment becomes inconsistent.