Skip to content
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

Preserve visible quote types for f-string debug expressions #4005

Merged
merged 18 commits into from Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -17,6 +17,7 @@
indented less (#3964)
- Multiline list and dict unpacking as the sole argument to a function is now also
indented less (#3992)
- Preserve printable quote types for debug f-strings (#4005)
henriholopainen marked this conversation as resolved.
Show resolved Hide resolved

### Configuration

Expand Down
19 changes: 14 additions & 5 deletions src/black/trans.py
Expand Up @@ -590,11 +590,20 @@ def make_naked(string: str, string_prefix: str) -> str:
"""
assert_is_leaf_string(string)
if "f" in string_prefix:
string = _toggle_fexpr_quotes(string, QUOTE)
# After quotes toggling, quotes in expressions won't be escaped
# because quotes can't be reused in f-strings. So we can simply
# let the escaping logic below run without knowing f-string
# expressions.
RE_QUOTES_IN_DEBUG_F_STRING = (
r"{[^[]*\[\s*[\"\']\w*[\"\']\s*\][^=}]*=[^}]*}"
)
is_debug_f_string_with_quotes = re.search(
RE_QUOTES_IN_DEBUG_F_STRING, string
)
if not is_debug_f_string_with_quotes:
# We don't want to toggle debug f-string quotes in the printable
# part, because that would modify the AST
string = _toggle_fexpr_quotes(string, QUOTE)
# After quotes toggling, quotes in expressions won't be escaped
# because quotes can't be reused in f-strings. So we can simply
# let the escaping logic below run without knowing f-string
# expressions.

RE_EVEN_BACKSLASHES = r"(?:(?<!\\)(?:\\\\)*)"
naked_string = string[len(string_prefix) + 1 : -1]
Expand Down
27 changes: 27 additions & 0 deletions tests/data/cases/preview_long_strings.py
Expand Up @@ -305,6 +305,13 @@ def foo():
# Test case of an outer string' parens enclose an inner string's parens.
call(body=("%s %s" % ((",".join(items)), suffix)))

log.info(f'Skipping: {desc["db_id"]=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]=} {desc["exposure_max"]=}')

log.info(f"Skipping: {desc['db_id']=} {desc['ms_name']} {money=} {dte=} {pos_share=} {desc['status']=} {desc['exposure_max']=}")

log.info(f'Skipping: {desc["db_id"]} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}')

log.info(f'Skipping: { longer_longer_longer_longer_longer_longer_name [ "db_id" ] [ "another_key" ] = : .3f }')

henriholopainen marked this conversation as resolved.
Show resolved Hide resolved
# output

Expand Down Expand Up @@ -846,3 +853,23 @@ def foo():

# Test case of an outer string' parens enclose an inner string's parens.
call(body="%s %s" % (",".join(items), suffix))

log.info(
"Skipping:"
f' {desc["db_id"]=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]=} {desc["exposure_max"]=}'
)

log.info(
"Skipping:"
f" {desc['db_id']=} {desc['ms_name']} {money=} {dte=} {pos_share=} {desc['status']=} {desc['exposure_max']=}"
)

log.info(
"Skipping:"
f" {desc['db_id']} {desc['ms_name']} {money=} {dte=} {pos_share=} {desc['status']} {desc['exposure_max']}"
)

log.info(
"Skipping:"
f' { longer_longer_longer_longer_longer_longer_name [ "db_id" ] [ "another_key" ] = : .3f }'
)