Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Preserve required parentheses in lambda bodies#22747

Merged
ntBre merged 2 commits intomainfrom
brent/lambda-walrus
Jan 20, 2026
Merged

Preserve required parentheses in lambda bodies#22747
ntBre merged 2 commits intomainfrom
brent/lambda-walrus

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Jan 19, 2026

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.

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.
@ntBre ntBre added bug Something isn't working formatter Related to the formatter preview Related to preview mode features labels Jan 19, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 19, 2026

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre marked this pull request as ready for review January 19, 2026 22:28
@ntBre ntBre requested a review from MichaReiser as a code owner January 19, 2026 22:28
@ntBre ntBre requested a review from dylwil3 January 19, 2026 22:29
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ntBre
Copy link
Contributor Author

ntBre commented Jan 19, 2026

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:
Copy link
Member

@MichaReiser MichaReiser Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ntBre ntBre merged commit 5c44152 into main Jan 20, 2026
41 checks passed
@ntBre ntBre deleted the brent/lambda-walrus branch January 20, 2026 15:28
carljm added a commit that referenced this pull request Jan 20, 2026
* 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)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working formatter Related to the formatter preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants