-
Notifications
You must be signed in to change notification settings - Fork 2.6k
internal error while formatting with-statement with tuple and none tuple argument #4642
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
I believe this doesn't need a fix since this code will always fail at runtime, as tuples don't support the context manager protocol. In fact, running a code such as: with (open("a.txt", "r"), open("b.txt", "r")), open("c.txt", "r"):
pass Will throw the following error:
|
To be honest, I don't like to create these kind of pedantic issues. But this are the things which are found by pysource-codegen. I can understand when you do not want to fix it, but I have a example where this code can be useful. def test_tuple_as_contextmanager():
from contextlib import nullcontext
try:
with (nullcontext(),nullcontext()),nullcontext():
pass
except TypeError:
# test passed
pass
else:
# this should be a type error
assert False Code like this could be used in tests of an python implementation. I have no real world example, but this is the only use case I can think of where this syntax is "useful". |
Fair point, I guess there is some niche where this would be useful. Working on a PR. |
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. |
@Pedro-Muller29 I think your fix solves only 50% of the cases. This is fixed: with (a, b), c:
pass This not: with c,(a, b):
pass I have looked at your merge request, but I can't see the problem. |
Nice catch! Yeah, it's weird that my PR didn't fix that since it looks for commas both ways. I can try to fix it this weekend. |
I believe the initial PR I opened already handles both cases correctly. To confirm this, I’m opening a follow-up PR that adds some additional test cases, including one for |
sorry, but I tested it again on my main and it did not work. I think this playground link uses the current main. I can not explain why it works in CI. |
I test it locally with example.py with c, (a, b):
pass |
This is interresting:
The tests pass but I can not format the test file which you have created @Pedro-Muller29 |
I think this playground link is using the stable version (which don't have my changes yet). I tried switching it to main and it worked. Here’s the link to the working version |
Does |
Yeah, this is really strange. It’s almost like pytest isn’t treating a formatting failure as a test failure. I was planning to create a test file that intentionally fails formatting just to check if pytest was behaving correctly. But then I switched back to the same venv I was using earlier and tried formatting the tuple_with_stmt.py file—and it failed. No idea why, especially since it worked just an hour ago in this exact same environment. |
It was working when I opened my PR but now it didn't in the same |
I also have no idea what is going on here. But thank you for verifying that I'm not crazy. |
Is it possible that we don't check for AST equivalence in the test suite? |
Maybe, but that still wouldn't explain the playground link I sent working |
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 |
Describe the bug
The following code can not be parsed/formatted by black:
(playground)
black reported the following error:
the reported diff in /tmp/blk_sxhcobwt.log is:
but it can be parsed by cpython:
The code can be formatted with
black -l 100 -C -t py313 bug.py --fast
:Environment
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.
The text was updated successfully, but these errors were encountered: