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

Black --preview mode produces invalid code in the first pass, causing second pass to fail #3117

Closed
yilei opened this issue Jun 10, 2022 · 0 comments · Fixed by #3440
Closed
Labels
C: invalid code Black destroyed a valid Python file F: strings Related to our handling of strings T: bug Something isn't working

Comments

@yilei
Copy link
Contributor

yilei commented Jun 10, 2022

Describe the bug

Black's --preview mode produces invalid code in the first pass, causing second pass to fail.

To Reproduce

For example, take this code:

some_dict = {
    "something_something":
        r"Lorem ipsum dolor sit amet, an sed convenire eloquentiam \t"
        r"signiferumque, duo ea vocibus consetetur scriptorem. Facer \t",
}

And run it with these arguments:

$ black --preview file.py

The resulting error is:

error: cannot format filepy: Cannot parse: 5:4:     r"signiferumque, duo ea vocibus consetetur scriptorem. Facer \t",

Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

It should succeed.

Environment

  • Black's version: black, 22.3.1.dev43+gf51e537 (compiled: no)
  • OS and Python version: Linux, Python (CPython) 3.9.12

Additional context

  1. Looks like the first _format_str_once call formats the code to:
some_dict = {
    "something_something": (
        r"Lorem ipsum dolor sit amet, an sed convenire eloquentiam \t"
    )
    r"signiferumque, duo ea vocibus consetetur scriptorem. Facer \t",
}
  1. It's only reproducible with r-strings.
@yilei yilei added the T: bug Something isn't working label Jun 10, 2022
@ichard26 ichard26 added C: invalid code Black destroyed a valid Python file F: strings Related to our handling of strings labels Aug 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: invalid code Black destroyed a valid Python file F: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants