Unverified Commit 519c06a8 authored by Zsolt Dollenstein's avatar Zsolt Dollenstein Committed by GitHub
Browse files

Don't introduce quotes to f-string sub-expressions on string boundaries (#871)

parent 5b01a8e3
Loading
Loading
Loading
Loading
+3 −0
Changes for README.md: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1034,6 +1034,9 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
* fixed bug that led *Black* format some code with a line length target
  of 1 (#762)

* *Black* no longer introduces quotes in f-string subexpressions on string
  boundaries (#863)


### 19.3b0

+9 −1
Changes for black.py: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2728,7 +2728,15 @@ def normalize_string_quotes(leaf: Leaf) -> None:
        new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
        new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
    if "f" in prefix.casefold():
        matches = re.findall(r"[^{]\{(.*?)\}[^}]", new_body)
        matches = re.findall(
            r"""
            (?:[^{]|^)\{  # start of the string or a non-{ followed by a single {
                ([^{].*?)  # contents of the brackets except if begins with {{
            \}(?:[^}]|$)  # A } followed by end of the string or a non-}
            """,
            new_body,
            re.VERBOSE,
        )
        for m in matches:
            if "\\" in str(m):
                # Do not introduce backslashes in interpolated expressions
+12 −0
Changes for tests/data/string_quotes.py: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -44,6 +44,12 @@
'\\""'
"\\''"
'Lots of \\\\\\\\\'quotes\''
f'{y * " "} \'{z}\''
f'{{y * " "}} \'{z}\''
f'\'{z}\' {y * " "}'
f'{y * x} \'{z}\''
'\'{z}\' {y * " "}'
'{y * x} \'{z}\''

# output

@@ -93,3 +99,9 @@
'\\""'
"\\''"
"Lots of \\\\\\\\'quotes'"
f'{y * " "} \'{z}\''
f"{{y * \" \"}} '{z}'"
f'\'{z}\' {y * " "}'
f"{y * x} '{z}'"
"'{z}' {y * \" \"}"
"{y * x} '{z}'"
 No newline at end of file