From 3e12bdff45d268b4bc19c373603476273d58da69 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 23 Jun 2023 09:35:29 +0200 Subject: [PATCH] Format Compare Op ## Summary This PR adds basic formatting for compare operations. The implementation currently breaks diffeently when nesting binary like expressions. I haven't yet figured out what Black's logic is in that case but I think that this by itself is already an improvement worth merging. ## Test Plan I added a few new tests --- .../test/fixtures/ruff/expression/compare.py | 61 ++++++ .../src/expression/expr_compare.rs | 156 +++++++++++++-- .../src/expression/mod.rs | 2 +- ...r__tests__black_test__bracketmatch_py.snap | 10 +- ...tter__tests__black_test__comments2_py.snap | 28 +-- ...tter__tests__black_test__comments3_py.snap | 5 +- ...tter__tests__black_test__comments5_py.snap | 15 +- ...er__tests__black_test__empty_lines_py.snap | 128 ++++++++---- ...ter__tests__black_test__expression_py.snap | 144 +++++++------ ...tter__tests__black_test__fmtonoff5_py.snap | 20 +- ...atter__tests__black_test__fmtonoff_py.snap | 4 +- ...atter__tests__black_test__fmtskip5_py.snap | 18 +- ...tter__tests__black_test__function2_py.snap | 8 +- ...lack_test__function_trailing_comma_py.snap | 50 +++-- ...ests__black_test__power_op_spacing_py.snap | 18 +- ...move_newline_after_code_block_open_py.snap | 16 +- ...rmatter__tests__black_test__slices_py.snap | 5 +- ...matter__tests__black_test__torture_py.snap | 48 ++++- ...t__trailing_comma_optional_parens1_py.snap | 24 ++- ...t__trailing_comma_optional_parens2_py.snap | 8 +- ...sts__ruff_test__expression__binary_py.snap | 8 +- ...ts__ruff_test__expression__compare_py.snap | 189 ++++++++++++++++++ ...r__tests__ruff_test__statement__if_py.snap | 10 +- ...ormatter__tests__ruff_test__trivia_py.snap | 4 +- 24 files changed, 735 insertions(+), 244 deletions(-) create mode 100644 crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/compare.py create mode 100644 crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__compare_py.snap diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/compare.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/compare.py new file mode 100644 index 0000000000..906d5710aa --- /dev/null +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/compare.py @@ -0,0 +1,61 @@ +a == b +a != b +a < b +a <= b +a > b +a >= b +a is b +a is not b +a in b +a not in b + +(a == + # comment + b +) + +(a == # comment + b + ) + +a < b > c == d + +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb > ccccccccccccccccccccccccccccc == ddddddddddddddddddddd + +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa < [ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + ff, +] < [ccccccccccccccccccccccccccccc, dddd] < ddddddddddddddddddddddddddddddddddddddddddd + +return 1 == 2 and ( + name, + description, + self_default, + self_selected, + self_auto_generated, + self_parameters, + self_meta_data, + self_schedule, +) == ( + name, + description, + othr_default, + othr_selected, + othr_auto_generated, + othr_parameters, + othr_meta_data, + othr_schedule, +) + +(name, description, self_default, self_selected, self_auto_generated, self_parameters, self_meta_data, self_schedule) == (name, description, other_default, othr_selected, othr_auto_generated, othr_parameters, othr_meta_data, othr_schedule) +((name, description, self_default, self_selected, self_auto_generated, self_parameters, self_meta_data, self_schedule) == (name, description, other_default, othr_selected, othr_auto_generated, othr_parameters, othr_meta_data, othr_schedule)) + +[ + ( + a + + [ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + ] + >= c + ) +] diff --git a/crates/ruff_python_formatter/src/expression/expr_compare.rs b/crates/ruff_python_formatter/src/expression/expr_compare.rs index 54c5301e85..d9031357a0 100644 --- a/crates/ruff_python_formatter/src/expression/expr_compare.rs +++ b/crates/ruff_python_formatter/src/expression/expr_compare.rs @@ -1,23 +1,96 @@ +use crate::comments::{leading_comments, Comments}; +use crate::expression::binary_like::{BinaryLayout, FormatBinaryLike}; use crate::expression::parentheses::{ default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize, }; -use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter}; - -use crate::comments::Comments; -use ruff_formatter::{write, Buffer, FormatResult}; -use rustpython_parser::ast::ExprCompare; +use crate::prelude::*; +use crate::FormatNodeRule; +use ruff_formatter::{ + write, FormatError, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions, +}; +use ruff_python_ast::prelude::Expr; +use rustpython_parser::ast::{CmpOp, ExprCompare}; #[derive(Default)] -pub struct FormatExprCompare; +pub struct FormatExprCompare { + parentheses: Option, +} + +impl FormatRuleWithOptions> for FormatExprCompare { + type Options = Option; + + fn with_options(mut self, options: Self::Options) -> Self { + self.parentheses = options; + self + } +} impl FormatNodeRule for FormatExprCompare { - fn fmt_fields(&self, _item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> { - write!( - f, - [not_yet_implemented_custom_text( - "NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right" - )] - ) + fn fmt_fields(&self, item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> { + item.fmt_binary(self.parentheses, f) + } +} + +impl<'ast> FormatBinaryLike<'ast> for ExprCompare { + type FormatOperator = FormatOwnedWithRule>; + + fn binary_layout(&self) -> BinaryLayout { + if self.ops.len() == 1 { + match self.comparators.as_slice() { + [right] => BinaryLayout::from_left_right(&self.left, right), + [..] => BinaryLayout::Default, + } + } else { + BinaryLayout::Default + } + } + + fn fmt_default(&self, f: &mut PyFormatter<'ast, '_>) -> FormatResult<()> { + let ExprCompare { + range: _, + left, + ops, + comparators, + } = self; + + let comments = f.context().comments().clone(); + + write!(f, [group(&left.format())])?; + + assert_eq!(comparators.len(), ops.len()); + + for (operator, comparator) in ops.iter().zip(comparators) { + let leading_comparator_comments = comments.leading_comments(comparator); + if leading_comparator_comments.is_empty() { + write!(f, [soft_line_break_or_space()])?; + } else { + // Format the expressions leading comments **before** the operator + write!( + f, + [ + hard_line_break(), + leading_comments(leading_comparator_comments) + ] + )?; + } + + write!(f, [operator.format(), space(), group(&comparator.format())])?; + } + + Ok(()) + } + + fn left(&self) -> FormatResult<&Expr> { + Ok(self.left.as_ref()) + } + + fn right(&self) -> FormatResult<&Expr> { + self.comparators.last().ok_or(FormatError::SyntaxError) + } + + fn operator(&self) -> Self::FormatOperator { + let op = *self.ops.first().unwrap(); + op.into_format() } } @@ -28,6 +101,61 @@ impl NeedsParentheses for ExprCompare { source: &str, comments: &Comments, ) -> Parentheses { - default_expression_needs_parentheses(self.into(), parenthesize, source, comments) + match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) { + parentheses @ Parentheses::Optional => match self.binary_layout() { + BinaryLayout::Default => parentheses, + + BinaryLayout::ExpandRight + | BinaryLayout::ExpandLeft + | BinaryLayout::ExpandRightThenLeft + if self + .comparators + .last() + .map_or(false, |right| comments.has_leading_comments(right)) => + { + parentheses + } + _ => Parentheses::Custom, + }, + parentheses => parentheses, + } + } +} + +#[derive(Copy, Clone)] +pub struct FormatCmpOp; + +impl<'ast> AsFormat> for CmpOp { + type Format<'a> = FormatRefWithRule<'a, CmpOp, FormatCmpOp, PyFormatContext<'ast>>; + + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new(self, FormatCmpOp) + } +} + +impl<'ast> IntoFormat> for CmpOp { + type Format = FormatOwnedWithRule>; + + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new(self, FormatCmpOp) + } +} + +impl FormatRule> for FormatCmpOp { + fn fmt(&self, item: &CmpOp, f: &mut Formatter>) -> FormatResult<()> { + let operator = match item { + CmpOp::Eq => "==", + CmpOp::NotEq => "!=", + CmpOp::Lt => "<", + CmpOp::LtE => "<=", + CmpOp::Gt => ">", + CmpOp::GtE => ">=", + CmpOp::Is => "is", + CmpOp::IsNot => "is not", + CmpOp::In => "in", + CmpOp::NotIn => "not in", + }; + + text(operator).fmt(f) } } diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 6637c091c6..46214396fc 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -76,7 +76,7 @@ impl FormatRule> for FormatExpr { Expr::Await(expr) => expr.format().fmt(f), Expr::Yield(expr) => expr.format().fmt(f), Expr::YieldFrom(expr) => expr.format().fmt(f), - Expr::Compare(expr) => expr.format().fmt(f), + Expr::Compare(expr) => expr.format().with_options(Some(parentheses)).fmt(f), Expr::Call(expr) => expr.format().fmt(f), Expr::FormattedValue(expr) => expr.format().fmt(f), Expr::JoinedStr(expr) => expr.format().fmt(f), diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__bracketmatch_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__bracketmatch_py.snap index 2a11007f5d..a04acefcee 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__bracketmatch_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__bracketmatch_py.snap @@ -19,11 +19,9 @@ lambda x=lambda y={1: 3}: y['x':lambda y: {1: 2}]: x ```diff --- Black +++ Ruff -@@ -1,4 +1,6 @@ +@@ -1,4 +1,4 @@ -for ((x in {}) or {})["a"] in x: -+for ((NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right) or {})[ -+ "NOT_YET_IMPLEMENTED_STRING" -+] in x: ++for ((x in {}) or {})["NOT_YET_IMPLEMENTED_STRING"] in x: pass -pem_spam = lambda l, spam={"x": 3}: not spam.get(l.strip()) -lambda x=lambda y={1: 3}: y["x" : lambda y: {1: 2}]: x @@ -34,9 +32,7 @@ lambda x=lambda y={1: 3}: y['x':lambda y: {1: 2}]: x ## Ruff Output ```py -for ((NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right) or {})[ - "NOT_YET_IMPLEMENTED_STRING" -] in x: +for ((x in {}) or {})["NOT_YET_IMPLEMENTED_STRING"] in x: pass pem_spam = lambda x: True lambda x: True diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments2_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments2_py.snap index 5454f791b8..6e43e9b850 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments2_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments2_py.snap @@ -249,7 +249,7 @@ instruction()#comment with bad spacing -if "PYTHON" in os.environ: - add_compiler(compiler_from_env()) -+if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++if "NOT_YET_IMPLEMENTED_STRING" in os.environ: + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) else: # for compiler in compilers.values(): @@ -283,15 +283,13 @@ instruction()#comment with bad spacing + parameters.children[-1], + ] # type: ignore if ( -- self._proc is not None -+ NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + self._proc is not None # has the child process finished? -- and self._returncode is None -+ and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + and self._returncode is None # the child process has finished, but the # transport hasn't been notified yet? - and self._proc.poll() is None -+ and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++ and NOT_IMPLEMENTED_call() is None ): pass # no newline before or after @@ -349,7 +347,7 @@ instruction()#comment with bad spacing while True: if False: continue -@@ -141,24 +111,18 @@ +@@ -141,24 +111,19 @@ # and round and round we go # let's return @@ -374,13 +372,14 @@ instruction()#comment with bad spacing def _init_host(self, parsed) -> None: - if parsed.hostname is None or not parsed.hostname.strip(): # type: ignore + if ( -+ NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right # type: ignore ++ parsed.hostname ++ is None # type: ignore + or not NOT_IMPLEMENTED_call() + ): pass -@@ -167,7 +131,7 @@ +@@ -167,7 +132,7 @@ ####################### @@ -440,7 +439,7 @@ not_shareables = [ NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg), ] -if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +if "NOT_YET_IMPLEMENTED_STRING" in os.environ: NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) else: # for compiler in compilers.values(): @@ -474,12 +473,12 @@ def inline_comments_in_brackets_ruin_everything(): parameters.children[-1], ] # type: ignore if ( - NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + self._proc is not None # has the child process finished? - and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + and self._returncode is None # the child process has finished, but the # transport hasn't been notified yet? - and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + and NOT_IMPLEMENTED_call() is None ): pass # no newline before or after @@ -516,7 +515,8 @@ CONFIG_FILES = [CONFIG_FILE] + SHARED_CONFIG_FILES + USER_CONFIG_FILES # type: class Test: def _init_host(self, parsed) -> None: if ( - NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right # type: ignore + parsed.hostname + is None # type: ignore or not NOT_IMPLEMENTED_call() ): pass diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments3_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments3_py.snap index aa92645b7a..ef02e64a10 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments3_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments3_py.snap @@ -83,7 +83,7 @@ def func(): + if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg): embedded = [] for exc in exc_value.exceptions: -- if exc not in _seen: + if exc not in _seen: - embedded.append( - # This should be left alone (before) - traceback.TracebackException.from_exception( @@ -97,7 +97,6 @@ def func(): - ) - # This should be left alone (after) - ) -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) # everything is fine if the expression isn't nested @@ -130,7 +129,7 @@ def func(): if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg): embedded = [] for exc in exc_value.exceptions: - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if exc not in _seen: NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) # everything is fine if the expression isn't nested diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments5_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments5_py.snap index 6273ad1e97..762929aa96 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments5_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__comments5_py.snap @@ -101,8 +101,7 @@ if __name__ == "__main__": -for i in range(100): +for i in NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg): # first we do this -- if i % 33 == 0: -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if i % 33 == 0: break # then we do this @@ -117,11 +116,11 @@ if __name__ == "__main__": -try: - with open(some_other_file) as w: - w.write(data) -- --except OSError: -- print("problems") +NOT_YET_IMPLEMENTED_StmtTry +-except OSError: +- print("problems") +- -import sys +NOT_YET_IMPLEMENTED_StmtImport @@ -151,7 +150,7 @@ if __name__ == "__main__": -if __name__ == "__main__": - main() -+if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++if __name__ == "NOT_YET_IMPLEMENTED_STRING": + NOT_IMPLEMENTED_call() ``` @@ -170,7 +169,7 @@ while True: for i in NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg): # first we do this - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if i % 33 == 0: break # then we do this @@ -223,7 +222,7 @@ def g(): ... -if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +if __name__ == "NOT_YET_IMPLEMENTED_STRING": NOT_IMPLEMENTED_call() ``` diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__empty_lines_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__empty_lines_py.snap index a9b2674d3a..4bb4e71127 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__empty_lines_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__empty_lines_py.snap @@ -105,7 +105,7 @@ def g(): ```diff --- Black +++ Ruff -@@ -1,59 +1,46 @@ +@@ -1,11 +1,11 @@ -"""Docstring.""" +"NOT_YET_IMPLEMENTED_STRING" @@ -121,13 +121,8 @@ def g(): t = leaf.type p = leaf.parent # trailing comment - v = leaf.value - -- if t in ALWAYS_NO_SPACE: -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: - pass -- if t == token.COMMENT: # another trailing comment -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: # another trailing comment +@@ -16,44 +16,51 @@ + if t == token.COMMENT: # another trailing comment return DOUBLESPACE - assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}" @@ -136,12 +131,11 @@ def g(): prev = leaf.prev_sibling if not prev: - prevp = preceding_leaf(p) -- if not prevp or prevp.type in OPENING_BRACKETS: + prevp = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -+ if not prevp or NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if not prevp or prevp.type in OPENING_BRACKETS: return NO -- if prevp.type == token.EQUAL: + if prevp.type == token.EQUAL: - if prevp.parent and prevp.parent.type in { - syms.typedargslist, - syms.varargslist, @@ -149,11 +143,20 @@ def g(): - syms.arglist, - syms.argument, - }: -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: -+ if prevp.parent and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++ if ( ++ prevp.parent ++ and prevp.parent.type ++ in { ++ syms.typedargslist, ++ syms.varargslist, ++ syms.parameters, ++ syms.arglist, ++ syms.argument, ++ } ++ ): return NO -- elif prevp.type == token.DOUBLESTAR: + elif prevp.type == token.DOUBLESTAR: - if prevp.parent and prevp.parent.type in { - syms.typedargslist, - syms.varargslist, @@ -161,8 +164,17 @@ def g(): - syms.arglist, - syms.dictsetmaker, - }: -+ elif NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: -+ if prevp.parent and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++ if ( ++ prevp.parent ++ and prevp.parent.type ++ in { ++ syms.typedargslist, ++ syms.varargslist, ++ syms.parameters, ++ syms.arglist, ++ syms.dictsetmaker, ++ } ++ ): return NO @@ -181,15 +193,7 @@ def g(): t = leaf.type p = leaf.parent -@@ -61,29 +48,23 @@ - - # Comment because comments - -- if t in ALWAYS_NO_SPACE: -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: - pass -- if t == token.COMMENT: -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +@@ -67,11 +74,11 @@ return DOUBLESPACE # Another comment because more comments @@ -201,13 +205,12 @@ def g(): - prevp = preceding_leaf(p) + prevp = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -- if not prevp or prevp.type in OPENING_BRACKETS: -+ if not prevp or NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if not prevp or prevp.type in OPENING_BRACKETS: # Start of the line or a bracketed expression. - # More than one line for the comment. +@@ -79,11 +86,15 @@ return NO -- if prevp.type == token.EQUAL: + if prevp.type == token.EQUAL: - if prevp.parent and prevp.parent.type in { - syms.typedargslist, - syms.varargslist, @@ -215,8 +218,17 @@ def g(): - syms.arglist, - syms.argument, - }: -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: -+ if prevp.parent and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++ if ( ++ prevp.parent ++ and prevp.parent.type ++ in { ++ syms.typedargslist, ++ syms.varargslist, ++ syms.parameters, ++ syms.arglist, ++ syms.argument, ++ } ++ ): return NO ``` @@ -236,9 +248,9 @@ def f(): p = leaf.parent # trailing comment v = leaf.value - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if t in ALWAYS_NO_SPACE: pass - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: # another trailing comment + if t == token.COMMENT: # another trailing comment return DOUBLESPACE NOT_YET_IMPLEMENTED_StmtAssert @@ -246,15 +258,35 @@ def f(): prev = leaf.prev_sibling if not prev: prevp = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) - if not prevp or NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if not prevp or prevp.type in OPENING_BRACKETS: return NO - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: - if prevp.parent and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if prevp.type == token.EQUAL: + if ( + prevp.parent + and prevp.parent.type + in { + syms.typedargslist, + syms.varargslist, + syms.parameters, + syms.arglist, + syms.argument, + } + ): return NO - elif NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: - if prevp.parent and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + elif prevp.type == token.DOUBLESTAR: + if ( + prevp.parent + and prevp.parent.type + in { + syms.typedargslist, + syms.varargslist, + syms.parameters, + syms.arglist, + syms.dictsetmaker, + } + ): return NO @@ -273,9 +305,9 @@ def g(): # Comment because comments - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if t in ALWAYS_NO_SPACE: pass - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if t == token.COMMENT: return DOUBLESPACE # Another comment because more comments @@ -285,13 +317,23 @@ def g(): if not prev: prevp = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) - if not prevp or NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if not prevp or prevp.type in OPENING_BRACKETS: # Start of the line or a bracketed expression. # More than one line for the comment. return NO - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: - if prevp.parent and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if prevp.type == token.EQUAL: + if ( + prevp.parent + and prevp.parent.type + in { + syms.typedargslist, + syms.varargslist, + syms.parameters, + syms.arglist, + syms.argument, + } + ): return NO ``` diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__expression_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__expression_py.snap index b049cca61c..57371f8bf4 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__expression_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__expression_py.snap @@ -281,7 +281,8 @@ last_call() ~int and not v1 ^ 123 + v2 | True (~int) and (not ((v1 ^ (123 + v2)) | True)) -+(really ** -(confusing ** ~(operator**-precedence))) --flags & ~select.EPOLLIN and waiters.write_task is not None +++really ** -confusing ** ~operator**-precedence + flags & ~select.EPOLLIN and waiters.write_task is not None -lambda arg: None -lambda a=True: a -lambda a, b, c=True: a @@ -304,8 +305,6 @@ last_call() -) -{"2.7": dead, "3.7": (long_live or die_hard)} -{"2.7": dead, "3.7": (long_live or die_hard), **{"3.6": verygood}} -++really ** -confusing ** ~operator**-precedence -+flags & ~select.EPOLLIN and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +lambda x: True +lambda x: True +lambda x: True @@ -364,17 +363,17 @@ last_call() - 4, - 5, -] +-[ +- 4, +- *a, +- 5, +-] +[1, 2, 3] +[NOT_YET_IMPLEMENTED_ExprStarred] +[NOT_YET_IMPLEMENTED_ExprStarred] +[NOT_YET_IMPLEMENTED_ExprStarred, 4, 5] +[4, NOT_YET_IMPLEMENTED_ExprStarred, 5] [ -- 4, -- *a, -- 5, --] --[ this_is_a_very_long_variable_which_will_force_a_delimiter_split, element, another, @@ -397,8 +396,21 @@ last_call() - k: v - for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension -} --Python3 > Python2 > COBOL --Life is Life ++NOT_YET_IMPLEMENTED_ExprSetComp ++NOT_YET_IMPLEMENTED_ExprSetComp ++NOT_YET_IMPLEMENTED_ExprSetComp ++NOT_YET_IMPLEMENTED_ExprSetComp ++[i for i in []] ++[i for i in []] ++[i for i in []] ++[i for i in []] ++{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} ++{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} ++{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} ++{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} ++{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} + Python3 > Python2 > COBOL + Life is Life -call() -call(arg) -call(kwarg="hey") @@ -415,21 +427,6 @@ last_call() -call(a, *gidgets[:2]) -call(**self.screen_kwargs) -call(b, **self.screen_kwargs) -+NOT_YET_IMPLEMENTED_ExprSetComp -+NOT_YET_IMPLEMENTED_ExprSetComp -+NOT_YET_IMPLEMENTED_ExprSetComp -+NOT_YET_IMPLEMENTED_ExprSetComp -+[i for i in []] -+[i for i in []] -+[i for i in []] -+[i for i in []] -+{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} -+{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} -+{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} -+{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} -+{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} -+NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +NOT_IMPLEMENTED_call() +NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) +NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) @@ -556,19 +553,10 @@ last_call() -g = 1, *"ten" -what_is_up_with_those_new_coord_names = (coord_names + set(vars_to_create)) + set( - vars_to_remove -+e = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -+f = 1, NOT_YET_IMPLEMENTED_ExprStarred -+g = 1, NOT_YET_IMPLEMENTED_ExprStarred -+what_is_up_with_those_new_coord_names = ( -+ (coord_names + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)) -+ + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) - ) +-) -what_is_up_with_those_new_coord_names = (coord_names | set(vars_to_create)) - set( - vars_to_remove -+what_is_up_with_those_new_coord_names = ( -+ (coord_names | NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)) -+ - NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) - ) +-) -result = ( - session.query(models.Customer.id) - .filter( @@ -576,7 +564,13 @@ last_call() - ) - .order_by(models.Customer.id.asc()) - .all() --) ++e = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) ++f = 1, NOT_YET_IMPLEMENTED_ExprStarred ++g = 1, NOT_YET_IMPLEMENTED_ExprStarred ++what_is_up_with_those_new_coord_names = ( ++ (coord_names + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)) ++ + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) + ) -result = ( - session.query(models.Customer.id) - .filter( @@ -586,7 +580,10 @@ last_call() - models.Customer.id.asc(), - ) - .all() --) ++what_is_up_with_those_new_coord_names = ( ++ (coord_names | NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg)) ++ - NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) + ) -Ø = set() -authors.łukasz.say_thanks() +result = NOT_IMPLEMENTED_call() @@ -641,44 +638,29 @@ last_call() ... for j in 1 + (2 + 3): ... -@@ -272,28 +254,16 @@ +@@ -272,7 +254,7 @@ addr_proto, addr_canonname, addr_sockaddr, -) in socket.getaddrinfo("google.com", "http"): +) in NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg): pass --a = ( -- aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp -- in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz --) --a = ( -- aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp -- not in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz --) --a = ( -- aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp -- is qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz --) --a = ( -- aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp -- is not qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz --) -+a = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+a = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+a = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+a = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + a = ( + aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp +@@ -291,9 +273,9 @@ + is not qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz + ) if ( - threading.current_thread() != threading.main_thread() - and threading.current_thread() != threading.main_thread() - or signal.getsignal(signal.SIGINT) != signal.default_int_handler -+ NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+ and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+ or NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++ NOT_IMPLEMENTED_call() != NOT_IMPLEMENTED_call() ++ and NOT_IMPLEMENTED_call() != NOT_IMPLEMENTED_call() ++ or NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) != signal.default_int_handler ): return True if ( -@@ -327,13 +297,18 @@ +@@ -327,13 +309,18 @@ ): return True if ( @@ -700,7 +682,7 @@ last_call() ^ aaaaaaaa.i << aaaaaaaa.k >> aaaaaaaa.l**aaaaaaaa.m // aaaaaaaa.n ): return True -@@ -341,7 +316,8 @@ +@@ -341,7 +328,8 @@ ~aaaaaaaaaaaaaaaa.a + aaaaaaaaaaaaaaaa.b - aaaaaaaaaaaaaaaa.c * aaaaaaaaaaaaaaaa.d @ aaaaaaaaaaaaaaaa.e @@ -710,7 +692,7 @@ last_call() ^ aaaaaaaaaaaaaaaa.i << aaaaaaaaaaaaaaaa.k >> aaaaaaaaaaaaaaaa.l**aaaaaaaaaaaaaaaa.m // aaaaaaaaaaaaaaaa.n -@@ -366,5 +342,5 @@ +@@ -366,5 +354,5 @@ ^ bbbb.a & aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ^ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ) @@ -756,7 +738,7 @@ not great ~int and not v1 ^ 123 + v2 | True (~int) and (not ((v1 ^ (123 + v2)) | True)) +really ** -confusing ** ~operator**-precedence -flags & ~select.EPOLLIN and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +flags & ~select.EPOLLIN and waiters.write_task is not None lambda x: True lambda x: True lambda x: True @@ -825,8 +807,8 @@ NOT_YET_IMPLEMENTED_ExprSetComp {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} -NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +Python3 > Python2 > COBOL +Life is Life NOT_IMPLEMENTED_call() NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) @@ -980,14 +962,26 @@ for ( addr_sockaddr, ) in NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg): pass -a = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -a = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -a = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -a = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +a = ( + aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp + in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz +) +a = ( + aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp + not in qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz +) +a = ( + aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp + is qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz +) +a = ( + aaaa.bbbb.cccc.dddd.eeee.ffff.gggg.hhhh.iiii.jjjj.kkkk.llll.mmmm.nnnn.oooo.pppp + is not qqqq.rrrr.ssss.tttt.uuuu.vvvv.xxxx.yyyy.zzzz +) if ( - NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right - and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right - or NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + NOT_IMPLEMENTED_call() != NOT_IMPLEMENTED_call() + and NOT_IMPLEMENTED_call() != NOT_IMPLEMENTED_call() + or NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) != signal.default_int_handler ): return True if ( diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtonoff5_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtonoff5_py.snap index cc7ee04dd8..b76dbc5ab7 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtonoff5_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtonoff5_py.snap @@ -152,21 +152,22 @@ elif unformatted: # Regression test for https://github.com/psf/black/issues/3184. -@@ -52,29 +34,27 @@ - async def call(param): +@@ -53,28 +35,29 @@ if param: # fmt: off -- if param[0:4] in ( + if param[0:4] in ( - "ABCD", "EFGH" - ) : -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++ "NOT_YET_IMPLEMENTED_STRING", ++ "NOT_YET_IMPLEMENTED_STRING", ++ ): # fmt: on - print ( "This won't be formatted" ) + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) - elif param[0:4] in ("ZZZZ",): - print ( "This won't be formatted either" ) -+ elif NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++ elif param[0:4] in ("NOT_YET_IMPLEMENTED_STRING",): + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) - print("This will be formatted") @@ -189,7 +190,7 @@ elif unformatted: # fmt: on -@@ -82,6 +62,6 @@ +@@ -82,6 +65,6 @@ if x: return x # fmt: off @@ -239,11 +240,14 @@ class A: async def call(param): if param: # fmt: off - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if param[0:4] in ( + "NOT_YET_IMPLEMENTED_STRING", + "NOT_YET_IMPLEMENTED_STRING", + ): # fmt: on NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) - elif NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + elif param[0:4] in ("NOT_YET_IMPLEMENTED_STRING",): NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtonoff_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtonoff_py.snap index d5245d0799..fb89db8d6f 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtonoff_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtonoff_py.snap @@ -411,7 +411,7 @@ d={'a':1, - because . the . handling . inside . generate_ignored_nodes() - now . considers . multiple . fmt . directives . within . one . prefix + this = NOT_IMPLEMENTED_call() -+ and_ = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++ and_ = indeed.it is not formatted + NOT_IMPLEMENTED_call() + now.considers.multiple.fmt.directives.within.one.prefix # fmt: on @@ -663,7 +663,7 @@ def on_and_off_broken(): # fmt: on # fmt: off this = NOT_IMPLEMENTED_call() - and_ = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + and_ = indeed.it is not formatted NOT_IMPLEMENTED_call() now.considers.multiple.fmt.directives.within.one.prefix # fmt: on diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtskip5_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtskip5_py.snap index 2247055281..32b6d86155 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtskip5_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__fmtskip5_py.snap @@ -22,15 +22,14 @@ else: ```diff --- Black +++ Ruff -@@ -1,9 +1,9 @@ +@@ -1,9 +1,10 @@ a, b, c = 3, 4, 5 if ( -- a == 3 + a == 3 - and b != 9 # fmt: skip -- and c is not None -+ NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+ and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right # fmt: skip -+ and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++ and b ++ != 9 # fmt: skip + and c is not None ): - print("I'm good!") + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) @@ -44,9 +43,10 @@ else: ```py a, b, c = 3, 4, 5 if ( - NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right - and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right # fmt: skip - and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + a == 3 + and b + != 9 # fmt: skip + and c is not None ): NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) else: diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__function2_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__function2_py.snap index 93b9b22acd..a13146444a 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__function2_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__function2_py.snap @@ -107,7 +107,7 @@ with hmm_but_this_should_get_two_preceding_newlines(): -if os.name == "posix": - import termios -+if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++if os.name == "NOT_YET_IMPLEMENTED_STRING": + NOT_YET_IMPLEMENTED_StmtImport def i_should_be_followed_by_only_one_newline(): @@ -124,7 +124,7 @@ with hmm_but_this_should_get_two_preceding_newlines(): - - def i_should_be_followed_by_only_one_newline(): - pass -+elif NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++elif os.name == "NOT_YET_IMPLEMENTED_STRING": + NOT_YET_IMPLEMENTED_StmtTry elif False: @@ -173,12 +173,12 @@ def h(): NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +if os.name == "NOT_YET_IMPLEMENTED_STRING": NOT_YET_IMPLEMENTED_StmtImport def i_should_be_followed_by_only_one_newline(): pass -elif NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +elif os.name == "NOT_YET_IMPLEMENTED_STRING": NOT_YET_IMPLEMENTED_StmtTry elif False: diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__function_trailing_comma_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__function_trailing_comma_py.snap index 965322236b..e44027943b 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__function_trailing_comma_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__function_trailing_comma_py.snap @@ -94,7 +94,7 @@ some_module.some_function( } tup = ( 1, -@@ -24,45 +24,23 @@ +@@ -24,45 +24,37 @@ def f( a: int = 1, ): @@ -112,9 +112,14 @@ some_module.some_function( - "a": 1, - "b": 2, - }["a"] -- if ( -- a -- == { ++ "NOT_YET_IMPLEMENTED_STRING": 1, ++ "NOT_YET_IMPLEMENTED_STRING": 2, ++ }[ ++ "NOT_YET_IMPLEMENTED_STRING" ++ ] + if ( + a + == { - "a": 1, - "b": 2, - "c": 3, @@ -124,13 +129,18 @@ some_module.some_function( - "g": 7, - "h": 8, - }["a"] -- ): -+ "NOT_YET_IMPLEMENTED_STRING": 1, -+ "NOT_YET_IMPLEMENTED_STRING": 2, -+ }[ -+ "NOT_YET_IMPLEMENTED_STRING" -+ ] -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++ "NOT_YET_IMPLEMENTED_STRING": 1, ++ "NOT_YET_IMPLEMENTED_STRING": 2, ++ "NOT_YET_IMPLEMENTED_STRING": 3, ++ "NOT_YET_IMPLEMENTED_STRING": 4, ++ "NOT_YET_IMPLEMENTED_STRING": 5, ++ "NOT_YET_IMPLEMENTED_STRING": 6, ++ "NOT_YET_IMPLEMENTED_STRING": 7, ++ "NOT_YET_IMPLEMENTED_STRING": 8, ++ }[ ++ "NOT_YET_IMPLEMENTED_STRING" ++ ] + ): pass @@ -152,7 +162,7 @@ some_module.some_function( } -@@ -80,35 +58,16 @@ +@@ -80,35 +72,16 @@ pass @@ -231,7 +241,21 @@ def f( }[ "NOT_YET_IMPLEMENTED_STRING" ] - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if ( + a + == { + "NOT_YET_IMPLEMENTED_STRING": 1, + "NOT_YET_IMPLEMENTED_STRING": 2, + "NOT_YET_IMPLEMENTED_STRING": 3, + "NOT_YET_IMPLEMENTED_STRING": 4, + "NOT_YET_IMPLEMENTED_STRING": 5, + "NOT_YET_IMPLEMENTED_STRING": 6, + "NOT_YET_IMPLEMENTED_STRING": 7, + "NOT_YET_IMPLEMENTED_STRING": 8, + }[ + "NOT_YET_IMPLEMENTED_STRING" + ] + ): pass diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__power_op_spacing_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__power_op_spacing_py.snap index 70995c951c..867b4314cd 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__power_op_spacing_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__power_op_spacing_py.snap @@ -99,13 +99,12 @@ return np.divide( +i = NOT_IMPLEMENTED_call() ** 5 +j = NOT_IMPLEMENTED_call().name ** 5 +k = [i for i in []] -+l = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++l = mod.weights_[0] == NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) m = [([2**63], [1, 2**63])] --n = count <= 10**5 + n = count <= 10**5 -o = settings(max_examples=10**6) -p = {(k, k**2): v**2 for k, v in pairs} -q = [10**i for i in range(6)] -+n = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +o = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) +p = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} +q = [i for i in []] @@ -131,13 +130,12 @@ return np.divide( +i = NOT_IMPLEMENTED_call() ** 5.0 +j = NOT_IMPLEMENTED_call().name ** 5.0 +k = [i for i in []] -+l = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++l = mod.weights_[0] == NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) m = [([2.0**63.0], [1.0, 2**63.0])] --n = count <= 10**5.0 + n = count <= 10**5.0 -o = settings(max_examples=10**6.0) -p = {(k, k**2): v**2.0 for k, v in pairs} -q = [10.5**i for i in range(6)] -+n = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +o = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) +p = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} +q = [i for i in []] @@ -187,9 +185,9 @@ h = 5 ** NOT_IMPLEMENTED_call() i = NOT_IMPLEMENTED_call() ** 5 j = NOT_IMPLEMENTED_call().name ** 5 k = [i for i in []] -l = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +l = mod.weights_[0] == NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) m = [([2**63], [1, 2**63])] -n = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +n = count <= 10**5 o = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) p = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} q = [i for i in []] @@ -206,9 +204,9 @@ h = 5.0 ** NOT_IMPLEMENTED_call() i = NOT_IMPLEMENTED_call() ** 5.0 j = NOT_IMPLEMENTED_call().name ** 5.0 k = [i for i in []] -l = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +l = mod.weights_[0] == NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) m = [([2.0**63.0], [1.0, 2**63.0])] -n = NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right +n = count <= 10**5.0 o = NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) p = {NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict} q = [i for i in []] diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__remove_newline_after_code_block_open_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__remove_newline_after_code_block_open_py.snap index 27eeb9155b..1bf0525ecc 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__remove_newline_after_code_block_open_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__remove_newline_after_code_block_open_py.snap @@ -179,21 +179,21 @@ with open("/path/to/file.txt", mode="r") as read_file: -if random.randint(0, 3) == 0: - print("The new line above me is about to be removed!") -+if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) == 0: + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -if random.randint(0, 3) == 0: - print("The new lines above me is about to be removed!") -+if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) == 0: + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -if random.randint(0, 3) == 0: - if random.uniform(0, 1) > 0.5: - print("Two lines above me are about to be removed!") -+if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: -+ if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: ++if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) == 0: ++ if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) > 0.5: + NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) @@ -273,16 +273,16 @@ for i in NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg): NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) == 0: NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) == 0: NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) -if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) == 0: + if NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) > 0.5: NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__slices_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__slices_py.snap index ae15220407..914dc2e5fd 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__slices_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__slices_py.snap @@ -95,10 +95,9 @@ x[ +slice[lambda x: True : lambda x: True] +slice[lambda x: True :, None::] slice[1 or 2 : True and False] --slice[not so_simple : 1 < val <= 10] + slice[not so_simple : 1 < val <= 10] -slice[(1 for i in range(42)) : x] -slice[:: [i for i in range(42)]] -+slice[not so_simple : NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right] +slice[(i for i in []) : x] +slice[ :: [i for i in []]] @@ -161,7 +160,7 @@ slice[ : -1 :] slice[lambda x: True : lambda x: True] slice[lambda x: True :, None::] slice[1 or 2 : True and False] -slice[not so_simple : NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right] +slice[not so_simple : 1 < val <= 10] slice[(i for i in []) : x] slice[ :: [i for i in []]] diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__torture_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__torture_py.snap index 5aa9c0c161..b270fd1801 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__torture_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__torture_py.snap @@ -65,7 +65,7 @@ assert ( importA 0 -@@ -24,35 +14,15 @@ +@@ -24,35 +14,34 @@ class A: def foo(self): @@ -97,8 +97,27 @@ assert ( - othr.meta_data, - othr.schedule, + return ( -+ NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+ and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++ 1 == 2 ++ and ( ++ name, ++ description, ++ self.default, ++ self.selected, ++ self.auto_generated, ++ self.parameters, ++ self.meta_data, ++ self.schedule, ++ ) ++ == ( ++ name, ++ description, ++ othr.default, ++ othr.selected, ++ othr.auto_generated, ++ othr.parameters, ++ othr.meta_data, ++ othr.schedule, ++ ) ) @@ -134,8 +153,27 @@ class A: def test(self, othr): return ( - NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right - and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + 1 == 2 + and ( + name, + description, + self.default, + self.selected, + self.auto_generated, + self.parameters, + self.meta_data, + self.schedule, + ) + == ( + name, + description, + othr.default, + othr.selected, + othr.auto_generated, + othr.parameters, + othr.meta_data, + othr.schedule, + ) ) diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__trailing_comma_optional_parens1_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__trailing_comma_optional_parens1_py.snap index c3a440c26d..f331e7a484 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__trailing_comma_optional_parens1_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__trailing_comma_optional_parens1_py.snap @@ -38,13 +38,14 @@ class A: ```diff --- Black +++ Ruff -@@ -1,34 +1,25 @@ +@@ -1,34 +1,32 @@ -if e1234123412341234.winerror not in ( - _winapi.ERROR_SEM_TIMEOUT, - _winapi.ERROR_PIPE_BUSY, -) or _check_timeout(t): +if ( -+ NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++ e1234123412341234.winerror ++ not in (_winapi.ERROR_SEM_TIMEOUT, _winapi.ERROR_PIPE_BUSY) + or NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) +): pass @@ -82,7 +83,13 @@ class A: - ) < self.connection.mysql_version < (10, 5, 2): + if ( + self.connection.mysql_is_mariadb -+ and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++ and ( ++ 10, ++ 4, ++ 3, ++ ) ++ < self.connection.mysql_version ++ < (10, 5, 2) + ): pass ``` @@ -91,7 +98,8 @@ class A: ```py if ( - NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + e1234123412341234.winerror + not in (_winapi.ERROR_SEM_TIMEOUT, _winapi.ERROR_PIPE_BUSY) or NOT_IMPLEMENTED_call(NOT_IMPLEMENTED_arg) ): pass @@ -112,7 +120,13 @@ class A: def b(self): if ( self.connection.mysql_is_mariadb - and NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + and ( + 10, + 4, + 3, + ) + < self.connection.mysql_version + < (10, 5, 2) ): pass ``` diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__trailing_comma_optional_parens2_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__trailing_comma_optional_parens2_py.snap index 2c3f46e706..3186661eb0 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__trailing_comma_optional_parens2_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__trailing_comma_optional_parens2_py.snap @@ -23,8 +23,8 @@ if (e123456.get_tk_patchlevel() >= (8, 6, 0, 'final') or - 8, -) <= get_tk_patchlevel() < (8, 6): +if ( -+ NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right -+ or NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right ++ NOT_IMPLEMENTED_call() >= (8, 6, 0, "NOT_YET_IMPLEMENTED_STRING") ++ or (8, 5, 8) <= NOT_IMPLEMENTED_call() < (8, 6) +): pass ``` @@ -33,8 +33,8 @@ if (e123456.get_tk_patchlevel() >= (8, 6, 0, 'final') or ```py if ( - NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right - or NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right + NOT_IMPLEMENTED_call() >= (8, 6, 0, "NOT_YET_IMPLEMENTED_STRING") + or (8, 5, 8) <= NOT_IMPLEMENTED_call() < (8, 6) ): pass ``` diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__binary_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__binary_py.snap index 82106ab6d3..e55f432f29 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__binary_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__binary_py.snap @@ -252,7 +252,13 @@ aaaaaaaaaaaaaa + NOT_YET_IMPLEMENTED_ExprSetComp # But only for expressions that have a statement parent. not (aaaaaaaaaaaaaa + NOT_YET_IMPLEMENTED_ExprSetComp) -[NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right] +[ + a + + [ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + ] + in c, +] # leading comment diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__compare_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__compare_py.snap new file mode 100644 index 0000000000..7abb3e4b1e --- /dev/null +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__expression__compare_py.snap @@ -0,0 +1,189 @@ +--- +source: crates/ruff_python_formatter/src/lib.rs +expression: snapshot +--- +## Input +```py +a == b +a != b +a < b +a <= b +a > b +a >= b +a is b +a is not b +a in b +a not in b + +(a == + # comment + b +) + +(a == # comment + b + ) + +a < b > c == d + +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb > ccccccccccccccccccccccccccccc == ddddddddddddddddddddd + +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa < [ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + ff, +] < [ccccccccccccccccccccccccccccc, dddd] < ddddddddddddddddddddddddddddddddddddddddddd + +return 1 == 2 and ( + name, + description, + self_default, + self_selected, + self_auto_generated, + self_parameters, + self_meta_data, + self_schedule, +) == ( + name, + description, + othr_default, + othr_selected, + othr_auto_generated, + othr_parameters, + othr_meta_data, + othr_schedule, +) + +(name, description, self_default, self_selected, self_auto_generated, self_parameters, self_meta_data, self_schedule) == (name, description, other_default, othr_selected, othr_auto_generated, othr_parameters, othr_meta_data, othr_schedule) +((name, description, self_default, self_selected, self_auto_generated, self_parameters, self_meta_data, self_schedule) == (name, description, other_default, othr_selected, othr_auto_generated, othr_parameters, othr_meta_data, othr_schedule)) + +[ + ( + a + + [ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + ] + >= c + ) +] +``` + + + +## Output +```py +a == b +a != b +a < b +a <= b +a > b +a >= b +a is b +a is not b +a in b +a not in b + +( + a + # comment + == b +) + +( + a # comment + == b +) + +a < b > c == d + +( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + > ccccccccccccccccccccccccccccc + == ddddddddddddddddddddd +) + +( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + < [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ff] + < [ccccccccccccccccccccccccccccc, dddd] + < ddddddddddddddddddddddddddddddddddddddddddd +) + +return ( + 1 == 2 + and ( + name, + description, + self_default, + self_selected, + self_auto_generated, + self_parameters, + self_meta_data, + self_schedule, + ) + == ( + name, + description, + othr_default, + othr_selected, + othr_auto_generated, + othr_parameters, + othr_meta_data, + othr_schedule, + ) +) + +( + name, + description, + self_default, + self_selected, + self_auto_generated, + self_parameters, + self_meta_data, + self_schedule, +) == ( + name, + description, + other_default, + othr_selected, + othr_auto_generated, + othr_parameters, + othr_meta_data, + othr_schedule, +) +( + ( + name, + description, + self_default, + self_selected, + self_auto_generated, + self_parameters, + self_meta_data, + self_schedule, + ) + == ( + name, + description, + other_default, + othr_selected, + othr_auto_generated, + othr_parameters, + othr_meta_data, + othr_schedule, + ) +) + +[ + ( + a + + [ + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, + ] + >= c + ), +] +``` + + diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__if_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__if_py.snap index ef06f0e165..8bd96653d9 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__if_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__statement__if_py.snap @@ -74,12 +74,12 @@ x = 3 ## Output ```py -if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: # trailing if condition +if x == y: # trailing if condition pass # trailing `pass` comment # Root `if` trailing comment # Leading elif comment -elif NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: # trailing elif condition +elif x < y: # trailing elif condition pass # `elif` trailing comment @@ -89,11 +89,11 @@ else: # trailing else condition # `else` trailing comment -if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +if x == y: + if y == z: ... - if NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: + if a == b: ... else: # trailing comment ... diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__trivia_py.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__trivia_py.snap index df8702bcb0..1d1c0586db 100644 --- a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__trivia_py.snap +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__ruff_test__trivia_py.snap @@ -62,7 +62,7 @@ class Test: c = 30 -while NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +while a == 10: ... # trailing comment with one line before @@ -71,7 +71,7 @@ while NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: d = 40 -while NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right: +while b == 20: ... # no empty line before