Skip to content

Improve multiline_string_handling with ternaries and dictionaries #4657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.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
return False
Expand Down
171 changes: 171 additions & 0 deletions tests/data/cases/preview_multiline_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,75 @@ 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""",
}

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(
Expand Down Expand Up @@ -399,3 +467,106 @@ 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"""
),
}

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
)
Loading