Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Issue #18865: Decorated classes below functions should be separated with blank lines
def hello(): ...
@lambda _, /: _
class A: ...

def world(): ...

@final
class B: ...
7 changes: 7 additions & 0 deletions crates/ruff_python_formatter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ pub(crate) const fn is_no_chaperone_for_escaped_quote_in_triple_quoted_docstring
) -> bool {
context.is_preview()
}

/// Returns `true` if the [`blank_line_before_decorated_class_in_stub`](https://github.com/astral-sh/ruff/issues/18865) preview style is enabled.
pub(crate) const fn is_blank_line_before_decorated_class_in_stub_enabled(
context: &PyFormatContext,
) -> bool {
context.is_preview()
}
14 changes: 10 additions & 4 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::comments::{
use crate::context::{NodeLevel, TopLevelStatementPosition, WithIndentLevel, WithNodeLevel};
use crate::other::string_literal::StringLiteralKind;
use crate::prelude::*;
use crate::preview::is_blank_line_before_decorated_class_in_stub_enabled;
use crate::statement::stmt_expr::FormatStmtExpr;
use crate::verbatim::{
suppressed_node, write_suppressed_statements_starting_with_leading_comment,
Expand Down Expand Up @@ -700,10 +701,15 @@ fn stub_suite_can_omit_empty_line(preceding: &Stmt, following: &Stmt, f: &PyForm
//
// class LockType2: ...
// ```
let class_decorator_instead_of_empty_line = preceding.is_function_def_stmt()
&& following
.as_class_def_stmt()
.is_some_and(|class| !class.decorator_list.is_empty());
//
// However, this behavior is incorrect and should not be replicated in preview mode.
// See: https://github.com/astral-sh/ruff/issues/18865
let class_decorator_instead_of_empty_line =
!is_blank_line_before_decorated_class_in_stub_enabled(f.context())
&& preceding.is_function_def_stmt()
&& following
.as_class_def_stmt()
.is_some_and(|class| !class.decorator_list.is_empty());

// A function definition following a stub function definition
// ```python
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/stub_files/decorated_class_after_function.pyi
---
## Input
```python
# Issue #18865: Decorated classes below functions should be separated with blank lines
def hello(): ...
@lambda _, /: _
class A: ...

def world(): ...

@final
class B: ...
```

## Output
```python
# Issue #18865: Decorated classes below functions should be separated with blank lines
def hello(): ...
@lambda _, /: _
class A: ...

def world(): ...
@final
class B: ...
```


## Preview changes
```diff
--- Stable
+++ Preview
@@ -1,8 +1,10 @@
# Issue #18865: Decorated classes below functions should be separated with blank lines
def hello(): ...
+
@lambda _, /: _
class A: ...

def world(): ...
+
@final
class B: ...
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/stub_files/top_level.pyi
snapshot_kind: text
---
## Input
```python
Expand Down Expand Up @@ -43,3 +42,18 @@ class LockType3: ...
@final
class LockType4: ...
```


## Preview changes
```diff
--- Stable
+++ Preview
@@ -4,6 +4,7 @@
def count2(): ...
@final
def count3(): ...
+
@final
class LockType1: ...

```
Loading