Preserve required parentheses in lambda bodies#22747
Conversation
Summary -- This PR fixes the issues revealed in #22744 by adding an additional branch to the lambda body formatting that checks if the body `needs_parentheses` before falling back on the `Parentheses::Never` case. I also updated the `ExprNamed::needs_parentheses` implementation to match the one from #8465. Test Plan -- New test based on the failing cases in #22744. I also checked out #22744 and checked that the tests pass after applying the changes from this PR.
|
dylwil3
left a comment
There was a problem hiding this comment.
Looks good to me! Strange we missed what appears to be exactly one case from PEP 572 in needs_parentheses, but I just checked again and it seems like they're all there now.
|
Oh good idea to double-check that, thank you! |
| else if has_own_parentheses(body, f.context()).is_some() { | ||
| body.format().fmt(f) | ||
| } | ||
| // Include parentheses for cases that always require them, such as named expressions: |
There was a problem hiding this comment.
Do we need the same change on line 356? Or is it that we don't need it because the body must already be parenthesized?
I suggest adding a test that tries to exercise the 356 branch, just to be sure
There was a problem hiding this comment.
Added another test! Yes, this is okay because we're already adding parentheses in that branch. I believe that's true for all of the other branches too, we just need to avoid the final fallback behavior to Parentheses::Never.
* main: (149 commits) Preserve required parentheses in lambda bodies (#22747) [`flake8-simplify`] Make fix unsafe if it deletes comments (`SIM911`) (#22661) [`refurb`] Make fix unsafe if it deletes comments (`FURB145`) (#22670) [`ruff`] Make fix unsafe if it deletes comments (`RUF020`) (#22664) [ty] Add README for `ty_completion_bench` CLI tool [ty] Update completion eval [ty] Collect completions into a max-heap [ty] Truncate imports and qualifications derived from completions [ty] Get rid of high-level completion deduplication [ty] Refactor how we sort completions [ty] Optimize collection of "all symbols" [ty] Speed up completions by tweaking sorts [ty] Add new `ty_completion_bench` for ad hoc benchmarking gitignore: ignore scratch directories in completion eval truth directory [`pyupgrade`] Allow shadowing non-builtin bindings (`UP029`) (#22749) [ty] Emit invalid type form for stringified annotations (#22752) [ty] Allow `if type(x) is Y` narrowing for types other than class-literal types (#22729) [ty] Add basic support for overloads in `ParamSpec` (#21946) [`ruff`] Make fix unsafe if it deletes comments (`RUF019`) (#22663) [`flake8-bugbear`] Make fix unsafe if it deletes comments (`B014`) (#22659) ...
Summary
This PR fixes the issues revealed in #22744 by adding an additional branch to
the lambda body formatting that checks if the body
needs_parenthesesbeforefalling back on the
Parentheses::Nevercase. I also updated theExprNamed::needs_parenthesesimplementation to match the one from #8465.Test Plan
New test based on the failing cases in #22744. I also checked out #22744 and
checked that the tests pass after applying the changes from this PR.