Better Python formatting
Right now lb-format
uses YAPF to format Python files. I'd like to suggest moving to the black formatter instead.
Admittedly this idea was spurred by me not liking the YAPF formatting very much, but I think there are objective reasons for changing. In particular, black recently became an official Python project, so it's 'blessed' in some sense and we can probably expect it to become like go fmt
or clang-format
is terms of de-facto formatting.
I'd say that reading through the black README should be convincing enough; black's very pragmatic (allowing slightly longer lines than usual if it avoids hard wrapping) and follows the guidance of creating the smallest possible diffs between formatted changes, e.g. by appending a comma to the ends of multi-line lists.
One particularly ugly formatting that YAPF provides is of multi-item lists, e.g. imports:
from Hlt1Conf.reconstruction import (
- gec,
- RawData,
- FilterEmptyPVs,
- FTDecoding,
- make_forward_tracks,
- make_param_forward_fitted_tracks,
- make_pvs,
- EmptyFilter
-)
+ gec, RawData, FilterEmptyPVs, FTDecoding, make_forward_tracks,
+ make_param_forward_fitted_tracks, make_pvs, EmptyFilter)
This makes the imports harder to read as any particular object is lost amongst the noise.
YAPF's style is also rather jarring for function calls, where it can feel arbitrary:
-env = EverythingHandler(threadPoolSize = 1, nEventSlots = 1, evtMax = 1000, debug=True)
+env = EverythingHandler(
+ threadPoolSize=1, nEventSlots=1, evtMax=1000, debug=True)
If black cannot format on a single line, it will format one item per line. Black also removes surrounding parentheses when not needed, whereas YAPF will keep them.
This is naturally all subjective of course, but.. c'mon, look at what YAPF does!
Seriously though, I think black produces much more readable Python (leaving 'prettiness' aside) and I care about that a lot. Consistency is probably better than inconsistency, but I think consistency via YAPF hurts readability.