microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
149 stars 35 forks source link

cannot format xxxxx.py: Cannot parse: 219:50: xxxxx = f"TEST{now.strftime("%Y%m%d%H%M%S")}{random.randint(1,9999):0>4}" #517

Closed godtang closed 1 day ago

godtang commented 3 months ago

Diagnostic Data

Behaviour

Expected Behavior

reformatted succ

Actual Behavior

Cannot parse

Reproduction Steps:

now = datetime.now()
tmp = f"TEST{now.strftime("%Y%m%d%H%M%S")}{random.randint(1,9999):0>4}"

Logs:

Click here for detailed logs 2024-06-11 15:38:40.561 [info] error: cannot format xxxxx.py: Cannot parse: 219:52: xxxx = f"TEST{now.strftime("%Y%m%d%H%M%S")}{random.randint(1,9999):0>4}" Oh no! 💥 💔 💥 1 file failed to reformat.

Outcome When Attempting Debugging Steps:

Did running it from the command line work?

Extra Details

karthiknadig commented 3 months ago

@godtang This is coming from Black itself. Does it work when you try to format it from command line?

godtang commented 3 months ago

@godtang This is coming from Black itself. Does it work when you try to format it from command line?

sorry , i don't know how to format from command line

godtang commented 3 months ago

image i try it, and black work

karthiknadig commented 3 months ago

What is the version of black where it worked? python -m black —version should tell you.

godtang commented 3 months ago

What is the version of black where it worked? python -m black —version should tell you.

python.exe -m black --version python -m black, 24.4.2 (compiled: yes) Python (CPython) 3.12.4 image

wbkboyer commented 2 months ago

Hey @godtang ! I think I've discovered how to resolve your issue: could you try to replace the double-quotes with single-quotes inside your f-string curly braces (i.e. inside your call to now.strftime())?

In my use-case:

2024-07-17 18:18:39.086 [info] error: cannot format XXXXXXX: Cannot parse: 398:34:                     f"|{data.get("infilename"):<{infilename_len}}"

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

After changing inner double-quotes to single-quotes:

outfile.write(
    f"|{data.get('infilename'):<{infilename_len}}"
    ...
)

Both the VSCode black extension (ms-python.black-formatter ships with black=24.3.0.) and black, 24.4.2 now succeed:

2024-07-17 18:32:19.912 [info] reformatted XXXXXX

All done! ✨ 🍰 ✨
1 file reformatted.
karthiknadig commented 1 day ago

Closing this, since the build in black is updated in the pre-release to latest.