-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 while formatting with-statement with tuple and none tuple argument #4642
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. |
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: