black icon indicating copy to clipboard operation
black copied to clipboard

Unnecessary split in left hand side of assignment due to multiline string

Open matejbasic opened this issue 6 years ago • 2 comments

Hi, I'm experience strange behavior when I use dicts and triple-quotes strings, for example:

a["b"] = f"""Some really long
    long string."""

ends up like this:

a[
   "b"
] = f"""Some really long
    long string."""

Is it expected behavior or? If yes, how can one disable it?

Operating system: Linux Ubuntu 18.04 Python version: 3.6 Black version: 18.9b0 Does also happen on master: yes

matejbasic avatar Mar 14 '19 15:03 matejbasic

It's a bug. It stems from Black seeing that the right hand side doesn't fit in a line (it doesn't, it's a multiline string!) and tries to split the line by any other means.

In this case, it should recognize that the split made matters worse and abort. But that's not super easy to do at this point. We'll be fixing this though.

ambv avatar Mar 14 '19 16:03 ambv

This also happens to the right side:

a = """Not a so long string
  but multiline.""" + b.call(c=1)

is formatted to:

a = """Not a so long string
  but multiline.""" + b.call(
    c=1
)

even though it fits well within the line length.

yilei avatar Oct 11 '22 20:10 yilei

Duplicate of #236 and #256.

JelleZijlstra avatar Dec 19 '22 04:12 JelleZijlstra