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

--preview flag produces INTERNAL ERROR: f-string expression part cannot include a backslash #3506

Closed
oleksandr-terentiev opened this issue Jan 19, 2023 · 1 comment · Fixed by #3509
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title.

Comments

@oleksandr-terentiev
Copy link

Black v23.1a1

Playground link

Options

--line-length=80
--safe
--preview

Input

my_dict = {"foo": "bar"}
s= f'Lorem Ipsum is simply dummy text of the printing and typesetting industry:\'{my_dict["foo"]}\''

Output

INTERNAL ERROR: Black produced invalid code: f-string expression part cannot include a backslash (<unknown>, line 5). Please report a bug on https://github.com/psf/black/issues.  This invalid output might be helpful: /tmp/blk_5qbr6zhy.log

  File "src/black/__init__.py", line 1324, in assert_equivalent
  File "src/black/parsing.py", line 183, in parse_ast
my_dict = {"foo": "bar"}
s = (
    "Lorem Ipsum is simply dummy text of the printing and typesetting"
    f" industry:'{my_dict[\"foo\"]}'"
)

Expected

my_dict = {"foo": "bar"}
s = (
    "Lorem Ipsum is simply dummy text of the printing and typesetting"
    f" industry:'{my_dict['foo']}'"
)
@JelleZijlstra JelleZijlstra added the C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. label Jan 19, 2023
@yilei
Copy link
Contributor

yilei commented Jan 19, 2023

I'll take a look at this.

JelleZijlstra pushed a commit that referenced this issue Jan 22, 2023
Fixes #3506

We can't simply escape the quotes in a naked f-string when merging string groups, because backslashes are invalid.

The quotes in f-string expressions should be toggled (this is safe since quotes can't be reused).

This fix also means implicitly concatenated f-strings with different quotes can now be merged or quote-normalized by changing the quotes used in expressions. e.g.:

```diff
         raise sa_exc.UnboundExecutionError(
             "Could not locate a bind configured on "
-            f'{", ".join(context)} or this Session.'
+            f"{', '.join(context)} or this Session."
         )
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants