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

Failure to parse string templates with similar quotes #4271

Closed
andrewvaughan opened this issue Mar 10, 2024 · 4 comments
Closed

Failure to parse string templates with similar quotes #4271

andrewvaughan opened this issue Mar 10, 2024 · 4 comments
Labels
T: bug Something isn't working

Comments

@andrewvaughan
Copy link

andrewvaughan commented Mar 10, 2024

Describe the bug

When using a ternary statement within a string template, Black fails to parse the string if the quotes used within the template are the same as the template string.

Edit: Any template string that contains quotes of the same style as the parent template will fail.

To Reproduce

This fails:

cmd.append(f"--{"no-" if not do_continue else ""}continue")

With the following error (per the playground):

Cannot parse: 1:17: cmd.append(f"--{"no-" if not do_continue else ""}continue")

But this will pass:

cmd.append(f"--{'no-' if not do_continue else ''}continue")

EDIT:

These also all fail (but are correctable by switching quote types), showing it has to do with template string parsing, not ternary:

cmd.append(f"--ffmpeg-location {pipes.quote(cfg["ffmpeg"])}")
cmd.append(f"--example {some_dict["key"]}")

These are all valid Python, however.

Expected behavior

Black should enforce quotes consistently, including within template strings (or, at minimum, not fail at parsing and rather throw a formatting error). Per my configurations, this should be the proper format:

cmd.append(f"--{"no-" if not do_continue else ""}continue")

But I am forced to use single-quotes to make Black function.

EDIT:

"Working" examples from addition:

cmd.append(f"--ffmpeg-location {pipes.quote(cfg['ffmpeg'])}")
cmd.append(f"--example {some_dict['key']}")

These should, at minimum, throw a validation error and not fail to parse, if enforcing different quote-styles is the intent (although, I would argue switching quote-styles is detrimental).

Environment

  • Black's version: Current Black playground (Stable: 24.1.0 | Main: @ed770b)
  • OS and Python version: N/A

Additional context

N/A

@andrewvaughan andrewvaughan added the T: bug Something isn't working label Mar 10, 2024
@andrewvaughan andrewvaughan changed the title Failure to parse ternary statements within string templates that use same quotes Failure to parse string templates with similar quotes Mar 10, 2024
@andrewvaughan
Copy link
Author

andrewvaughan commented Mar 10, 2024

I have also tested that nested template strings work, but only if all nested templates have opposite quote styles:

cmd.append(f"--add-header {pipes.quote(f'User-Agent:{browser['user-agent']}')}")

I was somewhat expecting this to be an "unsolvable" situation, but it did parse correctly when all internal quotes were single-quotes (') if the parent template was double-quotes (").

@JelleZijlstra
Copy link
Collaborator

Duplicate of #3746

@JelleZijlstra JelleZijlstra marked this as a duplicate of #3746 Mar 10, 2024
@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Mar 10, 2024
@andrewvaughan
Copy link
Author

Duplicate of #3746

Ah, thanks. Did a cursory search but didn't see it - I'm still getting into the habit of checking pins and not going straight to search.

@JelleZijlstra
Copy link
Collaborator

And just to explain the behavior you're seeing: Black's parser currently treats f-strings just like normal strings, treating whatever is inside as just a string literal. Your cmd.append(f"--add-header {pipes.quote(f'User-Agent:{browser['user-agent']}')}") is accepted because Black just ignores whatever happens until it sees the closing double quote.

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

No branches or pull requests

2 participants