Skip to content

Commit

Permalink
Fix trailing kwargs end of line comment after slash (#10297)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
MichaReiser committed Mar 8, 2024
1 parent c504d7a commit 965adbe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ def f42(
):
pass

# Regression test for https://github.com/astral-sh/ruff/issues/10281
def f43(
__bar: str,
/,
**specifiers: typing.Any, # noqa: ANN401
) -> int:
return len(specifiers)

# Check trailing commas are permitted in funcdef argument list.
def f(a, ): pass
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_python_formatter/src/other/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,15 @@ pub(crate) fn find_parameter_separators(
// * `f(a, /, b)`
// * `f(a, /, *b)`
// * `f(a, /, *, b)`
// * `f(a, /, *, **b)`
// * `f(a, /)`
let slash_following_start = parameters
.args
.first()
.map(Ranged::start)
.or(parameters.vararg.as_ref().map(|first| first.start()))
.or(star.as_ref().map(|star| star.separator.start()))
.or(parameters.kwarg.as_deref().map(Ranged::start))
.unwrap_or(parameters.end());
let slash = slash.map(|(preceding_end, slash)| ParameterSeparator {
preceding_end,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ def f42(
):
pass

# Regression test for https://github.com/astral-sh/ruff/issues/10281
def f43(
__bar: str,
/,
**specifiers: typing.Any, # noqa: ANN401
) -> int:
return len(specifiers)

# Check trailing commas are permitted in funcdef argument list.
def f(a, ): pass
Expand Down Expand Up @@ -760,6 +767,15 @@ def f42(
pass


# Regression test for https://github.com/astral-sh/ruff/issues/10281
def f43(
__bar: str,
/,
**specifiers: typing.Any, # noqa: ANN401
) -> int:
return len(specifiers)


# Check trailing commas are permitted in funcdef argument list.
def f(
a,
Expand Down Expand Up @@ -999,6 +1015,3 @@ def function_with_one_argument_and_a_keyword_separator(
) -> ReallyReallyReallyReallyReallyReallyReallyReallyLongName:
pass
```



0 comments on commit 965adbe

Please sign in to comment.