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

Intentional call chain with explicit parenthesis not respected #3100

Closed
scarf005 opened this issue Jun 4, 2022 · 6 comments
Closed

Intentional call chain with explicit parenthesis not respected #3100

scarf005 opened this issue Jun 4, 2022 · 6 comments
Labels
F: linebreak How should we split up lines? R: rejected This will not be worked on T: style What do we want Blackened code to look like?

Comments

@scarf005
Copy link

scarf005 commented Jun 4, 2022

Describe the bug

return (
    flu(files)
    .map(to_path)
    .flatten()
    .collect()  # type: ignore
)

this becomes

return flu(files).map(to_path).flatten().collect()  # type: ignore

this.

To Reproduce
Run above code with these arguments:

$ black --target-version py310 -- test.py

Expected behavior

intentional parenthesis around method chains should be respected and not break into single statement.

Environment

  • Black's version: main
  • OS and Python version: Linux/Python 3.10.4

Additional context

@scarf005 scarf005 added the T: bug Something isn't working label Jun 4, 2022
@JelleZijlstra JelleZijlstra added T: style What do we want Blackened code to look like? F: linebreak How should we split up lines? and removed T: bug Something isn't working labels Jun 4, 2022
@JelleZijlstra
Copy link
Collaborator

I'm inclined to keep the current behavior. Black generally tries to put things in one line where possible.

@Jacob-Stevens-Haas
Copy link

To perhaps paraphrase the argument, this is the call chain equivalent of the magic trailing comma

The problem is that call-chaining with periods at the end of the line, which is legal python, breaks for the final trailing period:

(
    numpy.
    array([3])
)  # works
(
    numpy.
    array([3]).
)  # SyntaxError

So the only way to tell black when you want to keep the call chain expanded vs when you want to be able to collapse it would be preceding vs trailing periods. But then collabsible-but-too-long call chains would have a different format than
code with intentionally expanded call chains:

(
    wont()
    .get()
    .collapsed()
    .unless()
    .some_lines()
    .are_deleted()
)

vs

(
    intentionally().
    left().
    expanded()
)

@felix-hilden
Copy link
Collaborator

felix-hilden commented Jun 9, 2022

I agree with Jelle, we don't want to take previous formatting into account any more than is absolutely necessary. Introducing another magic trailing comma does not sound good.

@felix-hilden felix-hilden added the R: rejected This will not be worked on label Jun 15, 2022
@felix-hilden
Copy link
Collaborator

With the maintainer rejection, I'll close this issue now.

@ichard26
Copy link
Collaborator

@maurosilber
Copy link

Couldn't the explicit parenthesis be like the trailing comma?

This:

(intentionally().left().expanded())

would format to:

(
    intentionally()
    .left()
    .expanded()
)

While something like this, without outer parenthesis,

intentionally(
).left(
).expanded()

would format to:

intentionally().left().expanded()

(if it fits in one line, of course)

In this case, the one line form is quite readable. But when we start to include parameters which are calls to other functions, it starts to get unreadable in one line:

intentionally(arg=func(x=1, y=2)).left(arg=f(3)).expanded()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linebreak How should we split up lines? R: rejected This will not be worked on T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

6 participants