-
Notifications
You must be signed in to change notification settings - Fork 2.6k
crash while formatting a delete statement with given line length #4625
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
Comments
Interesting, we have code in the AST safety check specifically allowing this: Line 211 in 5342d2e
|
I think the problem is that there is a tuple inside the tuple in the formatted code. |
@15r10nk great seeing your work being used to catch bugs in black. I'll pick up fixing this over the week, assuming you aren't working on a fix already. |
@tusharsadhwani Thank you, you can fix it if you want. I wanted to fix it, but I haven't found the time yet. |
Minimized the case to: del (), $ black -l1 -v x.py
[...]
Traceback (most recent call last):
File "/Users/tusharsadhwani/code/black/src/black/__init__.py", line 911, in reformat_one
if changed is not Changed.CACHED and format_file_in_place(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/tusharsadhwani/code/black/src/black/__init__.py", line 953, in format_file_in_place
dst_contents = format_file_contents(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/tusharsadhwani/code/black/src/black/__init__.py", line 1085, in format_file_contents
check_stability_and_equivalence(
File "/Users/tusharsadhwani/code/black/src/black/__init__.py", line 1059, in check_stability_and_equivalence
assert_equivalent(src_contents, dst_contents)
File "/Users/tusharsadhwani/code/black/src/black/__init__.py", line 1540, in assert_equivalent
raise ASTSafetyError(
black.parsing.ASTSafetyError: INTERNAL ERROR: Black 22.1.1.dev715+g97a730d.d20250219 on Python (CPython) 3.12.3 produced code that is not equivalent to the source. Please report a bug on https://github.com/psf/black/issues. |
It was indeed because we end up with a tuple inside a tuple after formatting. The AST check code was only unwrapping a top level tuple if present, causing the inner tuple after formatting to not get unwrapped before the AST comparison. |
Describe the bug
The following code can not be parsed/formatted by black:
black reported the following error:
but it can be parsed by cpython:
result:
black transforms the code with
--fast
intoThis has technical the same meaning but a different ast representation (tuple vs targets) https://docs.python.org/3/library/ast.html#ast.Delete.
Environment
)
Additional context
The bug was found by pysource-codegen (see #3908)
The text was updated successfully, but these errors were encountered: