From 7dfbdac298d43d5dd8b857a230aed4f035c83a97 Mon Sep 17 00:00:00 2001 From: cobalt <61329810+cobaltt7@users.noreply.github.com> Date: Fri, 11 Apr 2025 20:20:29 -0500 Subject: [PATCH 1/3] Keep parens around multiline strings in dicts Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> --- src/black/lines.py | 6 ++ tests/data/cases/preview_multiline_strings.py | 79 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/black/lines.py b/src/black/lines.py index 2a719def3c9..8dc1cb78f13 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -872,6 +872,12 @@ def is_line_short_enough( # noqa: C901 max_level_to_update = min(max_level_to_update, leaf.bracket_depth) if is_multiline_string(leaf): + if ( + leaf.parent + and leaf.parent.parent + and leaf.parent.parent.type == syms.dictsetmaker + ): + return False if len(multiline_string_contexts) > 0: # >1 multiline string cannot fit on a single line - force split return False diff --git a/tests/data/cases/preview_multiline_strings.py b/tests/data/cases/preview_multiline_strings.py index 1daf6f4d784..d4fdcde7d6d 100644 --- a/tests/data/cases/preview_multiline_strings.py +++ b/tests/data/cases/preview_multiline_strings.py @@ -182,6 +182,44 @@ def dastardly_default_value( expected: {expected_result} actual: {some_var}""" + +def foo(): + a = { + 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""" + }, + } + + +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""" + ), + "xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": ( + """ +a +a +a +a +a""" + ), + "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": """ +a +a +a +a +a""", +} + + # output """cow say""", @@ -399,3 +437,44 @@ def dastardly_default_value( assert some_var == expected_result, f""" expected: {expected_result} actual: {some_var}""" + + +def foo(): + a = { + 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""" + ), + }, + } + + +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""" + ), + "xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": ( + """ +a +a +a +a +a""" + ), + "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": ( + """ +a +a +a +a +a""" + ), +} From 6111860ff75129e3335e7eac0e0d2af00d0a5569 Mon Sep 17 00:00:00 2001 From: cobalt <61329810+cobaltt7@users.noreply.github.com> Date: Fri, 25 Apr 2025 20:00:52 -0500 Subject: [PATCH 2/3] Keep parens around ternaries with multiline strings in the value Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> --- src/black/lines.py | 8 +- tests/data/cases/preview_multiline_strings.py | 92 +++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index 8dc1cb78f13..c0f19667ec9 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -872,11 +872,11 @@ def is_line_short_enough( # noqa: C901 max_level_to_update = min(max_level_to_update, leaf.bracket_depth) if is_multiline_string(leaf): - if ( - leaf.parent - and leaf.parent.parent - and leaf.parent.parent.type == syms.dictsetmaker + if leaf.parent and ( + leaf.parent.type == syms.test + or (leaf.parent.parent and leaf.parent.parent.type == syms.dictsetmaker) ): + # Keep ternary and dictionary values parenthesized return False if len(multiline_string_contexts) > 0: # >1 multiline string cannot fit on a single line - force split diff --git a/tests/data/cases/preview_multiline_strings.py b/tests/data/cases/preview_multiline_strings.py index d4fdcde7d6d..e441fdc4ff8 100644 --- a/tests/data/cases/preview_multiline_strings.py +++ b/tests/data/cases/preview_multiline_strings.py @@ -219,8 +219,38 @@ def foo(): a""", } +a = """ +""" if """ +""" == """ +""" else """ +""" + +a = """ +""" if b else """ +""" + +a = """ +""" if """ +""" == """ +""" else b + +a = b if """ +""" == """ +""" else """ +""" + +a = """ +""" if b else c + +a = c if b else """ +""" + +a = b if """ +""" == """ +""" else c # output + """cow say""", call( @@ -478,3 +508,65 @@ def foo(): a""" ), } + +a = ( + """ +""" + if """ +""" + == """ +""" + else """ +""" +) + +a = ( + """ +""" + if b + else """ +""" +) + +a = ( + """ +""" + if """ +""" + == """ +""" + else b +) + +a = ( + b + if """ +""" + == """ +""" + else """ +""" +) + +a = ( + """ +""" + if b + else c +) + +a = ( + c + if b + else """ +""" +) + +a = ( + b + if """ +""" + == """ +""" + else c +) From 70a3ece1f493effdde626b8e9d6e85d53371a8c1 Mon Sep 17 00:00:00 2001 From: cobalt <61329810+cobaltt7@users.noreply.github.com> Date: Fri, 25 Apr 2025 20:44:47 -0500 Subject: [PATCH 3/3] Update changelog Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index b7520f3f93a..73c21046558 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ - Fix a bug where one-liner functions/conditionals marked with `# fmt: skip` would still be formatted (#4552) +- Improve `multiline_string_handling` with ternaries and dictionaries (#4657) ### Configuration