yapf icon indicating copy to clipboard operation
yapf copied to clipboard

Preserve arguments list aligned to an opening bracket

Open prokher opened this issue 7 years ago • 8 comments

I have the following complain. This

redis = await create_subprocess_exec("Redis", "/usr/local/bin/redis-server",
                                     "--bind", str(host), "--port", str(redis_port))

becomes this:

  redis = await create_subprocess_exec(
    "Redis", "/usr/local/bin/redis-server", "--bind", str(host), "--port", str(redis_port)
  )

I would like to keep the first variant (where I align code to opening bracket and line brakes are set by some logic), but cannot find proper setting, is that possible?

I have tried to modify split_before_named_assigns, but this did not help...

prokher avatar Feb 10 '17 09:02 prokher

I'm interested in this as well.

It looks like CONTINUATION_INDENT_WIDTH should have some sort of alignment option, rather than just a value.

spott avatar Feb 24 '17 01:02 spott

+1

eskhool avatar Mar 09 '17 09:03 eskhool

+1

rdburns avatar Mar 13 '18 13:03 rdburns

Aligning with the opening delimiter is supported by PEP8, and it is the de facto standard within much of the Python standard library. +1

ryanavella avatar Jul 19 '18 15:07 ryanavella

Any comments / progress on this? I much prefer this style -- I really wish there were a YAPF option for it :-(

PythonCHB avatar Mar 24 '19 06:03 PythonCHB

YAPF tends to prefer placing as many things on a single line as possible. When I test your example above, I'm not able to get that behavior (at least not with top-of-tree YAPF). You might want to look at SPLIT_BEFORE_FIRST_ARGUMENT though.

bwendling avatar Mar 24 '19 08:03 bwendling

I"m not the OP, but what I want is a lot of arguments (or parameters) to be formatted like this:

a_call_with_lots_of_arguments(arg1,
                              arg2,
                              arg3,
                              kwarg1=something,
                              kwarg2=something_else,
                              )

which YAPF wants to format like so:

a_call_with_lots_of_arguments(
    arg1,
    arg2,
    arg3,
    kwarg1=something,
    kwarg2=something_else,
)

As I read PEP8, either of those two are acceptable, as well as:

a_call_with_lots_of_arguments(arg1, arg2, arg3, kwarg1=something,
                              kwarg2=something_else,
                              )

which I personally don't like -- but it would be nice if YAPF provided all three options. Maybe it does, but I couldn't find the magic incantation.

PythonCHB avatar Mar 24 '19 08:03 PythonCHB

I think as of now, the pep8 style outputs what @prokher wanted:

redis = await create_subprocess_exec("Redis", "/usr/local/bin/redis-server",
                                     "--bind", str(host), "--port", str(redis_port))

And for that to become this:

redis = await create_subprocess_exec(
    "Redis", "/usr/local/bin/redis-server", "--bind", str(host), "--port", str(redis_port)
)

the setting split_before_expression_after_opening_paren must be set to true (which is the default for yapf style).

LECbg avatar Jun 30 '21 15:06 LECbg