mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-09 18:02:19 +00:00
Fix placement of inline parameter comments (#13379)
This commit is contained in:
parent
c7b2e336f0
commit
6ac61d7b89
6 changed files with 419 additions and 63 deletions
|
@ -142,53 +142,29 @@ variable: (
|
|||
): ...
|
||||
|
||||
|
||||
@@ -143,34 +141,31 @@
|
||||
|
||||
@@ -153,16 +151,18 @@
|
||||
|
||||
def foo(
|
||||
- arg: ( # comment with non-return annotation
|
||||
- int
|
||||
- # comment with non-return annotation
|
||||
- ),
|
||||
+ # comment with non-return annotation
|
||||
+ # comment with non-return annotation
|
||||
+ arg: (int),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def foo(
|
||||
- arg: ( # comment with non-return annotation
|
||||
arg: ( # comment with non-return annotation
|
||||
- int
|
||||
- | range
|
||||
- | memoryview
|
||||
- # comment with non-return annotation
|
||||
- ),
|
||||
+ # comment with non-return annotation
|
||||
+ # comment with non-return annotation
|
||||
+ arg: (int | range | memoryview),
|
||||
+ int | range | memoryview
|
||||
# comment with non-return annotation
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
-def foo(arg: int): # only before
|
||||
+def foo(
|
||||
+ # only before
|
||||
+ arg: (int),
|
||||
+ arg: ( # only before
|
||||
+ int
|
||||
+ ),
|
||||
+):
|
||||
pass
|
||||
|
||||
|
||||
def foo(
|
||||
- arg: (
|
||||
- int
|
||||
- # only after
|
||||
- ),
|
||||
+ # only after
|
||||
+ arg: (int),
|
||||
):
|
||||
pass
|
||||
|
||||
```
|
||||
|
||||
## Ruff Output
|
||||
|
@ -337,31 +313,36 @@ def foo() -> (
|
|||
|
||||
|
||||
def foo(
|
||||
# comment with non-return annotation
|
||||
# comment with non-return annotation
|
||||
arg: (int),
|
||||
arg: ( # comment with non-return annotation
|
||||
int
|
||||
# comment with non-return annotation
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def foo(
|
||||
# comment with non-return annotation
|
||||
# comment with non-return annotation
|
||||
arg: (int | range | memoryview),
|
||||
arg: ( # comment with non-return annotation
|
||||
int | range | memoryview
|
||||
# comment with non-return annotation
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def foo(
|
||||
# only before
|
||||
arg: (int),
|
||||
arg: ( # only before
|
||||
int
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def foo(
|
||||
# only after
|
||||
arg: (int),
|
||||
arg: (
|
||||
int
|
||||
# only after
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
|
|
@ -464,6 +464,111 @@ def foo(x: S) -> S: ...
|
|||
|
||||
@decorator # comment
|
||||
def foo(x: S) -> S: ...
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/13369
|
||||
def foo(
|
||||
arg: ( # comment with non-return annotation
|
||||
int
|
||||
# comment with non-return annotation
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def foo(
|
||||
arg: ( # comment with non-return annotation
|
||||
int
|
||||
| range
|
||||
| memoryview
|
||||
# comment with non-return annotation
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
def foo(arg: (
|
||||
int
|
||||
# only after
|
||||
)):
|
||||
pass
|
||||
|
||||
# Asserts that "incorrectly" placed comments don't *move* by fixing https://github.com/astral-sh/ruff/issues/13369
|
||||
def foo(
|
||||
# comment with non-return annotation
|
||||
# comment with non-return annotation
|
||||
arg: (int),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
# Comments between *args and **kwargs
|
||||
def args_no_type_annotation(*
|
||||
# comment
|
||||
args): pass
|
||||
|
||||
def args_type_annotation(*
|
||||
# comment
|
||||
args: int): pass
|
||||
|
||||
def args_trailing_end_of_line_comment(* # comment
|
||||
args): pass
|
||||
|
||||
def args_blank_line_comment(*
|
||||
|
||||
# comment
|
||||
|
||||
args): pass
|
||||
|
||||
def args_with_leading_parameter_comment(
|
||||
# What comes next are arguments
|
||||
*
|
||||
# with an inline comment
|
||||
args): pass
|
||||
|
||||
|
||||
def kargs_no_type_annotation(**
|
||||
# comment
|
||||
kwargs): pass
|
||||
|
||||
def kwargs_type_annotation(**
|
||||
# comment
|
||||
kwargs: int): pass
|
||||
|
||||
|
||||
def args_many_comments(
|
||||
# before
|
||||
*
|
||||
# between * and name
|
||||
args # trailing args
|
||||
# after name
|
||||
): pass
|
||||
|
||||
|
||||
def args_many_comments_with_type_annotation(
|
||||
# before
|
||||
*
|
||||
# between * and name
|
||||
args # trailing args
|
||||
# before colon
|
||||
: # after colon
|
||||
# before type
|
||||
int # trailing type
|
||||
# after type
|
||||
): pass
|
||||
|
||||
|
||||
|
||||
def args_with_type_annotations_no_after_colon_comment(
|
||||
# before
|
||||
*
|
||||
# between * and name
|
||||
args # trailing args
|
||||
# before colon
|
||||
:
|
||||
# before type
|
||||
int # trailing type
|
||||
# after type
|
||||
): pass
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -1089,6 +1194,130 @@ def foo(x: S) -> S: ...
|
|||
|
||||
@decorator # comment
|
||||
def foo(x: S) -> S: ...
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/13369
|
||||
def foo(
|
||||
arg: ( # comment with non-return annotation
|
||||
int
|
||||
# comment with non-return annotation
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def foo(
|
||||
arg: ( # comment with non-return annotation
|
||||
int | range | memoryview
|
||||
# comment with non-return annotation
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def foo(
|
||||
arg: (
|
||||
int
|
||||
# only after
|
||||
),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
# Asserts that "incorrectly" placed comments don't *move* by fixing https://github.com/astral-sh/ruff/issues/13369
|
||||
def foo(
|
||||
# comment with non-return annotation
|
||||
# comment with non-return annotation
|
||||
arg: (int),
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
# Comments between *args and **kwargs
|
||||
def args_no_type_annotation(
|
||||
# comment
|
||||
*args,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def args_type_annotation(
|
||||
# comment
|
||||
*args: int,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def args_trailing_end_of_line_comment(
|
||||
# comment
|
||||
*args,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def args_blank_line_comment(
|
||||
# comment
|
||||
*args,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def args_with_leading_parameter_comment(
|
||||
# What comes next are arguments
|
||||
# with an inline comment
|
||||
*args,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def kargs_no_type_annotation(
|
||||
# comment
|
||||
**kwargs,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def kwargs_type_annotation(
|
||||
# comment
|
||||
**kwargs: int,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def args_many_comments(
|
||||
# before
|
||||
# between * and name
|
||||
*args, # trailing args
|
||||
# after name
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def args_many_comments_with_type_annotation(
|
||||
# before
|
||||
# between * and name
|
||||
# trailing args
|
||||
# before colon
|
||||
*args:
|
||||
# after colon
|
||||
# before type
|
||||
int, # trailing type
|
||||
# after type
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
def args_with_type_annotations_no_after_colon_comment(
|
||||
# before
|
||||
# between * and name
|
||||
# trailing args
|
||||
# before colon
|
||||
*args:
|
||||
# before type
|
||||
int, # trailing type
|
||||
# after type
|
||||
):
|
||||
pass
|
||||
```
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue