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

Internal Error when formatting python 3.12 f-string with same quotes inside and out. #4110

Closed
jfrost-mo opened this issue Dec 14, 2023 · 1 comment
Labels
T: bug Something isn't working

Comments

@jfrost-mo
Copy link

jfrost-mo commented Dec 14, 2023

Describe the bug

Black crashes when trying to format code containing a string inside an f-string that uses the same type of quotes. This is valid syntax as of python 3.12.

To Reproduce

For example, take this code:

#! /usr/bin/env python3

print(f"{"a"}")

And run it with these arguments:

$ black --verbose --target-version py312 source.py

The resulting error is:

Identified `/` as project root containing a file system root.
Found input source: "net/home/h02/jfrost/blacktest/source.py"
Traceback (most recent call last):
  File "/data/users/jfrost/conda/envs/cset-dev/lib/python3.12/site-packages/black/__init__.py", line 837, in reformat_one
    if changed is not Changed.CACHED and format_file_in_place(
                                         ^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/jfrost/conda/envs/cset-dev/lib/python3.12/site-packages/black/__init__.py", line 879, in format_file_in_place
    dst_contents = format_file_contents(
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/jfrost/conda/envs/cset-dev/lib/python3.12/site-packages/black/__init__.py", line 1005, in format_file_contents
    dst_contents = format_str(src_contents, mode=mode, lines=lines)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/jfrost/conda/envs/cset-dev/lib/python3.12/site-packages/black/__init__.py", line 1156, in format_str
    dst_contents = _format_str_once(src_contents, mode=mode, lines=lines)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/jfrost/conda/envs/cset-dev/lib/python3.12/site-packages/black/__init__.py", line 1170, in _format_str_once
    src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/users/jfrost/conda/envs/cset-dev/lib/python3.12/site-packages/black/parsing.py", line 89, in lib2to3_parse
    raise exc from None
black.parsing.InvalidInput: Cannot parse: 3:10: print(f"{"a"}")
error: cannot format source.py: Cannot parse: 3:10: print(f"{"a"}")

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

Expected behavior

The code is successfully formatted.

Environment

  • Black's version: black, 23.11.0 (compiled: no)
  • OS and Python version: Linux (RHEL7), Python 3.12.0

Additional context

@jfrost-mo jfrost-mo added the T: bug Something isn't working label Dec 14, 2023
@hauntsaninja
Copy link
Collaborator

Duplicate of #3746

@hauntsaninja hauntsaninja marked this as a duplicate of #3746 Dec 14, 2023
@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants