From 0698de0a67f0e3e2874368eb18c0038dbd536d3c Mon Sep 17 00:00:00 2001 From: LaBatata101 Date: Thu, 24 Aug 2023 17:26:57 -0300 Subject: [PATCH] Format PatternMatchClass Closes #5834 --- .../test/fixtures/ruff/statement/match.py | 27 ++ .../src/pattern/pattern_match_class.rs | 40 +- ...y@py_310__pattern_matching_complex.py.snap | 11 +- ...ty@py_310__pattern_matching_extras.py.snap | 89 ++--- ...y@py_310__pattern_matching_generic.py.snap | 357 ------------------ ...ty@py_310__pattern_matching_simple.py.snap | 59 +-- ...ity@py_310__pattern_matching_style.py.snap | 43 ++- .../snapshots/format@fmt_skip__match.py.snap | 15 +- 8 files changed, 124 insertions(+), 517 deletions(-) delete mode 100644 crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_generic.py.snap diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py index 7f4b6a956a510..818e99111ce8c 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py @@ -263,3 +263,30 @@ def foo(): # comment ): y = 1 + + +match pattern_match_class: + case Foo( + # own line + ): + ... + + case Point2D(0, 0): + ... + + case Point3D(x=0, y=0, z=000000000000000000000000000000000000000000000000000000000000000000000000000000000): + ... + + case Bar(0, a=None, b="hello"): + ... + + case FooBar(# leading +# leading + # leading + # leading + 0 # trailing +# trailing + # trailing + # trailing + ): + ... diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_class.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_class.rs index 6d8601b95e445..2b60c7a570c29 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_class.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_class.rs @@ -1,19 +1,41 @@ -use ruff_formatter::{write, Buffer, FormatResult}; +use ruff_formatter::{write, FormatResult}; use ruff_python_ast::PatternMatchClass; -use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter}; +use crate::expression::parentheses::parenthesized; +use crate::prelude::*; +use crate::{FormatNodeRule, PyFormatter}; #[derive(Default)] pub struct FormatPatternMatchClass; impl FormatNodeRule for FormatPatternMatchClass { fn fmt_fields(&self, item: &PatternMatchClass, f: &mut PyFormatter) -> FormatResult<()> { - write!( - f, - [not_yet_implemented_custom_text( - "NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0)", - item - )] - ) + let PatternMatchClass { + range, + cls, + patterns, + kwd_attrs, + kwd_patterns, + } = item; + + let items = format_with(|f| { + let mut join = f.join_comma_separated(range.end()); + + if !patterns.is_empty() { + join.nodes(patterns.iter()); + } + + if !kwd_attrs.is_empty() { + for (key, value) in kwd_attrs.iter().zip(kwd_patterns.iter()) { + join.entry( + key, + &format_with(|f| write!(f, [key.format(), text("="), value.format()])), + ); + } + } + join.finish() + }); + + write!(f, [cls.format(), parenthesized("(", &items, ")")]) } } diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_complex.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_complex.py.snap index 69fa19f8de895..0bcc982954302 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_complex.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_complex.py.snap @@ -156,15 +156,6 @@ match x: ```diff --- Black +++ Ruff -@@ -6,7 +6,7 @@ - y = 0 - # case black_test_patma_142 - match x: -- case bytes(z): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - y = 0 - # case black_test_patma_073 - match x: @@ -16,23 +16,23 @@ y = 1 # case black_test_patma_006 @@ -282,7 +273,7 @@ match x: y = 0 # case black_test_patma_142 match x: - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case bytes(z): y = 0 # case black_test_patma_073 match x: diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap index bbb9c5c8246ca..b5a025861ea01 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap @@ -131,36 +131,6 @@ match bar1: ```diff --- Black +++ Ruff -@@ -5,9 +5,9 @@ - print(b) - case [a as b, c, d, e as f]: - print(f) -- case Point(a as b): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - print(b) -- case Point(int() as x, int() as y): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - print(x, y) - - -@@ -15,7 +15,7 @@ - case: int = re.match(something) - - match re.match(case): -- case type("match", match): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - pass - case match: - pass -@@ -23,7 +23,7 @@ - - def func(match: case, case: match) -> case: - match Something(): -- case func(match, case): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - ... - case another: - ... @@ -32,23 +32,32 @@ match maybe, multiple: case perhaps, 5: @@ -199,21 +169,7 @@ match bar1: assert "map" == b -@@ -59,12 +68,7 @@ - ), - case, - ): -- case case( -- match=case, -- case=re.match( -- loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong -- ), -- ): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - pass - - case [a as match]: -@@ -80,17 +84,14 @@ +@@ -80,17 +89,14 @@ match a, *b(), c: @@ -234,7 +190,7 @@ match bar1: pass -@@ -101,19 +102,22 @@ +@@ -101,7 +107,12 @@ case 2 as b, 3 as c: pass @@ -248,19 +204,16 @@ match bar1: pass - match bar1: -- case Foo(aa=Callable() as aa, bb=int()): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - print(bar1.aa, bar1.bb) - case _: - print("no match", "\n") - +@@ -114,6 +125,9 @@ match bar1: -- case Foo( + case Foo( - normal=x, perhaps=[list, {"x": d, "y": 1.0}] as y, otherwise=something, q=t as u -- ): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): ++ normal=x, ++ perhaps=[list, {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}] as y, ++ otherwise=something, ++ q=t as u, + ): pass ``` @@ -274,9 +227,9 @@ match something: print(b) case [a as b, c, d, e as f]: print(f) - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(a as b): print(b) - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(int() as x, int() as y): print(x, y) @@ -284,7 +237,7 @@ match = 1 case: int = re.match(something) match re.match(case): - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case type("match", match): pass case match: pass @@ -292,7 +245,7 @@ match re.match(case): def func(match: case, case: match) -> case: match Something(): - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case func(match, case): ... case another: ... @@ -337,7 +290,12 @@ match match( ), case, ): - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case case( + match=case, + case=re.match( + loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong + ), + ): pass case [a as match]: @@ -381,14 +339,19 @@ match something: match bar1: - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Foo(aa=Callable() as aa, bb=int()): print(bar1.aa, bar1.bb) case _: print("no match", "\n") match bar1: - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Foo( + normal=x, + perhaps=[list, {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}] as y, + otherwise=something, + q=t as u, + ): pass ``` diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_generic.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_generic.py.snap deleted file mode 100644 index 74ef3ffc47899..0000000000000 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_generic.py.snap +++ /dev/null @@ -1,357 +0,0 @@ ---- -source: crates/ruff_python_formatter/tests/fixtures.rs -input_file: crates/ruff_python_formatter/resources/test/fixtures/black/py_310/pattern_matching_generic.py ---- -## Input - -```py -re.match() -match = a -with match() as match: - match = f"{match}" - -re.match() -match = a -with match() as match: - match = f"{match}" - - -def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]: - if not target_versions: - # No target_version specified, so try all grammars. - return [ - # Python 3.7+ - pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords, - # Python 3.0-3.6 - pygram.python_grammar_no_print_statement_no_exec_statement, - # Python 2.7 with future print_function import - pygram.python_grammar_no_print_statement, - # Python 2.7 - pygram.python_grammar, - ] - - match match: - case case: - match match: - case case: - pass - - if all(version.is_python2() for version in target_versions): - # Python 2-only code, so try Python 2 grammars. - return [ - # Python 2.7 with future print_function import - pygram.python_grammar_no_print_statement, - # Python 2.7 - pygram.python_grammar, - ] - - re.match() - match = a - with match() as match: - match = f"{match}" - - def test_patma_139(self): - x = False - match x: - case bool(z): - y = 0 - self.assertIs(x, False) - self.assertEqual(y, 0) - self.assertIs(z, x) - - # Python 3-compatible code, so only try Python 3 grammar. - grammars = [] - if supports_feature(target_versions, Feature.PATTERN_MATCHING): - # Python 3.10+ - grammars.append(pygram.python_grammar_soft_keywords) - # If we have to parse both, try to parse async as a keyword first - if not supports_feature( - target_versions, Feature.ASYNC_IDENTIFIERS - ) and not supports_feature(target_versions, Feature.PATTERN_MATCHING): - # Python 3.7-3.9 - grammars.append( - pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords - ) - if not supports_feature(target_versions, Feature.ASYNC_KEYWORDS): - # Python 3.0-3.6 - grammars.append(pygram.python_grammar_no_print_statement_no_exec_statement) - - def test_patma_155(self): - x = 0 - y = None - match x: - case 1e1000: - y = 0 - self.assertEqual(x, 0) - self.assertIs(y, None) - - x = range(3) - match x: - case [y, case as x, z]: - w = 0 - - # At least one of the above branches must have been taken, because every Python - # version has exactly one of the two 'ASYNC_*' flags - return grammars - - -def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node: - """Given a string with source, return the lib2to3 Node.""" - if not src_txt.endswith("\n"): - src_txt += "\n" - - grammars = get_grammars(set(target_versions)) - - -re.match() -match = a -with match() as match: - match = f"{match}" - -re.match() -match = a -with match() as match: - match = f"{match}" -``` - -## Black Differences - -```diff ---- Black -+++ Ruff -@@ -46,7 +46,7 @@ - def test_patma_139(self): - x = False - match x: -- case bool(z): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - y = 0 - self.assertIs(x, False) - self.assertEqual(y, 0) -``` - -## Ruff Output - -```py -re.match() -match = a -with match() as match: - match = f"{match}" - -re.match() -match = a -with match() as match: - match = f"{match}" - - -def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]: - if not target_versions: - # No target_version specified, so try all grammars. - return [ - # Python 3.7+ - pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords, - # Python 3.0-3.6 - pygram.python_grammar_no_print_statement_no_exec_statement, - # Python 2.7 with future print_function import - pygram.python_grammar_no_print_statement, - # Python 2.7 - pygram.python_grammar, - ] - - match match: - case case: - match match: - case case: - pass - - if all(version.is_python2() for version in target_versions): - # Python 2-only code, so try Python 2 grammars. - return [ - # Python 2.7 with future print_function import - pygram.python_grammar_no_print_statement, - # Python 2.7 - pygram.python_grammar, - ] - - re.match() - match = a - with match() as match: - match = f"{match}" - - def test_patma_139(self): - x = False - match x: - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - y = 0 - self.assertIs(x, False) - self.assertEqual(y, 0) - self.assertIs(z, x) - - # Python 3-compatible code, so only try Python 3 grammar. - grammars = [] - if supports_feature(target_versions, Feature.PATTERN_MATCHING): - # Python 3.10+ - grammars.append(pygram.python_grammar_soft_keywords) - # If we have to parse both, try to parse async as a keyword first - if not supports_feature( - target_versions, Feature.ASYNC_IDENTIFIERS - ) and not supports_feature(target_versions, Feature.PATTERN_MATCHING): - # Python 3.7-3.9 - grammars.append( - pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords - ) - if not supports_feature(target_versions, Feature.ASYNC_KEYWORDS): - # Python 3.0-3.6 - grammars.append(pygram.python_grammar_no_print_statement_no_exec_statement) - - def test_patma_155(self): - x = 0 - y = None - match x: - case 1e1000: - y = 0 - self.assertEqual(x, 0) - self.assertIs(y, None) - - x = range(3) - match x: - case [y, case as x, z]: - w = 0 - - # At least one of the above branches must have been taken, because every Python - # version has exactly one of the two 'ASYNC_*' flags - return grammars - - -def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node: - """Given a string with source, return the lib2to3 Node.""" - if not src_txt.endswith("\n"): - src_txt += "\n" - - grammars = get_grammars(set(target_versions)) - - -re.match() -match = a -with match() as match: - match = f"{match}" - -re.match() -match = a -with match() as match: - match = f"{match}" -``` - -## Black Output - -```py -re.match() -match = a -with match() as match: - match = f"{match}" - -re.match() -match = a -with match() as match: - match = f"{match}" - - -def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]: - if not target_versions: - # No target_version specified, so try all grammars. - return [ - # Python 3.7+ - pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords, - # Python 3.0-3.6 - pygram.python_grammar_no_print_statement_no_exec_statement, - # Python 2.7 with future print_function import - pygram.python_grammar_no_print_statement, - # Python 2.7 - pygram.python_grammar, - ] - - match match: - case case: - match match: - case case: - pass - - if all(version.is_python2() for version in target_versions): - # Python 2-only code, so try Python 2 grammars. - return [ - # Python 2.7 with future print_function import - pygram.python_grammar_no_print_statement, - # Python 2.7 - pygram.python_grammar, - ] - - re.match() - match = a - with match() as match: - match = f"{match}" - - def test_patma_139(self): - x = False - match x: - case bool(z): - y = 0 - self.assertIs(x, False) - self.assertEqual(y, 0) - self.assertIs(z, x) - - # Python 3-compatible code, so only try Python 3 grammar. - grammars = [] - if supports_feature(target_versions, Feature.PATTERN_MATCHING): - # Python 3.10+ - grammars.append(pygram.python_grammar_soft_keywords) - # If we have to parse both, try to parse async as a keyword first - if not supports_feature( - target_versions, Feature.ASYNC_IDENTIFIERS - ) and not supports_feature(target_versions, Feature.PATTERN_MATCHING): - # Python 3.7-3.9 - grammars.append( - pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords - ) - if not supports_feature(target_versions, Feature.ASYNC_KEYWORDS): - # Python 3.0-3.6 - grammars.append(pygram.python_grammar_no_print_statement_no_exec_statement) - - def test_patma_155(self): - x = 0 - y = None - match x: - case 1e1000: - y = 0 - self.assertEqual(x, 0) - self.assertIs(y, None) - - x = range(3) - match x: - case [y, case as x, z]: - w = 0 - - # At least one of the above branches must have been taken, because every Python - # version has exactly one of the two 'ASYNC_*' flags - return grammars - - -def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node: - """Given a string with source, return the lib2to3 Node.""" - if not src_txt.endswith("\n"): - src_txt += "\n" - - grammars = get_grammars(set(target_versions)) - - -re.match() -match = a -with match() as match: - match = f"{match}" - -re.match() -match = a -with match() as match: - match = f"{match}" -``` - - diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_simple.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_simple.py.snap index d370a429178ba..f21534ee04519 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_simple.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_simple.py.snap @@ -143,50 +143,15 @@ def where_is(point): current_room = current_room.neighbor(direction) match command.split(): -@@ -60,33 +60,33 @@ - print("Sorry, you can't go that way") - +@@ -62,7 +62,7 @@ match event.get(): -- case Click(position=(x, y)): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Click(position=(x, y)): handle_click_at(x, y) - case KeyPress(key_name="Q") | Quit(): + case NOT_YET_IMPLEMENTED_PatternMatchOf | (y): game.quit() -- case KeyPress(key_name="up arrow"): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case KeyPress(key_name="up arrow"): game.go_north() -- case KeyPress(): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - pass # Ignore other keystrokes - case other_event: - raise ValueError(f"Unrecognized event: {other_event}") - - match event.get(): -- case Click((x, y), button=Button.LEFT): # This is a left click -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): # This is a left click - handle_click_at(x, y) -- case Click(): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - pass # ignore other clicks - - - def where_is(point): - match point: -- case Point(x=0, y=0): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - print("Origin") -- case Point(x=0, y=y): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - print(f"Y={y}") -- case Point(x=x, y=0): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - print(f"X={x}") -- case Point(): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - print("Somewhere else") - case _: - print("Not a point") ``` ## Ruff Output @@ -254,33 +219,33 @@ match command.split(): print("Sorry, you can't go that way") match event.get(): - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Click(position=(x, y)): handle_click_at(x, y) case NOT_YET_IMPLEMENTED_PatternMatchOf | (y): game.quit() - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case KeyPress(key_name="up arrow"): game.go_north() - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case KeyPress(): pass # Ignore other keystrokes case other_event: raise ValueError(f"Unrecognized event: {other_event}") match event.get(): - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): # This is a left click + case Click((x, y), button=Button.LEFT): # This is a left click handle_click_at(x, y) - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Click(): pass # ignore other clicks def where_is(point): match point: - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(x=0, y=0): print("Origin") - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(x=0, y=y): print(f"Y={y}") - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(x=x, y=0): print(f"X={x}") - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(): print("Somewhere else") case _: print("Not a point") diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_style.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_style.py.snap index 3f8e3b8febe2e..4b9042ff883ae 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_style.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_style.py.snap @@ -65,22 +65,17 @@ match match( ```diff --- Black +++ Ruff -@@ -1,35 +1,34 @@ - match something: -- case b(): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): +@@ -2,7 +2,8 @@ + case b(): print(1 + 1) -- case c( + case c( - very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1 -- ): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): ++ very_complex=True, ++ perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1, + ): print(1) -- case c( -- very_complex=True, -- perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1, -- ): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - print(2) + case c( +@@ -13,20 +14,26 @@ case a: pass @@ -109,22 +104,24 @@ match match( +) re.match() match match(): -- case case( -- arg, # comment -- ): -+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): - pass + case case( ``` ## Ruff Output ```py match something: - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case b(): print(1 + 1) - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case c( + very_complex=True, + perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1, + ): print(1) - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case c( + very_complex=True, + perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1, + ): print(2) case a: pass @@ -151,7 +148,9 @@ re.match( ) re.match() match match(): - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case case( + arg, # comment + ): pass ``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__match.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__match.py.snap index 32daa63f6652d..9c184bdad0708 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__match.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@fmt_skip__match.py.snap @@ -116,11 +116,11 @@ def location(point): match point: case Point(x=0, y =0 ) : # fmt: skip print("Origin is the point's location.") - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(x=0, y=y): print(f"Y={y} and the point is on the y-axis.") - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(x=x, y=0): print(f"X={x} and the point is on the x-axis.") - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(): print("The point is located somewhere else on the plane.") case _: print("Not a point") @@ -133,12 +133,9 @@ match points: Point(0, 0) ]: # fmt: skip print("The origin is the only point in the list.") - case [NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0)]: + case [Point(x, y)]: print(f"A single point {x}, {y} is in the list.") - case [ - NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0), - NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0), - ]: + case [Point(0, y1), Point(0, y2)]: print(f"Two points on the Y axis at {y1}, {y2} are in the list.") case _: print("Something else is found in the list.") @@ -158,7 +155,7 @@ match test_variable: match point: case Point(x, y) if x == y: # fmt: skip print(f"The point is located on the diagonal Y=X at {x}.") - case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0): + case Point(x, y): print(f"Point is not on the diagonal.") ```