yapf icon indicating copy to clipboard operation
yapf copied to clipboard

Option to align multiple assignments

Open hayd opened this issue 9 years ago • 22 comments

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.

hayd avatar Oct 16 '15 17:10 hayd

I would like to know how often this is done before putting a lot of effort into it...:-)

bwendling avatar Oct 19 '15 04:10 bwendling

This option would be useful to us

phb95 avatar Feb 26 '16 16:02 phb95

As long as it remained an option, I'd be fine with it. If someone would like to contribute it, that would be keen. :-)

bwendling avatar Feb 26 '16 19:02 bwendling

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.

straxhaber avatar Jul 31 '17 19:07 straxhaber

I would also very much appreciate that feature :-)

anoetos avatar Jan 25 '18 13:01 anoetos

Would also love this feature

mfrawley avatar Jun 15 '18 18:06 mfrawley

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.

rwarren avatar May 06 '19 04:05 rwarren

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:

  • There are many potential symbols/tokens to align on...
    • All operators for sure.
    • Most of the assignment delimiters, too. i.e. =, +=, |=, >>=, etc... but not delimiters used for literal definitions (all bracket forms, :, ., etc)
  • line width changing such as converting (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.
  • Starting with simple alignment of = would be great, with possible expansion to aligning other tokens left as a future option/pipe-dream.
  • # yapf: enable/disable works well enough for now

rwarren avatar May 06 '19 14:05 rwarren

this would be amazing for us as well

gil-cohen avatar Nov 10 '19 14:11 gil-cohen

I definitely prefer vertical alignment of assignment.

r-barnes avatar Feb 27 '20 02:02 r-barnes

After 5 years, this is still an open issue...

Alby407 avatar Apr 25 '20 12:04 Alby407

"After 5 years, this is still an open issue..." -- yapf is Open Source, and no doubt a contribution would be accepted.

kamahen avatar Apr 26 '20 00:04 kamahen

alignment is a must for human reader of python code.

arita37 avatar Jun 22 '21 00:06 arita37

Is somebody currently working on this feature? Otherwise I would try implementing it.

hendrikboeck avatar Mar 09 '22 08:03 hendrikboeck

I don't think anyone's working on it at the moment. Please give it a shot! :-)

bwendling avatar Mar 09 '22 08:03 bwendling

Overall , having easy to human read code is beneficial to spot bugs/ potential issues quickly

On Mar 9, 2022, at 17:37, Bill Wendling @.***> wrote:

 I don't think anyone's working on it at the moment. Please give it a shot! :-)

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you commented.

arita37 avatar Mar 09 '22 11:03 arita37

This would be awesome! I'm absolutely ignorant about automatic formatting, but I'd like to help if someone is working on this

JackLilhammers avatar Aug 30 '22 09:08 JackLilhammers

Definitely a must have for readability.

gsmethells avatar Nov 15 '22 22:11 gsmethells

As I see, last PRs made this functionality available and are ready to review. Wonder if reviewers have seen it yet

ufukty avatar Nov 16 '22 07:11 ufukty

Please review it, after many years of waiting ! Thanks

arita37 avatar Jan 03 '23 05:01 arita37

In the Perl world, perltidy do well on the assignment align.

0017031 avatar Mar 24 '23 05:03 0017031

Please review the PRs for this issue. The features they provide would allow use to implement our style guide in yapf other than the vertical alignment of import statments using the "from X import A, B, C" style.

gsmethells avatar May 29 '23 04:05 gsmethells