psf/black

Cannot parse multiline f-string starting with backslash `\`

Closed this issue · 2 comments

Describe the bug

Black fails to parse a file containing a multiline f-string that starts with a backslash \

To Reproduce

For example, take this code:

print """\
This is a multiline string\
"""

And paste into https://black.vercel.app/?version=main

The resulting error is:

Cannot parse: 1:6: print """\

Example:

https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ACZAGFdAD2IimZxl1N_WmOQqEcOUhKyNfjwrEvHNPJJ-L3FEiTxKn-kRLm-QFP0oU2RsSfpKieifrParaKSlYAU88u1B6F0bWn_krYppL9-px2jvbbsLVg9_qRBqH77AUD8IILs3zEAAAAAeWQH5u87eYsAAX2aAQAAABaS41exxGf7AgAAAAAEWVo=

Expected behavior

Successfully parse.

Environment

  • Black's version: 24.4.2
  • OS and Python version: psf/black@stable

Locally, I am using Python 2.7.18 but the issue occurs in the GitHub Action.

Additional context

Reused material from #4337.

Locally, I am using Python 2.7.18

We no longer support formatting Python 2 (https://black.readthedocs.io/en/stable/faq.html#which-python-versions-does-black-support). If you use Python 3 syntax, Black will format this fine.

Ah, sorry. Thank-you.

Edit: I got it to pass by using parentheses with the print() call:

import sys

if len(sys.argv) < 2:
    print(
        """\
Something something\
"""
    )