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

Use one line between top-level items if formatting a stub file #6501

Merged
merged 22 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class A:
def __init__(self):
pass

class B:
def __init__(self):
pass

def foo():
pass

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

class Foo(Bar):
...

class Baz(Qux):
def __init__(self):
pass

class Quux(Qux):
def __init__(self):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class A:
def __init__(self):
pass


class B:
def __init__(self):
pass


def foo():
pass


class Del(expr_context):
...


class Load(expr_context):
...


class Store(expr_context):
...
tjkuson marked this conversation as resolved.
Show resolved Hide resolved


class Foo(Bar):
...


class Baz(Qux):
def __init__(self):
pass


class Quux(Qux):
def __init__(self):
pass
4 changes: 4 additions & 0 deletions crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ impl PyFormatOptions {
self.quote_style
}

pub fn source_type(&self) -> PySourceType {
self.source_type
}

#[must_use]
pub fn with_quote_style(mut self, style: QuoteStyle) -> Self {
self.quote_style = style;
Expand Down
50 changes: 50 additions & 0 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {

let comments = f.context().comments().clone();
let source = f.context().source();
let source_type = f.options().source_type();

let mut iter = statements.iter();
let Some(first) = iter.next() else {
Expand Down Expand Up @@ -116,6 +117,38 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
for statement in iter {
if is_class_or_function_definition(last) || is_class_or_function_definition(statement) {
match self.kind {
SuiteKind::TopLevel if source_type.is_stub() => match (last, statement) {
// Check if the statements are class definitions of the
// same type, containing only an ellipsis.
(
Stmt::ClassDef(ast::StmtClassDef {
arguments: Some(last_args),
body: last_body,
..
}),
Stmt::ClassDef(ast::StmtClassDef {
arguments: Some(args),
body,
..
}),
) if last_args.len() == args.len()
&& last_args.args.iter().zip(args.args.iter()).all(
|(last_arg, arg)| match (last_arg, arg) {
(Expr::Name(last_name), Expr::Name(name)) => {
last_name.id == name.id
}
_ => false,
},
)
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
&& contains_only_an_ellipsis(last_body)
&& contains_only_an_ellipsis(body) =>
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
{
write!(f, [statement.format()])?;
}
// Otherwise, top-level stub items should be separated
// one empty line.
_ => write!(f, [empty_line(), statement.format()])?,
},
SuiteKind::TopLevel => {
write!(f, [empty_line(), empty_line(), statement.format()])?;
}
Expand Down Expand Up @@ -208,6 +241,23 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
}
}

/// Returns `true` if a function or class body contains only an ellipsis.
fn contains_only_an_ellipsis(body: &Vec<Stmt>) -> bool {
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
body.len() == 1
&& body.first().map_or(false, |stmt| match stmt {
Stmt::Expr(ast::StmtExpr { value, .. }) => {
matches!(
value.as_ref(),
Expr::Constant(ast::ExprConstant {
value: Constant::Ellipsis,
..
})
)
}
_ => false,
})
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
}

/// Returns `true` if a [`Stmt`] is a class or function definition.
const fn is_class_or_function_definition(stmt: &Stmt) -> bool {
matches!(stmt, Stmt::FunctionDef(_) | Stmt::ClassDef(_))
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn format() {
});
};

insta::glob!("../resources", "test/fixtures/ruff/**/*.py", test_file);
insta::glob!("../resources", "test/fixtures/ruff/**/*.py*", test_file);
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
}

/// Format another time and make sure that there are no changes anymore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/top_level.py
---
## Input
```py
class A:
def __init__(self):
pass

class B:
def __init__(self):
pass

def foo():
pass

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

class Foo(Bar):
...

class Baz(Qux):
def __init__(self):
pass

class Quux(Qux):
def __init__(self):
pass
```

## Output
```py
class A:
def __init__(self):
pass


class B:
def __init__(self):
pass


def foo():
pass


class Del(expr_context):
...


class Load(expr_context):
...


class Store(expr_context):
...


class Foo(Bar):
...


class Baz(Qux):
def __init__(self):
pass


class Quux(Qux):
def __init__(self):
pass
```



Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/top_level.pyi
---
## Input
```py
class A:
def __init__(self):
pass


class B:
def __init__(self):
pass


def foo():
pass


class Del(expr_context):
...


class Load(expr_context):
...


class Store(expr_context):
...


class Foo(Bar):
...


class Baz(Qux):
def __init__(self):
pass


class Quux(Qux):
def __init__(self):
pass
```

## Output
```py
class A:
def __init__(self):
pass

class B:
def __init__(self):
pass

def foo():
pass

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

class Foo(Bar):
...

class Baz(Qux):
def __init__(self):
pass

class Quux(Qux):
def __init__(self):
pass
```