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

no opinion on tuple assignment parentheses? #445

Open
matejcik opened this issue Aug 13, 2018 · 7 comments
Open

no opinion on tuple assignment parentheses? #445

matejcik opened this issue Aug 13, 2018 · 7 comments
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. T: enhancement New feature or request

Comments

@matejcik
Copy link

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

@zsol zsol added the T: style What do we want Blackened code to look like? label Aug 13, 2018
@zsol
Copy link
Collaborator

zsol commented Aug 13, 2018

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

@ambv ambv added T: enhancement New feature or request and removed T: style What do we want Blackened code to look like? labels Aug 17, 2018
@ambv
Copy link
Collaborator

ambv commented Aug 17, 2018

Agreed, I have this on my internal TODO list.

@JelleZijlstra
Copy link
Collaborator

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.

@sr105
Copy link

sr105 commented Apr 25, 2019

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)

@felix-hilden
Copy link
Collaborator

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.

@yilei
Copy link
Contributor

yilei commented Oct 21, 2022

@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.

@felix-hilden
Copy link
Collaborator

I would agree!

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: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants