Don't introduce quotes to f-string sub-expressions on string boundaries (#871)
This commit is contained in:
parent
5b01a8e3b0
commit
519c06a8cc
@ -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
|
* fixed bug that led *Black* format some code with a line length target
|
||||||
of 1 (#762)
|
of 1 (#762)
|
||||||
|
|
||||||
|
* *Black* no longer introduces quotes in f-string subexpressions on string
|
||||||
|
boundaries (#863)
|
||||||
|
|
||||||
|
|
||||||
### 19.3b0
|
### 19.3b0
|
||||||
|
|
||||||
|
10
black.py
10
black.py
@ -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(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
|
||||||
new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
|
new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
|
||||||
if "f" in prefix.casefold():
|
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:
|
for m in matches:
|
||||||
if "\\" in str(m):
|
if "\\" in str(m):
|
||||||
# Do not introduce backslashes in interpolated expressions
|
# Do not introduce backslashes in interpolated expressions
|
||||||
|
@ -44,6 +44,12 @@
|
|||||||
'\\""'
|
'\\""'
|
||||||
"\\''"
|
"\\''"
|
||||||
'Lots of \\\\\\\\\'quotes\''
|
'Lots of \\\\\\\\\'quotes\''
|
||||||
|
f'{y * " "} \'{z}\''
|
||||||
|
f'{{y * " "}} \'{z}\''
|
||||||
|
f'\'{z}\' {y * " "}'
|
||||||
|
f'{y * x} \'{z}\''
|
||||||
|
'\'{z}\' {y * " "}'
|
||||||
|
'{y * x} \'{z}\''
|
||||||
|
|
||||||
# output
|
# output
|
||||||
|
|
||||||
@ -93,3 +99,9 @@
|
|||||||
'\\""'
|
'\\""'
|
||||||
"\\''"
|
"\\''"
|
||||||
"Lots of \\\\\\\\'quotes'"
|
"Lots of \\\\\\\\'quotes'"
|
||||||
|
f'{y * " "} \'{z}\''
|
||||||
|
f"{{y * \" \"}} '{z}'"
|
||||||
|
f'\'{z}\' {y * " "}'
|
||||||
|
f"{y * x} '{z}'"
|
||||||
|
"'{z}' {y * \" \"}"
|
||||||
|
"{y * x} '{z}'"
|
Loading…
Reference in New Issue
Block a user