Skip to content

Fix leading comment formatting for lambdas with multiple parameters#21879

Merged
ntBre merged 9 commits intomainfrom
brent/fix-kwargs
Dec 9, 2025
Merged

Fix leading comment formatting for lambdas with multiple parameters#21879
ntBre merged 9 commits intomainfrom
brent/fix-kwargs

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Dec 9, 2025

Summary

This is a follow-up to #21868. As soon as I started merging #21868 into #21385, I realized that I had missed a test case with **kwargs after the *args parameter. Such a case is supposed to be formatted on one line like:

# input
(
    lambda
    # comment
    *x,
    **y: x
)

# output
(
    lambda
    # comment
    *x, **y: x
)

which you can still see on the playground, but on main after #21868, this was formatted as:

(
    lambda
    # comment
    *x,
    **y: x
)

because the leading comment on the first parameter caused the whole group around the parameters to break.

Instead of making these comments leading comments on the first parameter, this PR makes them leading comments on the parameters list as a whole.

Test Plan

New tests, and I will also try merging this into #21385 before opening it for review this time.


(labeling internal since #21868 should not be released before some kind of fix)

ntBre added 2 commits December 9, 2025 15:06
the new leading comment is causing the whole Parameters list to break. these
cases should instead format like:

```py
(
    lambda
    # comment
    *x, **y: x
)

(
    lambda
    # comment 2
    *x, **y: x
)
```

without line breaks in the parameter list
@ntBre ntBre added internal An internal refactor or improvement formatter Related to the formatter labels Dec 9, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 9, 2025

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser

This comment was marked as resolved.

@ntBre

This comment was marked as resolved.

@MichaReiser

This comment was marked as resolved.

@ntBre

This comment was marked as resolved.

@MichaReiser

This comment was marked as duplicate.

@ntBre

This comment was marked as resolved.

@MichaReiser

This comment was marked as resolved.

@ntBre

This comment was marked as resolved.

ntBre added 6 commits December 9, 2025 16:45
and fix some names

the start check handles both the `are_parameters_parenthesized` check (because
lambda parameters cannot be parenthesized and thus nothing can come between the
start of the parameters and the first parameter) and the comparison with
first.range() since the parameters start where the first parameter starts
I believe parameters itself would be None if there were no next parameter
@ntBre ntBre marked this pull request as ready for review December 9, 2025 22:30
@ntBre ntBre merged commit f3714fd into main Dec 9, 2025
37 checks passed
@ntBre ntBre deleted the brent/fix-kwargs branch December 9, 2025 23:15
dcreager added a commit that referenced this pull request Dec 10, 2025
* origin/main: (33 commits)
  [ty] Simplify union lower bounds and intersection upper bounds in constraint sets (#21871)
  [ty] Collapse `never` paths in constraint set BDDs (#21880)
  Fix leading comment formatting for lambdas with multiple parameters (#21879)
  [ty] Type inference for `@asynccontextmanager` (#21876)
  Fix comment placement in lambda parameters (#21868)
  [`pylint`] Detect subclasses of builtin exceptions (`PLW0133`) (#21382)
  Fix stack overflow with recursive generic protocols (depth limit) (#21858)
  New diagnostics for unused range suppressions (#21783)
  [ty] Use default settings in completion tests
  [ty] Infer type variables within generic unions  (#21862)
  [ty] Fix overload filtering to prefer more "precise" match (#21859)
  [ty] Stabilize auto-import
  [ty] Fix reveal-type E2E test (#21865)
  [ty] Use concise message for LSP clients not supporting related diagnostic information (#21850)
  Include more details in Tokens 'offset is inside token' panic message (#21860)
  apply range suppressions to filter diagnostics (#21623)
  [ty] followup: add-import action for `reveal_type` too (#21668)
  [ty] Enrich function argument auto-complete suggestions with annotated types
  [ty] Add autocomplete suggestions for function arguments
  [`flake8-bugbear`] Accept immutable slice default arguments (`B008`) (#21823)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants