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

Formatting of conditional expressions in named function arguments #2248

Open
JEphron opened this issue May 20, 2021 · 1 comment
Open

Formatting of conditional expressions in named function arguments #2248

JEphron opened this issue May 20, 2021 · 1 comment
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. T: style What do we want Blackened code to look like?

Comments

@JEphron
Copy link
Contributor

JEphron commented May 20, 2021

When formatting conditional expressions in function calls with named arguments, Black's formatting varies significantly depending on whether the expression is surrounded by parenthesis.

Exhibit A:

Black considers this correctly formatted

aaa = my_function(
    foo="test, this is a sample value",
    bar=some_long_value_name_foo_bar_baz
    if some_boolean_variable
    else some_fallback_value_foo_bar_baz,
    baz="hello, this is a another value",
)

Exhibit B

When fed the following block as input:

aaa = my_function(
    foo="hello, this is a sample value",
    bar=(some_long_value_name_foo_bar_baz
    if some_boolean_variable
    else some_fallback_value_foo_bar_baz),
    baz="hello, this is a sample value",
)

Black formats it with a much nicer indentation

aaa = my_function(
    foo="hello, this is a sample value",
    bar=(
        some_long_value_name_foo_bar_baz
        if some_boolean_variable
        else some_fallback_value_foo_bar_baz
    ),
    baz="hello, this is a sample value",
)

I consider the second output far more readable. In exhibit A the if expression lies on the same indent as the kwargs, making it hard to tell what's going on. In the second, it's quite clear that some computation is spanning those three lines.

I think ideally Black would never produce Exhibit A, rather it would insert the parenthesis in order to indent the conditional expression.

In keeping with the opinionated style, we should pick one or the other, but probably not allow both. Even if Black decided to strip parenthesis and produce exhibit A all of the time, it'd be preferable to the current ambiguity.

@TylerYep
Copy link

TylerYep commented May 22, 2021

+1 that Exhibit B's end result is much easier to read!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. T: style What do we want Blackened code to look like?
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants