-
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
Fix long case blocks not split into multiple lines #4024
Conversation
Thanks, I think we need to fix the |
Co-Authored-By: Jelle Zijlstra <[email protected]>
Thanks for the feedback, I've implemented a fix for the Now our parser won't attempt to insert the additional space. The unit tests were also modified (reverted) correspondingly to expect no change for this case. This additional fix caused flake8 to flag the Please let me know if changes are needed. |
for more information, see https://pre-commit.ci
Not sure what's going on with the diff-shades job; it passed before but started failing after I merged in main. Possibly it's hanging, as I don't see useful output. |
Yes, this also occured to me earlier once where the diff-shades job failed after running for 1h+, but then it succeeded upon re-running with no actual code change. Maybe I could make some trivial change and see if it can success this time. |
A follow up to psf#4024 but for `if` guards in `case` statements. I noticed this when psf#4024 was made stable, and noticed I had some code that had extra parens around the `if` guard. There might be some edge cases I am not thinking of, but all tests are passing. I put these new changes under the `remove_redundant_guard_parens` preview name.
Description
Fixes #3850 .
Previously Black doesn't recognize
case
block as one of the syntax blocks need to be wrapped with invisible parentheses, and hence will also not be splitting thecase
blocks into multiple lines even when it's too long.Now with (invisible) parentheses correctly wrapped around
case
blocks, the lines are able to be splitted into multiple lines when needed, this also allows the trailing comma rules to be enabled forcase
blocks. Unit testpattern_matching_long.py
andpattern_matching_trailing_comma.py
has been added to verify these modifications.At start, a side effect of this update is the altered formatting of
case case:
scenarios, resulting an additional space before the colon (i.e.,case case :
). This is due to Black after this update will treat the secondcase
as a distinct block that requires invisible parenthesis.Now, a special patch to the
case case:
scenario have been made to preserve consistency of user-visible outputs.A bit of discussion on the side effect prior to the special patch
Some background
Python 3.10 and later support matching the final case with an undefined pattern (e.g.
_
) as a default when other cases cannot match, this allow code like all following examples to execute and print the "x" without error:The side effect
After enabling parentheses to be wrapped around
case
blocks (hence also line breaking), the output of this particular corner case in our unit test will be different:Previously,
case
blocks were not considered for wrapping with parentheses, but now Black treats the second occurrence of "case" as an actualcase
block, so an additional space will be added between this occurrence and the colon. The underlying representation Black used for the second line is: "case (case ()):").Hence, the output is now:
On the other hand, while the consecutive
case
block now facing this additional space, the consecutivematch
block remains unchanged for unit tests like the one below, since thematch
block have no need of line breaking and was not using invisible parentheses.Summary
I was uncertain if it is preferable to add a special patch for this particular
case case:
scenario since this would adds additional complexity to the codebase. Even with Python 3.10+ allowing to treat the second "case" as an undefined pattern instead of a keyword, I would doubt developers will intentionally use a Python keyword as a pattern/variable.Nonetheless, considering that the user-visible output should be left as unchanged as possible, a patch have been made to address this.
Checklist - did you ...
CHANGES.md
if necessary?