Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to align multiple assignments #194

Open
hayd opened this issue Oct 16, 2015 · 22 comments
Open

Option to align multiple assignments #194

hayd opened this issue Oct 16, 2015 · 22 comments

Comments

@hayd
Copy link
Contributor

hayd commented Oct 16, 2015

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.

@bwendling
Copy link
Member

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

@phb95
Copy link

phb95 commented Feb 26, 2016

This option would be useful to us

@bwendling
Copy link
Member

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

@straxhaber
Copy link

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.

@anoetos
Copy link

anoetos commented Jan 25, 2018

I would also very much appreciate that feature :-)

@mfrawley
Copy link

Would also love this feature

@rwarren
Copy link

rwarren commented May 6, 2019

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
Copy link

rwarren commented May 6, 2019

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

@gil-cohen
Copy link

this would be amazing for us as well

@r-barnes
Copy link

I definitely prefer vertical alignment of assignment.

@Alby407
Copy link

Alby407 commented Apr 25, 2020

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

@kamahen
Copy link
Contributor

kamahen commented Apr 26, 2020

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

@arita37
Copy link

arita37 commented Jun 22, 2021

alignment is a must for human reader of python code.

@hendrikboeck
Copy link

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

@bwendling
Copy link
Member

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

@arita37
Copy link

arita37 commented Mar 9, 2022 via email

@JackLilhammers
Copy link

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

@gsmethells
Copy link

Definitely a must have for readability.

@ufukty
Copy link

ufukty commented Nov 16, 2022

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

@arita37
Copy link

arita37 commented Jan 3, 2023

Please review it, after many years of waiting !
Thanks

@0017031
Copy link

0017031 commented Mar 24, 2023

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

@gsmethells
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests