Skip to content

Commit

Permalink
Format assert statement (#5168)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer authored Jul 14, 2023
1 parent 5a4516b commit a961f75
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 370 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
assert True # Trailing same-line
assert True is True # Trailing same-line
assert 1, "Some string" # Trailing same-line
assert aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # Trailing same-line

assert ( # Dangle1
# Dangle2
)

# TODO: https://github.com/astral-sh/ruff/pull/5168#issuecomment-1630767421
# Leading assert
assert (
# Leading test value
True # Trailing test value same-line
# Trailing test value own-line
), "Some string" # Trailing msg same-line
# Trailing assert

# Random dangler

# TODO: https://github.com/astral-sh/ruff/pull/5168#issuecomment-1630767421
# Leading assert
assert (
# Leading test value
True # Trailing test value same-line
# Trailing test value own-line

# Test dangler
), "Some string" # Trailing msg same-line
# Trailing assert
33 changes: 31 additions & 2 deletions crates/ruff_python_formatter/src/statement/stmt_assert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAssert;

Expand All @@ -7,6 +10,32 @@ pub struct FormatStmtAssert;

impl FormatNodeRule<StmtAssert> for FormatStmtAssert {
fn fmt_fields(&self, item: &StmtAssert, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let StmtAssert {
range: _,
test,
msg,
} = item;

write!(
f,
[
text("assert"),
space(),
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks)
]
)?;

if let Some(msg) = msg {
write!(
f,
[
text(","),
space(),
maybe_parenthesize_expression(msg, item, Parenthesize::IfBreaks),
]
)?;
}

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,37 +316,23 @@ long_unmergable_string_with_pragma = (

comment_string = "Long lines with inline comments should have their comments appended to the reformatted string's enclosing right parentheses." # This comment gets thrown to the top.

@@ -165,25 +165,13 @@
@@ -165,13 +165,9 @@

triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""

-assert (
- some_type_of_boolean_expression
-), "Followed by a really really really long string that is used to provide context to the AssertionError exception."
+NOT_YET_IMPLEMENTED_StmtAssert
+assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception."

-assert (
- some_type_of_boolean_expression
-), "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format(
- "formatting"
-)
+NOT_YET_IMPLEMENTED_StmtAssert

-assert some_type_of_boolean_expression, (
- "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string %s."
- % "formatting"
-)
+NOT_YET_IMPLEMENTED_StmtAssert

-assert some_type_of_boolean_expression, (
- "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic %s %s."
- % ("string", "formatting")
-)
+NOT_YET_IMPLEMENTED_StmtAssert
+assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format(
"formatting"
)

some_function_call(
"With a reallly generic name and with a really really long string that is, at some point down the line, "
@@ -221,8 +209,8 @@
@@ -221,8 +217,8 @@
func_with_bad_comma(
(
"This is a really long string argument to a function that has a trailing comma"
Expand All @@ -357,7 +343,7 @@ long_unmergable_string_with_pragma = (
)

func_with_bad_parens_that_wont_fit_in_one_line(
@@ -271,10 +259,10 @@
@@ -271,10 +267,10 @@


def foo():
Expand Down Expand Up @@ -542,13 +528,21 @@ pragma_comment_string2 = "Lines which end with an inline pragma comment of the f

triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""

NOT_YET_IMPLEMENTED_StmtAssert
assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception."

NOT_YET_IMPLEMENTED_StmtAssert
assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format(
"formatting"
)

NOT_YET_IMPLEMENTED_StmtAssert
assert some_type_of_boolean_expression, (
"Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string %s."
% "formatting"
)

NOT_YET_IMPLEMENTED_StmtAssert
assert some_type_of_boolean_expression, (
"Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic %s %s."
% ("string", "formatting")
)

some_function_call(
"With a reallly generic name and with a really really long string that is, at some point down the line, "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,6 @@ if True:
nested_no_trailing_comma = {(1, 2, 3), (4, 5, 6)}
nested_long_lines = [
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
@@ -52,10 +41,7 @@
y = {
"oneple": (1,),
}
-assert False, (
- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s"
- % bar
-)
+NOT_YET_IMPLEMENTED_StmtAssert
# looping over a 1-tuple should also not get wrapped
for x in (1,):
```

## Ruff Output
Expand Down Expand Up @@ -170,7 +158,10 @@ x = {"oneple": (1,)}
y = {
"oneple": (1,),
}
NOT_YET_IMPLEMENTED_StmtAssert
assert False, (
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s"
% bar
)
# looping over a 1-tuple should also not get wrapped
for x in (1,):
Expand Down
Loading

0 comments on commit a961f75

Please sign in to comment.