black icon indicating copy to clipboard operation
black copied to clipboard

no opinion on tuple assignment parentheses?

Open matejcik opened this issue 7 years ago • 7 comments

This might be intentional but it feels like an omission, so I'm reporting it.

Does Black not have an opinion on parentheses around tuples, when assigning or returning? The following file remains untouched:

(a, b, c) = 1, 2, 3
a, b, c = (1, 2, 3)
return (d, e, f)
return 1, 2, 3

I would expect the optional parentheses would be removed in all above cases, per this: "In those cases, parentheses are removed when the entire statement fits in one line, or if the inner expression doesn't have any delimiters to further split on."

Operating system: Linux Mint Python version: 3.6 Black version: 18.6b4 Does also happen on master: yes

matejcik avatar Aug 13 '18 14:08 matejcik

Agreed, this is something black should do in my opinion as well

zsol avatar Aug 13 '18 14:08 zsol

Agreed, I have this on my internal TODO list.

ambv avatar Aug 17 '18 14:08 ambv

I agree, but I also think we should add parentheses in places like this where the multiple assignment is too long to fit in one line. This tends to be pretty common in our codebase due to the way http://github.com/quora/asynq makes us do batching.

So I would like to see @matejcik's first example become:

(
    a,
    b,
    c,
) = (
    1,
    2,
    3,
)

if the line otherwise becomes too long.

JelleZijlstra avatar Aug 19 '18 21:08 JelleZijlstra

I came here to report this because black will add, but not remove parentheses

Example:

value = some_really_really_really_long_variable_name_1, some_really_really_really_long_variable_name_2

becomes

value = (
    some_really_really_really_long_variable_name_1,
    some_really_really_really_long_variable_name_2,
)

but if you then shorten those names...

value = (
    name_1,
    name_2,
)

becomes

value = (name_1, name_2)

sr105 avatar Apr 25 '19 17:04 sr105

Just for clarity and to give another opinion, I'd expect the results to be:

# all fits line
a, b, c = (d, e, f)

# too long overall but LHS fits line
a, b, c = (
    d, e, f
)

# LHS too long but RHS fits line (always exploded fully)
(
    a,
    b,
    c,
) = (d, e, f)

Then if both are too long, we'd format as Jelle proposed, and then continue with the inner line breaks etc. Arguably, in the last case d,e,f could be exploded already. I'm not adamant on letting it be on a single line. But I think at least allowing the second case would be better.

felix-hilden avatar Nov 04 '21 07:11 felix-hilden

@felix-hilden could you please also clarify the following case when rhs is a function call?

first_item, second_item = some_looooooooong_module.some_looooooooooooooong_function_name(
    first_argument, second_argument, third_argument
)

Currently black does:

(
    first_item,
    second_item,
) = some_looooooooong_module.some_looooooooooooooong_function_name(
    first_argument, second_argument, third_argument
)

IMO the following is more readable:

first_item, second_item = (
    some_looooooooong_module.some_looooooooooooooong_function_name(
        first_argument, second_argument, third_argument
    )
)

This is too long overall but LHS fits line. It's neat that that ) = happens to be 4 characters equal to the indentation.

yilei avatar Oct 21 '22 23:10 yilei

I would agree!

felix-hilden avatar Oct 22 '22 07:10 felix-hilden