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

This commit is contained in:
Zsolt Dollenstein 2019-05-26 11:58:00 +02:00 committed by GitHub
parent 5b01a8e3b0
commit 519c06a8cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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}'"