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

Parenthesizing a lambda's body if it contain line wraps #3606

Open
huzecong opened this issue Mar 14, 2023 · 0 comments
Open

Parenthesizing a lambda's body if it contain line wraps #3606

huzecong opened this issue Mar 14, 2023 · 0 comments
Labels
T: style What do we want Blackened code to look like?

Comments

@huzecong
Copy link

Describe the style change

It's possible to have line wraps in the body of a lambda if the lambda expression as a whole is parenthesized, or used in contexts such as function arguments. Currently black would give the wrapped lines the same indent as the line that lambda is on, which could cause confusion. I propose to have black automatically parenthesize the lambda's body so it could be further indented.

Examples in the current Black style

some_func = (
    lambda arg1, arg2: do_something_with_the_two_args(arg1, arg2)
    if arg2 is not None
    else do_something_with_only_one_arg(arg1)
)

some_function(
    fn=lambda arg1, arg2: do_something_with_the_two_args(arg1, arg2)
    if arg2 is not None
    else do_something_with_only_one_arg(arg1)
)

The current style is very confusing in my opinion, because it's hard to tell at first glance what the boundary of the lambda expression is. Since usually lambda expressions are single-line, one might be more likely to initially parse this as:

    (lambda arg1, arg2: do_something_with_the_two_args(arg1, arg2))
    if (arg2 is not None)
    else (do_something_with_only_one_arg(arg1))

Desired style

some_func = lambda arg1, arg2: (
    do_something_with_the_two_args(arg1, arg2)
    if arg2 is not None
    else do_something_with_only_one_arg(arg1)
)

some_function(
    fn=lambda arg1, arg2: (
        do_something_with_the_two_args(arg1, arg2)
        if arg2 is not None
        else do_something_with_only_one_arg(arg1)
    )
)

Parentheses are inserted inside the lambda's body, so it has an extra level of indentation that clearly marks the boundaries of the lambda.

Additional context

The function argument case might be similar to #2248 and #3510, both of which I also endorse.

@huzecong huzecong added the T: style What do we want Blackened code to look like? label Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

1 participant