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

Prevent f-string merge quote changes with nested quotes #4498

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

<!-- Changes that affect Black's preview style -->

- Fix/remove string merging changing f-string quotes (#4498)

### Configuration

<!-- Changes to how Black can be configured -->
Expand Down
9 changes: 5 additions & 4 deletions docs/the_black_code_style/future_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ foo(

_Black_ will split long string literals and merge short ones. Parentheses are used where
appropriate. When split, parts of f-strings that don't need formatting are converted to
plain strings. User-made splits are respected when they do not exceed the line length
limit. Line continuation backslashes are converted into parenthesized strings.
Unnecessary parentheses are stripped. The stability and status of this feature is
tracked in [this issue](https://github.com/psf/black/issues/2188).
plain strings. f-strings will not be merged if it would change their quotation mark
style. User-made splits are respected when they do not exceed the line length limit.
Line continuation backslashes are converted into parenthesized strings. Unnecessary
parentheses are stripped. The stability and status of this feature istracked in
[this issue](https://github.com/psf/black/issues/2188).

(labels/wrap-long-dict-values)=

Expand Down
8 changes: 8 additions & 0 deletions src/black/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ def _validate_msg(line: Line, string_idx: int) -> TResult[None]:
- The set of all string prefixes in the string group is of
length greater than one and is not equal to {"", "f"}.
- The string group consists of raw strings.
- The string group merge would change f-string quote style.
- The string group is stringified type annotations. We don't want to
process stringified type annotations since pyright doesn't support
them spanning multiple string values. (NOTE: mypy, pytype, pyre do
Expand All @@ -820,6 +821,8 @@ def _validate_msg(line: Line, string_idx: int) -> TResult[None]:

i += inc

QUOTE = line.leaves[string_idx].value[-1]

num_of_inline_string_comments = 0
set_of_prefixes = set()
num_of_strings = 0
Expand All @@ -842,6 +845,11 @@ def _validate_msg(line: Line, string_idx: int) -> TResult[None]:

set_of_prefixes.add(prefix)

if "f" in prefix and leaf.value[-1] != QUOTE:
MeGaGiGaGon marked this conversation as resolved.
Show resolved Hide resolved
return TErr(
"StringMerger does NOT change f-string quote style on merge."
)

if id(leaf) in line.comments:
num_of_inline_string_comments += 1
if contains_pragma_comment(line.comments[id(leaf)]):
Expand Down
2 changes: 2 additions & 0 deletions tests/data/cases/preview_fstring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# flags: --unstable
f"{''=}" f'{""=}'
14 changes: 0 additions & 14 deletions tests/data/cases/preview_long_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,12 @@ def foo():

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"]} {foo("bar",x=123)} {"foo" != "bar"} {(x := "abc=")} {pos_share=} {desc["status"]} {desc["exposure_max"]}')
MeGaGiGaGon marked this conversation as resolved.
Show resolved Hide resolved

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

log.info(f'Skipping: {desc["db_id"]} {foo("bar",x=123)=} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}')

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

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

log.info(f'Skipping: {"a" == "b" == "c" == "d"=} {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 }')
Expand Down Expand Up @@ -880,11 +876,6 @@ def foo():
f" {desc['db_id']=} {desc['ms_name']} {money=} {dte=} {pos_share=} {desc['status']=} {desc['exposure_max']=}"
)

log.info(
"Skipping:"
f" {desc['db_id']} {foo('bar',x=123)} {'foo' != 'bar'} {(x := 'abc=')} {pos_share=} {desc['status']} {desc['exposure_max']}"
)

log.info(
"Skipping:"
f' {desc["db_id"]} {desc["ms_name"]} {money=} {(x := "abc=")=} {pos_share=} {desc["status"]} {desc["exposure_max"]}'
Expand All @@ -900,11 +891,6 @@ def foo():
f' {foo("asdf")=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}'
)

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

log.info(
"Skipping:"
f' {"a" == "b" == "c" == "d"=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}'
Expand Down
18 changes: 0 additions & 18 deletions tests/data/cases/preview_long_strings__regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,6 @@ async def foo(self):
("item1", "item2", "item3"),
}

# Regression test for https://github.com/psf/black/issues/3506.
s = (
"With single quote: ' "
f" {my_dict['foo']}"
' With double quote: " '
f' {my_dict["bar"]}'
)

s = f'Lorem Ipsum is simply dummy text of the printing and typesetting industry:\'{my_dict["foo"]}\''


# output

Expand Down Expand Up @@ -1237,11 +1227,3 @@ async def foo(self):
# And there is a comment before the value
("item1", "item2", "item3"),
}

# Regression test for https://github.com/psf/black/issues/3506.
s = f"With single quote: ' {my_dict['foo']} With double quote: \" {my_dict['bar']}"

s = (
"Lorem Ipsum is simply dummy text of the printing and typesetting"
f" industry:'{my_dict['foo']}'"
)
Loading