string-processing: F-string with triple quoted string containing single quotes
Opened this issue · 4 comments
Describe the bug
Sorry for another cursed example.
Black changes the "
in the second inner f-string expression to '
which changes the program output.
f"{'''test ' '''}" f'{"""other " """}'
Actual output
f"{'''test ' '''}{'''other ' '''}"
# ^ note the changed quote
To Reproduce
Create a file with the above code and run with --unstable
with a pre python 312 target.
Or see this playground
Expected behavior
- Black should not join these two f-strings pre-python 312. At least I don't see any quote selection that results in valid pre python312 code.
- Black should not change the quote for Python 312 or newer
At least I don't see any quote selection that results in valid pre python312 code.
This would work: f"""{'''test ' '''}{'''other " '''}"""
. Probably better for Black to just leave the string alone, though.
At least I don't see any quote selection that results in valid pre python312 code.
This would work:
f"""{'''test ' '''}{'''other " '''}"""
. Probably better for Black to just leave the string alone, though.
True. To be more specific. I don't see a way without changing from single to triple quotes :)
While trying to fix #4495, I found this is actually doesn't need triple quotes to happen, since the quote flipping logic itself is flawed.
"" f'{"'"}' # output: Cannot parse for target version Python 3.13: 1:3: EOF in multi-line string
"" f'{"' "}' # output: Cannot parse for target version Python 3.13: 1:5: f"{'' '}"
This happens because _toggle_fexpr_quotes
does a replace, instead of considering if any strings in the expr have the other quote type.
Also in this same area of _toggle_fexpr_quotes
being broken, playground link, formatting "" f'{1:""}'
causes an "inequivalent to source code" error since it changes the :""
to :''
, which is correct since it is observable if the thing being formatted has a custom __format__
.