black: internal error while formatting with-statement with tuple and none tuple argument
Describe the bug
The following code can not be parsed/formatted by black:
with (name_5, name_4), name_5:
pass
black reported the following error:
> black -l 100 -C -t py313 bug.py
error: cannot format bug.py: INTERNAL ERROR: Black 25.1.1.dev21+g944a38e.d20250317 on Python (CPython) 3.13.1 produced code that is not equivalent to the source. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: /tmp/blk_sxhcobwt.log
Oh no! 💥 💔 💥
1 file failed to reformat.
the reported diff in /tmp/blk_sxhcobwt.log is:
--- src
+++ dst
@@ -5,30 +5,29 @@
Pass(
) # /Pass
items=
withitem(
context_expr=
- Tuple(
+ Name(
ctx=
Load(
) # /Load
- elts=
- Name(
- ctx=
- Load(
- ) # /Load
- id=
- 'name_5', # str
- ) # /Name
- Name(
- ctx=
- Load(
- ) # /Load
- id=
- 'name_4', # str
- ) # /Name
- ) # /Tuple
+ id=
+ 'name_5', # str
+ ) # /Name
+ optional_vars=
+ None, # NoneType
+ ) # /withitem
+ withitem(
+ context_expr=
+ Name(
+ ctx=
+ Load(
+ ) # /Load
+ id=
+ 'name_4', # str
+ ) # /Name
optional_vars=
None, # NoneType
) # /withitem
withitem(
context_expr=
but it can be parsed by cpython:
from ast import parse
parse(
'with (name_5, name_4), name_5:\n'
' pass\n'
)
The code can be formatted with black -l 100 -C -t py313 bug.py --fast:
with name_5, name_4, name_5:
pass
Environment
- Black’s version: current main (950ec38c119916868833cb9896dab324c5d8eadd)
- OS and Python version: Linux/Python 3.13.1 (main, Jan 14 2025, 22:47:38) [Clang 19.1.6 ]
Additional context
The bug was found by pysource-codegen (see #3908) The above problem description was created from a script, let me know if you think it can be improved.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 18 (8 by maintainers)
The link stops working when you deselect 3.8 https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ADFAHBdAD2IimZxl1N_WmufDs0Loq3S0OujgtCoa6AGvvQPuvBGmbyr-KSEqr0X_j0sQDbLR8QE1fGl0ja2t5zbln7xaJ-krx95VW_XNCZly-Coo8Klf9vqRtHuK6UufVN_zebQwK4Y4axfwmji8lXDui_kF2AAFrLd_oOX-YEAAYwBxgEAACPyo8WxxGf7AgAAAAAEWVo=
The formatting fails when the target version is > 3.8
I also have no idea what is going on here. But thank you for verifying that I’m not crazy.
@Pedro-Muller29 I think your fix solves only 50% of the cases.
This is fixed:
This not:
I have looked at your merge request, but I can’t see the problem.
I do think we should fix this sort of thing even if the code always fails—if nothing else so that we can continue fuzzing, because fuzzing is a good way to find bugs!
Thanks @15r10nk for doing this fuzzing and thanks @Pedro-Muller29 for the fix.