Skip to content

Commit 89f3610

Browse files
authored
Improve multiline_string_handling with ternaries and dictionaries (#4657)
1 parent a8de14f commit 89f3610

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
- Fix a bug where one-liner functions/conditionals marked with `# fmt: skip` would still
3131
be formatted (#4552)
32+
- Improve `multiline_string_handling` with ternaries and dictionaries (#4657)
3233
- Fix a bug where `string_processing` would not split f-strings directly after
3334
expressions (#4680)
3435

src/black/lines.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,12 @@ def is_line_short_enough( # noqa: C901
872872
max_level_to_update = min(max_level_to_update, leaf.bracket_depth)
873873

874874
if is_multiline_string(leaf):
875+
if leaf.parent and (
876+
leaf.parent.type == syms.test
877+
or (leaf.parent.parent and leaf.parent.parent.type == syms.dictsetmaker)
878+
):
879+
# Keep ternary and dictionary values parenthesized
880+
return False
875881
if len(multiline_string_contexts) > 0:
876882
# >1 multiline string cannot fit on a single line - force split
877883
return False

tests/data/cases/preview_multiline_strings.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,75 @@ def dastardly_default_value(
182182
expected: {expected_result}
183183
actual: {some_var}"""
184184

185+
186+
def foo():
187+
a = {
188+
xxxx_xxxxxxx.xxxxxx_xxxxxxxxxxxx_xxxxxx_xx_xxx_xxxxxx: {
189+
"xxxxx": """Sxxxxx xxxxxxxxxxxx xxx xxxxx (xxxxxx xxx xxxxxxx)""",
190+
"xxxxxxxx": (
191+
"""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
192+
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
193+
),
194+
"xxxxxxxx": """Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
195+
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
196+
},
197+
}
198+
199+
200+
xxxx_xxxxxxx.xxxxxx_xxxxxxxxxxxx_xxxxxx_xx_xxx_xxxxxx = {
201+
"xxxxx": """Sxxxxx xxxxxxxxxxxx xxx xxxxx (xxxxxx xxx xxxxxxx)""",
202+
"xxxxxxxx": (
203+
"""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
204+
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
205+
),
206+
"xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
207+
"""
208+
a
209+
a
210+
a
211+
a
212+
a"""
213+
),
214+
"xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": """
215+
a
216+
a
217+
a
218+
a
219+
a""",
220+
}
221+
222+
a = """
223+
""" if """
224+
""" == """
225+
""" else """
226+
"""
227+
228+
a = """
229+
""" if b else """
230+
"""
231+
232+
a = """
233+
""" if """
234+
""" == """
235+
""" else b
236+
237+
a = b if """
238+
""" == """
239+
""" else """
240+
"""
241+
242+
a = """
243+
""" if b else c
244+
245+
a = c if b else """
246+
"""
247+
248+
a = b if """
249+
""" == """
250+
""" else c
251+
185252
# output
253+
186254
"""cow
187255
say""",
188256
call(
@@ -399,3 +467,106 @@ def dastardly_default_value(
399467
assert some_var == expected_result, f"""
400468
expected: {expected_result}
401469
actual: {some_var}"""
470+
471+
472+
def foo():
473+
a = {
474+
xxxx_xxxxxxx.xxxxxx_xxxxxxxxxxxx_xxxxxx_xx_xxx_xxxxxx: {
475+
"xxxxx": """Sxxxxx xxxxxxxxxxxx xxx xxxxx (xxxxxx xxx xxxxxxx)""",
476+
"xxxxxxxx": (
477+
"""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
478+
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
479+
),
480+
"xxxxxxxx": (
481+
"""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
482+
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
483+
),
484+
},
485+
}
486+
487+
488+
xxxx_xxxxxxx.xxxxxx_xxxxxxxxxxxx_xxxxxx_xx_xxx_xxxxxx = {
489+
"xxxxx": """Sxxxxx xxxxxxxxxxxx xxx xxxxx (xxxxxx xxx xxxxxxx)""",
490+
"xxxxxxxx": (
491+
"""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
492+
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
493+
),
494+
"xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
495+
"""
496+
a
497+
a
498+
a
499+
a
500+
a"""
501+
),
502+
"xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
503+
"""
504+
a
505+
a
506+
a
507+
a
508+
a"""
509+
),
510+
}
511+
512+
a = (
513+
"""
514+
"""
515+
if """
516+
"""
517+
== """
518+
"""
519+
else """
520+
"""
521+
)
522+
523+
a = (
524+
"""
525+
"""
526+
if b
527+
else """
528+
"""
529+
)
530+
531+
a = (
532+
"""
533+
"""
534+
if """
535+
"""
536+
== """
537+
"""
538+
else b
539+
)
540+
541+
a = (
542+
b
543+
if """
544+
"""
545+
== """
546+
"""
547+
else """
548+
"""
549+
)
550+
551+
a = (
552+
"""
553+
"""
554+
if b
555+
else c
556+
)
557+
558+
a = (
559+
c
560+
if b
561+
else """
562+
"""
563+
)
564+
565+
a = (
566+
b
567+
if """
568+
"""
569+
== """
570+
"""
571+
else c
572+
)

0 commit comments

Comments
 (0)