Skip to content

List concatenation, multiple lines #260

Closed

Description

@sebdiem

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

Activity

  1. ambv commented on May 26, 2018

    @ambv
    Collaborator

    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.

  2. added
    T: enhancement
    on May 29, 2018
  3. e3krisztian commented on Jun 8, 2018

    @e3krisztian

    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?

  4. added
    help wanted
    on Aug 17, 2018
  5. added
    F: symmetry
    on Mar 3, 2020
  6. added
    T: style
    F: linebreak
    on May 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: linebreakF: symmetryT: enhancementT: stylehelp wanted

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions