-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Comments
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 ( |
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. |
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 |
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:
With the following error (per the playground):
But this will pass:
EDIT:
These also all fail (but are correctable by switching quote types), showing it has to do with template string parsing, not ternary:
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:
But I am forced to use single-quotes to make Black function.
EDIT:
"Working" examples from addition:
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
Additional context
N/A
The text was updated successfully, but these errors were encountered: