Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format function and class definitions into a single line if its body is an ellipsis #6592

Merged
merged 15 commits into from
Aug 21, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ class Test2(A):
def b(): ...
# comment
def c(): ...

class EllipsisWithComment:
... # comment

tjkuson marked this conversation as resolved.
Show resolved Hide resolved
def function_with_comment():
... # comment
34 changes: 25 additions & 9 deletions crates/ruff_python_formatter/src/statement/stmt_class_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ruff_python_trivia::lines_after_ignoring_trivia;

use crate::comments::{leading_comments, trailing_comments};
use crate::prelude::*;
use crate::statement::suite::SuiteKind;
use crate::statement::suite::{contains_only_an_ellipsis, SuiteKind};
use crate::FormatNodeRule;

#[derive(Default)]
Expand Down Expand Up @@ -117,14 +117,30 @@ impl FormatNodeRule<StmtClassDef> for FormatStmtClassDef {
}
}

write!(
f,
[
text(":"),
trailing_comments(trailing_definition_comments),
block_indent(&body.format().with_options(SuiteKind::Class))
]
)
// If the body contains only an ellipsis, format as:
// ```python
// class A: ...
// ```
if f.options().source_type().is_stub() && contains_only_an_ellipsis(body) {
write!(
f,
[
text(":"),
space(),
&body.format().with_options(SuiteKind::Class),
hard_line_break()
]
)
} else {
write!(
f,
[
text(":"),
trailing_comments(trailing_definition_comments),
block_indent(&body.format().with_options(SuiteKind::Class))
]
)
}
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
}

fn fmt_dangling_comments(
Expand Down
34 changes: 25 additions & 9 deletions crates/ruff_python_formatter/src/statement/stmt_function_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::comments::{leading_comments, trailing_comments};
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::{Parentheses, Parenthesize};
use crate::prelude::*;
use crate::statement::suite::SuiteKind;
use crate::statement::suite::{contains_only_an_ellipsis, SuiteKind};
use crate::FormatNodeRule;

#[derive(Default)]
Expand Down Expand Up @@ -135,14 +135,30 @@ impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {

write!(f, [group(&format_inner)])?;

write!(
f,
[
text(":"),
trailing_comments(trailing_definition_comments),
block_indent(&item.body.format().with_options(SuiteKind::Function))
]
)
// If the body contains only an ellipsis, format as:
// ```python
// def f(): ...
// ```
if f.options().source_type().is_stub() && contains_only_an_ellipsis(&item.body) {
write!(
f,
[
text(":"),
space(),
&item.body.format().with_options(SuiteKind::Function),
hard_line_break()
]
)
} else {
write!(
f,
[
text(":"),
trailing_comments(trailing_definition_comments),
block_indent(&item.body.format().with_options(SuiteKind::Function))
]
)
}
}

fn fmt_dangling_comments(
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
}

/// Returns `true` if a function or class body contains only an ellipsis.
fn contains_only_an_ellipsis(body: &[Stmt]) -> bool {
pub(crate) fn contains_only_an_ellipsis(body: &[Stmt]) -> bool {
match body {
[Stmt::Expr(ast::StmtExpr { value, .. })] => matches!(
value.as_ref(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class Test2(A):
def b(): ...
# comment
def c(): ...

class EllipsisWithComment:
... # comment

def function_with_comment():
... # comment
```

## Output
Expand All @@ -97,18 +103,13 @@ class B:
def foo():
pass

class Del(expr_context):
...
class Load(expr_context):
...
class Del(expr_context): ...
class Load(expr_context): ...

# Some comment.
class Other(expr_context):
...
class Store(expr_context):
...
class Foo(Bar):
...
class Other(expr_context): ...
class Store(expr_context): ...
class Foo(Bar): ...

class Baz(Qux):
def __init__(self):
Expand All @@ -123,30 +124,27 @@ class Quuz(Qux):
def __init__(self):
pass

def bar():
...
def baz():
...
def bar(): ...
def baz(): ...
def quux():
"""Some docstring."""

def quuz():
"""Some docstring."""

def a():
...
def a(): ...

class Test:
...
class Test2(A):
...
class Test: ...
class Test2(A): ...

def b():
...
def b(): ...

# comment
def c():
...
def c(): ...

class EllipsisWithComment: ... # comment

def function_with_comment(): ... # comment
```


Expand Down