Fix string normalization sometimes producing invalid fstrings (#327)
This commit is contained in:
parent
aad62d3de8
commit
5d0a469e8e
@ -807,6 +807,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
|
|||||||
|
|
||||||
## Change Log
|
## Change Log
|
||||||
|
|
||||||
|
### 18.6b3
|
||||||
|
|
||||||
|
* fixed improper formatting of f-strings with quotes inside interpolated
|
||||||
|
expressions (#322)
|
||||||
|
|
||||||
### 18.6b2
|
### 18.6b2
|
||||||
|
|
||||||
* added `--config` (#65)
|
* added `--config` (#65)
|
||||||
|
6
black.py
6
black.py
@ -2549,6 +2549,12 @@ def normalize_string_quotes(leaf: Leaf) -> None:
|
|||||||
leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
|
leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
|
||||||
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():
|
||||||
|
matches = re.findall(r"[^{]\{(.*?)\}[^}]", new_body)
|
||||||
|
for m in matches:
|
||||||
|
if "\\" in str(m):
|
||||||
|
# Do not introduce backslashes in interpolated expressions
|
||||||
|
return
|
||||||
if new_quote == '"""' and new_body[-1:] == '"':
|
if new_quote == '"""' and new_body[-1:] == '"':
|
||||||
# edge case:
|
# edge case:
|
||||||
new_body = new_body[:-1] + '\\"'
|
new_body = new_body[:-1] + '\\"'
|
||||||
|
@ -1,5 +1,21 @@
|
|||||||
f"f-string without formatted values is just a string"
|
f"f-string without formatted values is just a string"
|
||||||
f"{{NOT a formatted value}}"
|
f"{{NOT a formatted value}}"
|
||||||
|
f"{{NOT 'a' \"formatted\" \"value\"}}"
|
||||||
f"some f-string with {a} {few():.2f} {formatted.values!r}"
|
f"some f-string with {a} {few():.2f} {formatted.values!r}"
|
||||||
f"{f'{nested} inner'} outer"
|
f'some f-string with {a} {few(""):.2f} {formatted.values!r}'
|
||||||
|
f"{f'''{'nested'} inner'''} outer"
|
||||||
|
f"\"{f'{nested} inner'}\" outer"
|
||||||
f"space between opening braces: { {a for a in (1, 2, 3)}}"
|
f"space between opening braces: { {a for a in (1, 2, 3)}}"
|
||||||
|
f'Hello \'{tricky + "example"}\''
|
||||||
|
|
||||||
|
# output
|
||||||
|
|
||||||
|
f"f-string without formatted values is just a string"
|
||||||
|
f"{{NOT a formatted value}}"
|
||||||
|
f'{{NOT \'a\' "formatted" "value"}}'
|
||||||
|
f"some f-string with {a} {few():.2f} {formatted.values!r}"
|
||||||
|
f'some f-string with {a} {few(""):.2f} {formatted.values!r}'
|
||||||
|
f"{f'''{'nested'} inner'''} outer"
|
||||||
|
f"\"{f'{nested} inner'}\" outer"
|
||||||
|
f"space between opening braces: { {a for a in (1, 2, 3)}}"
|
||||||
|
f'Hello \'{tricky + "example"}\''
|
||||||
|
Loading…
Reference in New Issue
Block a user