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

List concatenation, multiple lines #260

Open
sebdiem opened this issue May 26, 2018 · 2 comments
Open

List concatenation, multiple lines #260

sebdiem opened this issue May 26, 2018 · 2 comments
Labels
F: linebreak How should we split up lines? F: symmetry Fixing this would require Black to understand symmetry. help wanted Extra attention is needed T: enhancement New feature or request T: style What do we want Blackened code to look like?

Comments

@sebdiem
Copy link

sebdiem commented May 26, 2018

Probably not a bug, but I was wondering if the code below shouldn't be formatted more like call chains are.
I did not expect black to change anything other than quotes in this example.
Aligned brackets feel much more readable.

# before
-    search_fields = (
-        ['file__%s' % field for field in FileAdmin.search_fields] +
-        ['resource__%s' % field for field in ResourceAdmin.search_fields]
-    )
# black output
+    search_fields = ["file__%s" % field for field in FileAdmin.search_fields] + [
+        "resource__%s" % field for field in ResourceAdmin.search_fields
+    ]

Operating system: OSX 10.11.6
Python version: 3.6.5
Black version: 18.5b0
Does also happen on master: yes

@ambv
Copy link
Collaborator

ambv commented May 26, 2018

Agreed with you on this particular case. It's a case of over-optimization that we decided to do due to a popular idiom in assignments and if statements:

  • if there is just a single operator; and
  • the line up to and including the operator is short enough; and
  • the rest ends with a bracket (so we have something to split on); then

omit optional parentheses and split by the discovered bracket instead.

We should extend this rule to discover symmetrical cases like yours.

@ambv ambv added the T: enhancement New feature or request label May 29, 2018
@e3krisztian
Copy link

Maybe this rule should be eliminated instead?

The extra bracket has a purpose: it provides the line continuation, but unfortunately it also costs an extra line. However if it is not there, some other bracket needs to provide line continuation, which is quite awkward looking.

I have just added some more examples in #317 with reasoning why the popular idiom is bad in general. But maybe I miss something, is there any example, where this idiom produces a good result?

@ambv ambv added the help wanted Extra attention is needed label Aug 17, 2018
@ambv ambv added the F: symmetry Fixing this would require Black to understand symmetry. label Mar 3, 2020
@JelleZijlstra JelleZijlstra added T: style What do we want Blackened code to look like? F: linebreak How should we split up lines? labels May 30, 2021
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? F: symmetry Fixing this would require Black to understand symmetry. help wanted Extra attention is needed T: enhancement New feature or request T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

4 participants