Skip to content

Commit

Permalink
Format assert statement
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Jun 17, 2023
1 parent 653a0eb commit 239daa7
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ if True:
+["NOT_YET_IMPLEMENTED_STRING", "NOT_YET_IMPLEMENTED_STRING" % (foo,)]
+x = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value}
+y = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value}
+NOT_YET_IMPLEMENTED_StmtAssert
+assert False, "NOT_YET_IMPLEMENTED_STRING" % bar
# looping over a 1-tuple should also not get wrapped
-for x in (1,):
Expand Down Expand Up @@ -250,7 +250,7 @@ nested_long_lines = [
["NOT_YET_IMPLEMENTED_STRING", "NOT_YET_IMPLEMENTED_STRING" % (foo,)]
x = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value}
y = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value}
NOT_YET_IMPLEMENTED_StmtAssert
assert False, "NOT_YET_IMPLEMENTED_STRING" % bar
# looping over a 1-tuple should also not get wrapped
NOT_YET_IMPLEMENTED_StmtFor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def g():
```diff
--- Black
+++ Ruff
@@ -1,89 +1,70 @@
@@ -1,89 +1,74 @@
-"""Docstring."""
+"NOT_YET_IMPLEMENTED_STRING"
Expand Down Expand Up @@ -134,7 +134,9 @@ def g():
return DOUBLESPACE
- assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
+ NOT_YET_IMPLEMENTED_StmtAssert
+ assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right, (
+ NOT_YET_IMPLEMENTED_ExprJoinedStr
+ )
- prev = leaf.prev_sibling
- if not prev:
Expand Down Expand Up @@ -202,7 +204,9 @@ def g():
# Another comment because more comments
- assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
+ NOT_YET_IMPLEMENTED_StmtAssert
+ assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right, (
+ NOT_YET_IMPLEMENTED_ExprJoinedStr
+ )
- prev = leaf.prev_sibling
- if not prev:
Expand Down Expand Up @@ -251,7 +255,9 @@ def f():
if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: # another trailing comment
return DOUBLESPACE
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right, (
NOT_YET_IMPLEMENTED_ExprJoinedStr
)
prev = leaf.NOT_IMPLEMENTED_attr
if NOT_YET_IMPLEMENTED_ExprUnaryOp:
Expand Down Expand Up @@ -289,7 +295,9 @@ def g():
return DOUBLESPACE
# Another comment because more comments
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right, (
NOT_YET_IMPLEMENTED_ExprJoinedStr
)
prev = leaf.NOT_IMPLEMENTED_attr
if NOT_YET_IMPLEMENTED_ExprUnaryOp:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ last_call()
Name
None
True
@@ -7,294 +8,236 @@
@@ -7,294 +8,238 @@
1
1.0
1j
Expand Down Expand Up @@ -538,9 +538,6 @@ last_call()
- int,
- float,
- dict[str, int],
-]
-very_long_variable_name_filters: t.List[
- t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
+[
+ 1,
+ 2,
Expand All @@ -555,6 +552,9 @@ last_call()
+ NOT_IMPLEMENTED_bool_op1 and NOT_IMPLEMENTED_bool_op2,
+ NOT_IMPLEMENTED_bool_op1 and NOT_IMPLEMENTED_bool_op2,
]
-very_long_variable_name_filters: t.List[
- t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
-]
-xxxx_xxxxx_xxxx_xxx: Callable[..., List[SomeClass]] = classmethod( # type: ignore
- sync(async_xxxx_xxx_xxxx_xxxxx_xxxx_xxx.__func__)
-)
Expand Down Expand Up @@ -710,9 +710,11 @@ last_call()
+NOT_IMPLEMENTED_call()
+NOT_IMPLEMENTED_call()
+NOT_IMPLEMENTED_call()
+NOT_YET_IMPLEMENTED_StmtAssert
+NOT_YET_IMPLEMENTED_StmtAssert
+NOT_YET_IMPLEMENTED_StmtAssert
+assert NOT_YET_IMPLEMENTED_ExprUnaryOp, "NOT_YET_IMPLEMENTED_STRING"
+assert (
+ NOT_IMPLEMENTED_bool_op1 and NOT_IMPLEMENTED_bool_op2
+), "NOT_YET_IMPLEMENTED_STRING"
+assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
+NOT_YET_IMPLEMENTED_StmtFor
+NOT_YET_IMPLEMENTED_StmtFor
+NOT_YET_IMPLEMENTED_StmtFor
Expand Down Expand Up @@ -758,7 +760,7 @@ last_call()
return True
if (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
@@ -327,24 +270,44 @@
@@ -327,24 +272,44 @@
):
return True
if (
Expand Down Expand Up @@ -815,7 +817,7 @@ last_call()
):
return True
(
@@ -363,8 +326,9 @@
@@ -363,8 +328,9 @@
bbbb >> bbbb * bbbb
(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Expand Down Expand Up @@ -1056,9 +1058,11 @@ async def f():
NOT_IMPLEMENTED_call()
NOT_IMPLEMENTED_call()
NOT_IMPLEMENTED_call()
NOT_YET_IMPLEMENTED_StmtAssert
NOT_YET_IMPLEMENTED_StmtAssert
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_YET_IMPLEMENTED_ExprUnaryOp, "NOT_YET_IMPLEMENTED_STRING"
assert (
NOT_IMPLEMENTED_bool_op1 and NOT_IMPLEMENTED_bool_op2
), "NOT_YET_IMPLEMENTED_STRING"
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
NOT_YET_IMPLEMENTED_StmtFor
NOT_YET_IMPLEMENTED_StmtFor
NOT_YET_IMPLEMENTED_StmtFor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_calculate_fades():
- """Hey, ho."""
- assert test.passed()
+ "NOT_YET_IMPLEMENTED_STRING"
+ NOT_YET_IMPLEMENTED_StmtAssert
+ assert NOT_IMPLEMENTED_call()
+
def test_calculate_fades():
Expand Down Expand Up @@ -138,7 +138,7 @@ def verify_fader(test):
def verify_fader(test):
"NOT_YET_IMPLEMENTED_STRING"
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_call()
def test_calculate_fades():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ d={'a':1,
+ i="NOT_YET_IMPLEMENTED_STRING",
+):
+ offset = NOT_IMPLEMENTED_call()
+ NOT_YET_IMPLEMENTED_StmtAssert
+ assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
def spaces_types(
Expand Down Expand Up @@ -571,7 +571,7 @@ def spaces(
i="NOT_YET_IMPLEMENTED_STRING",
):
offset = NOT_IMPLEMENTED_call()
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
def spaces_types(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __await__(): return (yield)


def func_no_args():
@@ -14,135 +13,87 @@
@@ -14,135 +13,90 @@
b
c
if True:
Expand Down Expand Up @@ -185,7 +185,7 @@ def __await__(): return (yield)
+ i="NOT_YET_IMPLEMENTED_STRING",
+):
+ offset = NOT_IMPLEMENTED_call()
+ NOT_YET_IMPLEMENTED_StmtAssert
+ assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right


def spaces_types(
Expand All @@ -210,7 +210,10 @@ def __await__(): return (yield)
-def spaces2(result=_core.Value(None)):
- assert fut is self._read_fut, (fut, self._read_fut)
+def spaces2(result=NOT_IMPLEMENTED_call()):
+ NOT_YET_IMPLEMENTED_StmtAssert
+ assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right, (
+ fut,
+ self.NOT_IMPLEMENTED_attr,
+ )


def example(session):
Expand Down Expand Up @@ -358,7 +361,7 @@ def spaces(
i="NOT_YET_IMPLEMENTED_STRING",
):
offset = NOT_IMPLEMENTED_call()
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right


def spaces_types(
Expand All @@ -376,7 +379,10 @@ def spaces_types(


def spaces2(result=NOT_IMPLEMENTED_call()):
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right, (
fut,
self.NOT_IMPLEMENTED_attr,
)


def example(session):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ assert (
- "3b": set(),
- }
-) == ["2a", "2b", "2", "3a", "3b", "3", "1"]
+NOT_YET_IMPLEMENTED_StmtAssert
+assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
importA
0
Expand Down Expand Up @@ -104,7 +104,7 @@ assert (
- very_long_arguments_that_surpass_the_limit,
- which_is_eighty_eight_in_this_case_plus_a_bit_more,
-) == {"x": "this need to pass the line limit as well", "b": "but only by a little bit"}
+NOT_YET_IMPLEMENTED_StmtAssert
+assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
```
## Ruff Output
Expand All @@ -117,7 +117,7 @@ importA
**101234234242352525425252352352525234890264906820496920680926538059059209922523523525
) #
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
importA
0
Expand All @@ -131,7 +131,7 @@ def test(self, othr):
return NOT_IMPLEMENTED_bool_op1 and NOT_IMPLEMENTED_bool_op2
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
```
## Black Output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(
- long_module.long_class.long_func().another_func()
- == long_module.long_class.long_func()["some_key"].another_func(arg1)
-)
+NOT_YET_IMPLEMENTED_StmtAssert
+assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
# Regression test for https://github.com/psf/black/issues/3414.
-assert xxxxxxxxx.xxxxxxxxx.xxxxxxxxx(
- xxxxxxxxx
-).xxxxxxxxxxxxxxxxxx(), (
- "xxx {xxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-)
+NOT_YET_IMPLEMENTED_StmtAssert
+assert NOT_IMPLEMENTED_call(), "NOT_YET_IMPLEMENTED_STRING"
```
## Ruff Output
Expand Down Expand Up @@ -131,10 +131,10 @@ def refresh_token(self, device_family, refresh_token, api_key):
# Edge case where a bug in a working-in-progress version of
# https://github.com/psf/black/pull/3370 causes an infinite recursion.
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right
# Regression test for https://github.com/psf/black/issues/3414.
NOT_YET_IMPLEMENTED_StmtAssert
assert NOT_IMPLEMENTED_call(), "NOT_YET_IMPLEMENTED_STRING"
```
## Black Output
Expand Down
32 changes: 30 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,6 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::expression::parentheses::Parenthesize;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAssert;

Expand All @@ -7,6 +9,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;
if let Some(msg) = msg {
write!(
f,
[
text("assert"),
space(),
test.format().with_options(Parenthesize::IfBreaks),
text(","),
space(),
msg.format().with_options(Parenthesize::IfBreaks)
]
)
} else {
write!(
f,
[
text("assert"),
space(),
test.format().with_options(Parenthesize::IfBreaks)
]
)
}
}
}

0 comments on commit 239daa7

Please sign in to comment.