Skip to content

Commit 965adbe

Browse files
authored
Fix trailing kwargs end of line comment after slash (#10297)
## Summary Fixes the handling end of line comments that belong to `**kwargs` when the `**kwargs` come after a slash. The issue was that we missed to include the `**kwargs` start position when determining the start of the next node coming after the `/`. Fixes #10281 ## Test Plan Added test
1 parent c504d7a commit 965adbe

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/function.py

+7
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ def f42(
278278
):
279279
pass
280280

281+
# Regression test for https://github.com/astral-sh/ruff/issues/10281
282+
def f43(
283+
__bar: str,
284+
/,
285+
**specifiers: typing.Any, # noqa: ANN401
286+
) -> int:
287+
return len(specifiers)
281288

282289
# Check trailing commas are permitted in funcdef argument list.
283290
def f(a, ): pass

crates/ruff_python_formatter/src/other/parameters.rs

+2
Original file line numberDiff line numberDiff line change
@@ -450,13 +450,15 @@ pub(crate) fn find_parameter_separators(
450450
// * `f(a, /, b)`
451451
// * `f(a, /, *b)`
452452
// * `f(a, /, *, b)`
453+
// * `f(a, /, *, **b)`
453454
// * `f(a, /)`
454455
let slash_following_start = parameters
455456
.args
456457
.first()
457458
.map(Ranged::start)
458459
.or(parameters.vararg.as_ref().map(|first| first.start()))
459460
.or(star.as_ref().map(|star| star.separator.start()))
461+
.or(parameters.kwarg.as_deref().map(Ranged::start))
460462
.unwrap_or(parameters.end());
461463
let slash = slash.map(|(preceding_end, slash)| ParameterSeparator {
462464
preceding_end,

crates/ruff_python_formatter/tests/snapshots/format@statement__function.py.snap

+16-3
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ def f42(
284284
):
285285
pass
286286

287+
# Regression test for https://github.com/astral-sh/ruff/issues/10281
288+
def f43(
289+
__bar: str,
290+
/,
291+
**specifiers: typing.Any, # noqa: ANN401
292+
) -> int:
293+
return len(specifiers)
287294

288295
# Check trailing commas are permitted in funcdef argument list.
289296
def f(a, ): pass
@@ -760,6 +767,15 @@ def f42(
760767
pass
761768

762769

770+
# Regression test for https://github.com/astral-sh/ruff/issues/10281
771+
def f43(
772+
__bar: str,
773+
/,
774+
**specifiers: typing.Any, # noqa: ANN401
775+
) -> int:
776+
return len(specifiers)
777+
778+
763779
# Check trailing commas are permitted in funcdef argument list.
764780
def f(
765781
a,
@@ -999,6 +1015,3 @@ def function_with_one_argument_and_a_keyword_separator(
9991015
) -> ReallyReallyReallyReallyReallyReallyReallyReallyLongName:
10001016
pass
10011017
```
1002-
1003-
1004-

0 commit comments

Comments
 (0)