Skip to content

Commit

Permalink
Fix incorrect placement of leading function comment with type params (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Jul 22, 2024
1 parent 3ace129 commit ed238e0
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,25 @@ def as_manager(cls):

as_manager.queryset_only = True
as_manager = classmethod(as_manager)


# Decorators
@decorator
# comment
class Foo1: ...

@decorator
# comment
class Foo2(Foo1): ...

@decorator
# comment
class Foo3[T]: ...

@decorator # comment
class Foo4: ...

@decorator
# comment
@decorato2
class Foo5: ...
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,25 @@ def function_with_variadic_generics(*args: *tuple[int],): ...

# Generic arguments (PEP 695)
def func[T](lotsoflongargs: T, lotsoflongargs2: T, lotsoflongargs3: T, lotsoflongargs4: T, lotsoflongargs5: T) -> T: ...


# Decorators
@decorator
# comment
def foo[S](x: S) -> S: ...

@decorator
# comment
def foo(x: S) -> S: ...

@decorator
# comment
def foo() -> S: ...

@decorator
# comment
@decorator2
def foo(x: S) -> S: ...

@decorator # comment
def foo(x: S) -> S: ...
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ fn handle_leading_function_with_decorators_comment(comment: DecoratedComment) ->

let is_following_parameters = comment
.following_node()
.is_some_and(|node| node.is_parameters());
.is_some_and(|node| node.is_parameters() || node.is_type_params());

if comment.line_position().is_own_line() && is_preceding_decorator && is_following_parameters {
CommentPlacement::dangling(comment.enclosing_node(), comment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@ class QuerySet(AltersData):
as_manager.queryset_only = True
as_manager = classmethod(as_manager)
# Decorators
@decorator
# comment
class Foo1: ...
@decorator
# comment
class Foo2(Foo1): ...
@decorator
# comment
class Foo3[T]: ...
@decorator # comment
class Foo4: ...
@decorator
# comment
@decorato2
class Foo5: ...
```

## Output
Expand Down Expand Up @@ -489,7 +511,30 @@ class QuerySet(AltersData):
as_manager.queryset_only = True
as_manager = classmethod(as_manager)
```
# Decorators
@decorator
# comment
class Foo1: ...
@decorator
# comment
class Foo2(Foo1): ...
@decorator
# comment
class Foo3[T]: ...
@decorator # comment
class Foo4: ...
@decorator
# comment
@decorato2
class Foo5: ...
```
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,28 @@ def function_with_variadic_generics(*args: *tuple[int],): ...

# Generic arguments (PEP 695)
def func[T](lotsoflongargs: T, lotsoflongargs2: T, lotsoflongargs3: T, lotsoflongargs4: T, lotsoflongargs5: T) -> T: ...


# Decorators
@decorator
# comment
def foo[S](x: S) -> S: ...

@decorator
# comment
def foo(x: S) -> S: ...

@decorator
# comment
def foo() -> S: ...

@decorator
# comment
@decorator2
def foo(x: S) -> S: ...

@decorator # comment
def foo(x: S) -> S: ...
```

## Output
Expand Down Expand Up @@ -1041,6 +1063,32 @@ def func[T](
lotsoflongargs4: T,
lotsoflongargs5: T,
) -> T: ...


# Decorators
@decorator
# comment
def foo[S](x: S) -> S: ...


@decorator
# comment
def foo(x: S) -> S: ...


@decorator
# comment
def foo() -> S: ...


@decorator
# comment
@decorator2
def foo(x: S) -> S: ...


@decorator # comment
def foo(x: S) -> S: ...
```


Expand Down

0 comments on commit ed238e0

Please sign in to comment.