Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string-processing: F-string with triple quoted string containing single quotes #4494

Open
MichaReiser opened this issue Oct 22, 2024 · 4 comments
Labels
F: strings Related to our handling of strings T: bug Something isn't working

Comments

@MichaReiser
Copy link

MichaReiser commented Oct 22, 2024

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
@MichaReiser MichaReiser added the T: bug Something isn't working label Oct 22, 2024
@JelleZijlstra JelleZijlstra added the F: strings Related to our handling of strings label Oct 22, 2024
@JelleZijlstra
Copy link
Collaborator

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.

@MichaReiser
Copy link
Author

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 :)

@MeGaGiGaGon
Copy link
Contributor

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.

@MeGaGiGaGon
Copy link
Contributor

MeGaGiGaGon commented Oct 25, 2024

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__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants