yapf: Options for preferring hanging indents with one item per line when formating lists

I would like to add options/code so that yapf can be configured to prefer outputting something like

MyLongNamedTuple = (
    something.somethingelse,
    something.somethingelse,
    foo=(
        bar.somethingelse,
        something.asdfas,
    ),
)

over

MyLongNamedTuple = ( something.somethingelse,
    something.somethingelse, foo=( bar.somethingelse,
    something.asdfas),)

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 15
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Any news regarding this feature? We are heavy user of vertical lists and it would be a much appreciated option to have.

Probably the best thing that can be done here is to use the heuristic that if the list ends in a comma then we will try to place them on individual lines. Otherwise, we will do “bin packing” on the items in the list.

MyLongNamedTuple = [
    something.somethingelse,
    something.somethingelse,
    [
        bar.somethingelse,
        something.asdfas,
    ],
]

as opposed to:

MyLongNamedTuple = [
    something.somethingelse, something.somethingelse, [
        bar.somethingelse, something.asdfas
    ]
]

I feel like a list should read EITHER left to right OR Up to down but not both, and I think its worth the trade off in vertical space. Of course anything that can fit on a single reasonable line, should be left on a single line. For example if the variables in foo in the above example where bar and asdf then even on the vertically stacked example i would want foo to be formatted as

foo=(bar, asdf)

not

foo=(
    bar,
    asdf,
)

Not everyone would agree with keeping lists 1 dimentional, so I would want this to be tune-able in style.

I feel like a list should read EITHER left to right OR Up to down but not both

👍

I would love a configuration option to make comma-separated things always be either

  1. all on one line
  2. one item per line