Often the following convention is used:
a = 1
bb = 2
ccc = 3
whilst this is against pep8, it seems like this would be a nice option to support for those people that are (forced??) to use this convention.
I would like to know how often this is done before putting a lot of effort into it...:-)
This option would be useful to us
As long as it remained an option, I'd be fine with it. If someone would like to contribute it, that would be keen. :-)
Has anyone built a fork that includes this? If not, I might consider building it as I think vertical alignment of assignments makes for easier to read code.
I would also very much appreciate that feature :-)
Would also love this feature
This knob/option is sorely needed. If anyone understands the utility/significance of clean vertical alignment through the use of whitespace, it should be python programmers! :)
Detecting aligned equals signs in a code block doesn't seem like it would be that difficult (especially since comments can already be aligned within a block). However, there are other vertical alignment situations, such as columnar alignment in embedded data/dispatch tables, that might be tricky to achieve unless 100% consistent (although that is obviously out of scope for this issue).
Until this issue is resolved (if ever), a workaround for anyone arriving here is to suppress yapf formatting with the use of comment directives around the block:
# yapf: disable
C_CHED = 1234
C_BRIE = 5678
C_3 = 9012
C_4 = 3456
my_lookup = (
# function foo bar baz qux
# -------- --- --- --- ---
(my_func1, False, "cheddar", 1,12, C_CHED),
(some_other_fn, False, "brie", 3.19, C_BRIE),
(f2, True, "camembert", 0.57, C_3),
(your_fn3, False, "stilton", 9.99, C_4),
)
# yapf: enable
Without these directives, yapf completely mangles such alignments (and so does any other formatter I can find).
Adding the comment clutter is a bit frustrating, but there are definitely many cases when this is better than mangling the alignment.
In case this does ever get worked on (even if by me), I'll add some more notes...
It would be nice to go further and preserve alignment for more than just equals signs. Maybe an option along the lines of this:
PRESERVE_VERTICALLY_ALIGNED_SYMBOLS = true
When enabled, yapf could try and detect and preserve alignments such as this:
len1 = (foo + 12) - somevar
len2 = (longer_var + 19) - x
l3 = (an_even_longer_var + 19) - x
l3 = (yyy + 4) - a_really_long_var_name_that_is_right_here
However, this could get confusing/complex if different symbols are used (+ vs -), or with commented lines (which I think should be ignored in a contiguous line block), or alternative object constructs...
len1 = (foo + 12) - somevar
#len2 = (longer_var + 19) - x
l3 = (an_even_longer_var + 19) - x
l3 = ((yyy + 9) + 4) + a_really_long_var_name_that_is_right_here
My opinion on that example block is that it would still be be untouched with the alignment feature enabled. Reasons being: A) the commented line should be ignored from an alignment perspective (could be a pure comment); B) the the + and - could be recognized as symbols for alignment, and; C) the nested (yyy + 9) doesn't have symbols that consistently align across the block so aren't part of any alignment consideration.
Additional notes/ideas:
=, +=, |=, >>=, etc... but _not_ delimiters used for literal definitions (all bracket forms, :, ., etc)(x+1) to (x + 1) would alter alignment, so any alignment detection (and any stashing of in-line alignment positions) would have to be determined in advance.= would be great, with possible expansion to aligning other tokens left as a future option/pipe-dream.# yapf: enable/disable works well enough for nowthis would be amazing for us as well
I definitely prefer vertical alignment of assignment.
After 5 years, this is still an open issue...
"After 5 years, this is still an open issue..." -- yapf is Open Source, and no doubt a contribution would be accepted.
Most helpful comment
"After 5 years, this is still an open issue..." -- yapf is Open Source, and no doubt a contribution would be accepted.