Skip to content

Commit f7e1cf4

Browse files
authored
Format class definitions (#5289)
1 parent 7d4f8e5 commit f7e1cf4

File tree

27 files changed

+914
-541
lines changed

27 files changed

+914
-541
lines changed

Cargo.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ toml = { version = "0.7.2" }
5050
# v0.0.1
5151
libcst = { git = "https://github.com/charliermarsh/LibCST", rev = "80e4c1399f95e5beb532fdd1e209ad2dbb470438" }
5252
# v0.0.3
53-
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca" }
53+
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11" }
5454
# v0.0.3
55-
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca" , default-features = false, features = ["all-nodes-with-ranges", "num-bigint"]}
55+
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11" , default-features = false, features = ["all-nodes-with-ranges", "num-bigint"]}
5656
# v0.0.3
57-
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca", default-features = false, features = ["num-bigint"] }
57+
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11", default-features = false, features = ["num-bigint"] }
5858
# v0.0.3
59-
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca", default-features = false }
59+
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11", default-features = false }
6060
# v0.0.3
61-
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca" , default-features = false, features = ["full-lexer", "all-nodes-with-ranges", "num-bigint"] }
61+
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11" , default-features = false, features = ["full-lexer", "all-nodes-with-ranges", "num-bigint"] }
6262

6363
[profile.release]
6464
lto = "fat"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Test(
2+
Aaaaaaaaaaaaaaaaa,
3+
Bbbbbbbbbbbbbbbb,
4+
DDDDDDDDDDDDDDDD,
5+
EEEEEEEEEEEEEE,
6+
metaclass=meta,
7+
):
8+
pass
9+
10+
11+
class Test((Aaaaaaaaaaaaaaaaa), Bbbbbbbbbbbbbbbb, metaclass=meta):
12+
pass
13+
14+
class Test( # trailing class comment
15+
Aaaaaaaaaaaaaaaaa, # trailing comment
16+
17+
# in between comment
18+
19+
Bbbbbbbbbbbbbbbb,
20+
# another leading comment
21+
DDDDDDDDDDDDDDDD,
22+
EEEEEEEEEEEEEE,
23+
# meta comment
24+
metaclass=meta, # trailing meta comment
25+
):
26+
pass
27+
28+
class Test((Aaaa)):
29+
...
30+
31+
32+
class Test(aaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccc + dddddddddddddddddddddd + eeeeeeeee, ffffffffffffffffff, gggggggggggggggggg):
33+
pass
34+
35+
class Test(Aaaa): # trailing comment
36+
pass

crates/ruff_python_formatter/src/comments/placement.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,12 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
582582
.as_deref()
583583
.map(AnyNodeRef::from)
584584
.or_else(|| Some(AnyNodeRef::from(args.as_ref()))),
585+
AnyNodeRef::StmtClassDef(StmtClassDef {
586+
bases, keywords, ..
587+
}) => keywords
588+
.last()
589+
.map(AnyNodeRef::from)
590+
.or_else(|| bases.last().map(AnyNodeRef::from)),
585591
_ => None,
586592
};
587593

@@ -622,8 +628,13 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
622628
TokenKind::RParen => {
623629
// Skip over any closing parentheses
624630
}
625-
_ => {
626-
unreachable!("Only ')' or ':' should follow the condition")
631+
TokenKind::Comma => {
632+
// Skip over any trailing comma
633+
}
634+
kind => {
635+
unreachable!(
636+
"Only ')' or ':' should follow the condition but encountered {kind:?}"
637+
)
627638
}
628639
}
629640
}

crates/ruff_python_formatter/src/expression/parentheses.rs

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub(super) fn default_expression_needs_parentheses(
2626
#[allow(clippy::if_same_then_else)]
2727
if parenthesize.is_always() {
2828
Parentheses::Always
29+
} else if parenthesize.is_never() {
30+
Parentheses::Never
2931
}
3032
// `Optional` or `Preserve` and expression has parentheses in source code.
3133
else if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, source) {
@@ -58,14 +60,22 @@ pub enum Parenthesize {
5860
/// Parenthesizes the expression only if it doesn't fit on a line.
5961
IfBreaks,
6062

63+
/// Always adds parentheses
6164
Always,
65+
66+
/// Never adds parentheses. Parentheses are handled by the caller.
67+
Never,
6268
}
6369

6470
impl Parenthesize {
6571
pub(crate) const fn is_always(self) -> bool {
6672
matches!(self, Parenthesize::Always)
6773
}
6874

75+
pub(crate) const fn is_never(self) -> bool {
76+
matches!(self, Parenthesize::Never)
77+
}
78+
6979
pub(crate) const fn is_if_breaks(self) -> bool {
7080
matches!(self, Parenthesize::IfBreaks)
7181
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
2-
use ruff_formatter::{write, Buffer, FormatResult};
1+
use crate::prelude::*;
2+
use crate::FormatNodeRule;
3+
use ruff_formatter::write;
34
use rustpython_parser::ast::Keyword;
45

56
#[derive(Default)]
67
pub struct FormatKeyword;
78

89
impl FormatNodeRule<Keyword> for FormatKeyword {
910
fn fmt_fields(&self, item: &Keyword, f: &mut PyFormatter) -> FormatResult<()> {
10-
write!(f, [not_yet_implemented(item)])
11+
let Keyword {
12+
range: _,
13+
arg,
14+
value,
15+
} = item;
16+
if let Some(argument) = arg {
17+
write!(f, [argument.format(), text("=")])?;
18+
}
19+
20+
value.format().fmt(f)
1121
}
1222
}

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__class_blank_parentheses_py.snap

-135
This file was deleted.

0 commit comments

Comments
 (0)