-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Black fails to parse parenthesized (manager() as x)
in with statements
#1948
Comments
This is new syntax in 3.9, right? We'll just have to update our parser. |
I didn't realize it was new, but I did just confirm that you are correct. I really thought I had, at some point, done some multi-line |
This might be harder than it appears at first glance because IIRC this syntax is only allowed by the new PEG parser available in CPython 3.9 and higher... and our parser is LL(1). IIRC we can allow this syntax while continuing to use our LL(1) parser by hacking the tokenizer, but I'm not sure of any this. I should really check and play around, but I wanted to provide a possible explanation to why this support hasn't landed in Black yet. Also while slightly off-topic, I dislike that we advertise that Black handles with statements in a sane manner in the documentation, yet none of the work needed for any of it has seen the light of day. Doing documentation-driven development is good, but I think we're sending the wrong signal if the actual development ends up never finished. But, just my opinion, shrugs. |
I agree about the documentation; we should make the documentation reflect what Black actually does. We can also mention what we'd like Black to do, but the primary focus should be on what Black actually does. |
Just came here to file an issue regarding the documentation:
This is no longer true, and Black should break up |
@NeilGirdhar, good to know (I'm rewriting the docs right now), but note that Black will only be able to use parentheses for with statements when targeting Python 3.9 and higher and since Black won't be defaulting to |
With the merge of #2586 Black can now parse parenthesized with statements 🎉 Although we are still undecided with how we want to handle both the detection and rewriting of with statements to use this newer prettier syntax. Discussion on Python Discord quoted below:
The gist of it is that we shouldn't detect parenthesized withs as a marker of 3.9+ code (and automagically set target-version) since it's technically an implementation detail of CPython until 3.10. Unfortunately we can't really use it as marker of 3.10+ code because it'd activate the currently experimental pattern matching support (i.e. special grammar). FWIW I mentioned one potential solution above. Another potential solution is to not detect parenthesized withs at all and use --target-version as the safeguard to enable rewriting to parenthesized withs (kinda like with pattern matching). Anyway this discussion happened after I wrote up some code to detect parenthesized with so I'll just leave this here for when we make a decision: parenthesized-with-detection-support-needs-tests.patch.txt (it still needs tests). |
This works now on main. |
Describe the bug A clear and concise description of what the bug is.
Black fails to parse
with
statements that are both parenthesized and useas
such aswith (open("a", "w") as a):
. I found this while noting that black would not reformat an overly long line of the formwith a as b, x as y:
.To Reproduce Steps to reproduce the behavior:
https://black.now.sh/?version=master&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ADxAGhdAD2IimZxl1N_WmufDs0LpGEw9emYfCBakMKmw1q3jXUj3wtHo6NCOupRzg7ydqs0H1WF8HT1qPymFuq8UolBICiBSQevQsn3TFo9kaHrTJtsFiiPLpaMZ8_MAWjg5A9X2Ar4c0sX0RkzAApiLbCr57chAAGEAfIBAAC4H7lWscRn-wIAAAAABFla
Expected behavior A clear and concise description of what you expected to happen.
I expect that black does not fail to parse and also reformats as needed when the line is too long. Reformatting would include adding parentheses and wrapping such as below (for actually too-long lines).
Environment (please complete the following information):
Does this bug also happen on master? To answer this, you have two options:
yes
Additional context Add any other context about the problem here.
Perhaps I should enter two bugs. One for the parsing failure and one for the lack of willingness to add parentheses and reformat. Or perhaps they are sufficiently intertwined... let me know if I need to file a second issue.
The text was updated successfully, but these errors were encountered: