File tree 3 files changed +28
-1
lines changed
3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -807,6 +807,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
807
807
808
808
# # Change Log
809
809
810
+ # ## 18.6b3
811
+
812
+ * fixed improper formatting of f-strings with quotes inside interpolated
813
+ expressions (#322)
814
+
810
815
# ## 18.6b2
811
816
812
817
* added `--config` (#65)
Original file line number Diff line number Diff line change @@ -2549,6 +2549,12 @@ def normalize_string_quotes(leaf: Leaf) -> None:
2549
2549
leaf .value = f"{ prefix } { orig_quote } { body } { orig_quote } "
2550
2550
new_body = sub_twice (escaped_orig_quote , rf"\1\2{ orig_quote } " , new_body )
2551
2551
new_body = sub_twice (unescaped_new_quote , rf"\1\\{ new_quote } " , new_body )
2552
+ if "f" in prefix .casefold ():
2553
+ matches = re .findall (r"[^{]\{(.*?)\}[^}]" , new_body )
2554
+ for m in matches :
2555
+ if "\\ " in str (m ):
2556
+ # Do not introduce backslashes in interpolated expressions
2557
+ return
2552
2558
if new_quote == '"""' and new_body [- 1 :] == '"' :
2553
2559
# edge case:
2554
2560
new_body = new_body [:- 1 ] + '\\ "'
Original file line number Diff line number Diff line change 1
1
f"f-string without formatted values is just a string"
2
2
f"{{NOT a formatted value}}"
3
+ f"{{NOT 'a' \" formatted\" \" value\" }}"
3
4
f"some f-string with { a } { few ():.2f} { formatted .values !r} "
4
- f"{ f'{ nested } inner' } outer"
5
+ f'some f-string with { a } { few ("" ):.2f} { formatted .values !r} '
6
+ f"{ f'''{ 'nested' } inner''' } outer"
7
+ f"\" { f'{ nested } inner' } \" outer"
5
8
f"space between opening braces: { {a for a in (1 , 2 , 3 )}} "
9
+ f'Hello \' { tricky + "example" } \' '
10
+
11
+ # output
12
+
13
+ f"f-string without formatted values is just a string"
14
+ f"{{NOT a formatted value}}"
15
+ f'{{NOT \' a\' "formatted" "value"}}'
16
+ f"some f-string with { a } { few ():.2f} { formatted .values !r} "
17
+ f'some f-string with { a } { few ("" ):.2f} { formatted .values !r} '
18
+ f"{ f'''{ 'nested' } inner''' } outer"
19
+ f"\" { f'{ nested } inner' } \" outer"
20
+ f"space between opening braces: { {a for a in (1 , 2 , 3 )}} "
21
+ f'Hello \' { tricky + "example" } \' '
You can’t perform that action at this time.
0 commit comments