Skip to content

multiline_string_handling sometimes leads to worse formatting #4159

@JelleZijlstra

Description

@JelleZijlstra

Reports from @amyreese in #4042 (comment):

# before 
Thing(
    """
    value
    """
)

# after
Thing("""
    value
    """)
# before
VALUE = [
    Thing(
        """
        value
        """
    )
]

# after
VALUE = [Thing("""
    value
    """)]
# before
func(
    (
        value
        + """
        something
        """
    ),
    argument,
)

# after
func(
    (value + """
        something
        """),
    argument,
)
# before
def foo():
    result = (
        """
        something
        """
        + value
    )

# after
def foo():
    result = """
        something
        """ + value

I just enabled only multiline_string_handling and ran Black on my work codebase. Many changes are improvements, but quite a few look uglier.

Some examples:

         xxxx_xxxxxxx.xxxxxx_xxxxxxxxxxxx_xxxxxx_xx_xxx_xxxxxx: {
             "xxxxx": """Sxxxxx xxxxxxxxxxxx xxx xxxxx (xxxxxx xxx xxxxxxx)""",
-            "xxxxxxxx": (
-                """Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx 
-                xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
-            ),
+            "xxxxxxxx": ("""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx 
+                xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""),
         },
         for xxx_xxxx, xxx_xxxx, xxx_xxxxxxx, xxx_xxxxxxx in xxxxxx_xxxxx:
-            xxxx = (
-                x"""
+            xxxx = x"""
                 ({x.x18x.xxx.xxxxxx_xxxxxx(xxx_xxxx)} xx {x.x18x.xxx.xxxxxx_xxxxxx(xxx_xxxx)})
-                """
-                if xxx_xxxx != xxx_xxxx
-                else x.x18x.xxx.xxxxxx_xxxxxx(xxx_xxxx)
-            )
+                """ if xxx_xxxx != xxx_xxxx else x.x18x.xxx.xxxxxx_xxxxxx(xxx_xxxx)

(Harder to notice the ternary)

cc @aneeshusa as the author of #1879

I'm inclined to remove this feature from the 2024 stable style given this feedback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: preview styleIssues with the preview and unstable style. Add the name of the responsible feature in the title.T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions