From 438001af4efab1b97b266d8e7b7704e853cc21b9 Mon Sep 17 00:00:00 2001 From: konstin Date: Fri, 12 Jul 2024 11:49:27 +0200 Subject: [PATCH] Gate behind preview --- .../src/statement/suite.rs | 8 +- ...ack_compatibility@cases__comments9.py.snap | 18 +- ...ack_compatibility@cases__function2.py.snap | 215 ++++++++++++++++++ .../tests/snapshots/format@newlines.py.snap | 70 +++++- .../format@statement__function.py.snap | 25 +- .../snapshots/format@statement__if.py.snap | 23 +- .../snapshots/format@statement__try.py.snap | 30 ++- 7 files changed, 371 insertions(+), 18 deletions(-) create mode 100644 crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__function2.py.snap diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index c6778486b80545..1ac1a2a695f23d 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -516,7 +516,13 @@ impl FormatSuite { let existing_newlines = lines_after_ignoring_end_of_line_trivia(end_of_def_or_class, f.context().source()); if existing_newlines < 2 { - empty_line().fmt(f)?; + if f.context().is_preview() { + empty_line().fmt(f)?; + } else { + if last_def_or_class.is_stmt_class_def() && f.options().source_type().is_stub() { + empty_line().fmt(f)?; + } + } } Ok(()) } diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__comments9.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__comments9.py.snap index b3dcbc78a56624..85d3044bacef43 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__comments9.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__comments9.py.snap @@ -159,6 +159,22 @@ def bar(): # leading 3 that already has an empty line @deco3 # leading 4 +@@ -106,7 +105,6 @@ + # Another leading comment + def another_inline(): + pass +- + else: + # More leading comments + def inline_after_else(): +@@ -121,7 +119,6 @@ + # Another leading comment + def another_top_level_quote_inline_inline(): + pass +- + else: + # More leading comments + def top_level_quote_inline_after_else(): ``` ## Ruff Output @@ -271,7 +287,6 @@ def main(): # Another leading comment def another_inline(): pass - else: # More leading comments def inline_after_else(): @@ -286,7 +301,6 @@ if a: # Another leading comment def another_top_level_quote_inline_inline(): pass - else: # More leading comments def top_level_quote_inline_after_else(): diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__function2.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__function2.py.snap new file mode 100644 index 00000000000000..49a9d5f781bec6 --- /dev/null +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__function2.py.snap @@ -0,0 +1,215 @@ +--- +source: crates/ruff_python_formatter/tests/fixtures.rs +input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/function2.py +--- +## Input + +```python +def f( + a, + **kwargs, +) -> A: + with cache_dir(): + if something: + result = ( + CliRunner().invoke(black.main, [str(src1), str(src2), "--diff", "--check"]) + ) + limited.append(-limited.pop()) # negate top + return A( + very_long_argument_name1=very_long_value_for_the_argument, + very_long_argument_name2=-very.long.value.for_the_argument, + **kwargs, + ) +def g(): + "Docstring." + def inner(): + pass + print("Inner defs should breathe a little.") +def h(): + def inner(): + pass + print("Inner defs should breathe a little.") + + +if os.name == "posix": + import termios + def i_should_be_followed_by_only_one_newline(): + pass +elif os.name == "nt": + try: + import msvcrt + def i_should_be_followed_by_only_one_newline(): + pass + + except ImportError: + + def i_should_be_followed_by_only_one_newline(): + pass + +elif False: + + class IHopeYouAreHavingALovelyDay: + def __call__(self): + print("i_should_be_followed_by_only_one_newline") +else: + + def foo(): + pass + +with hmm_but_this_should_get_two_preceding_newlines(): + pass +``` + +## Black Differences + +```diff +--- Black ++++ Ruff +@@ -36,7 +36,6 @@ + + def i_should_be_followed_by_only_one_newline(): + pass +- + elif os.name == "nt": + try: + import msvcrt +``` + +## Ruff Output + +```python +def f( + a, + **kwargs, +) -> A: + with cache_dir(): + if something: + result = CliRunner().invoke( + black.main, [str(src1), str(src2), "--diff", "--check"] + ) + limited.append(-limited.pop()) # negate top + return A( + very_long_argument_name1=very_long_value_for_the_argument, + very_long_argument_name2=-very.long.value.for_the_argument, + **kwargs, + ) + + +def g(): + "Docstring." + + def inner(): + pass + + print("Inner defs should breathe a little.") + + +def h(): + def inner(): + pass + + print("Inner defs should breathe a little.") + + +if os.name == "posix": + import termios + + def i_should_be_followed_by_only_one_newline(): + pass +elif os.name == "nt": + try: + import msvcrt + + def i_should_be_followed_by_only_one_newline(): + pass + + except ImportError: + + def i_should_be_followed_by_only_one_newline(): + pass + +elif False: + + class IHopeYouAreHavingALovelyDay: + def __call__(self): + print("i_should_be_followed_by_only_one_newline") + +else: + + def foo(): + pass + + +with hmm_but_this_should_get_two_preceding_newlines(): + pass +``` + +## Black Output + +```python +def f( + a, + **kwargs, +) -> A: + with cache_dir(): + if something: + result = CliRunner().invoke( + black.main, [str(src1), str(src2), "--diff", "--check"] + ) + limited.append(-limited.pop()) # negate top + return A( + very_long_argument_name1=very_long_value_for_the_argument, + very_long_argument_name2=-very.long.value.for_the_argument, + **kwargs, + ) + + +def g(): + "Docstring." + + def inner(): + pass + + print("Inner defs should breathe a little.") + + +def h(): + def inner(): + pass + + print("Inner defs should breathe a little.") + + +if os.name == "posix": + import termios + + def i_should_be_followed_by_only_one_newline(): + pass + +elif os.name == "nt": + try: + import msvcrt + + def i_should_be_followed_by_only_one_newline(): + pass + + except ImportError: + + def i_should_be_followed_by_only_one_newline(): + pass + +elif False: + + class IHopeYouAreHavingALovelyDay: + def __call__(self): + print("i_should_be_followed_by_only_one_newline") + +else: + + def foo(): + pass + + +with hmm_but_this_should_get_two_preceding_newlines(): + pass +``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@newlines.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@newlines.py.snap index 9c60f179f02e19..33976cbc39f58f 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@newlines.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@newlines.py.snap @@ -616,7 +616,6 @@ if True: def a(): return 1 - else: pass @@ -625,7 +624,6 @@ match True: def a(): return 1 - case 1: def a(): @@ -636,7 +634,6 @@ try: def a(): return 1 - except RuntimeError: def a(): @@ -647,7 +644,6 @@ try: def a(): return 1 - finally: def a(): @@ -658,22 +654,18 @@ try: def a(): return 1 - except RuntimeError: def a(): return 1 - except ZeroDivisionError: def a(): return 1 - else: def a(): return 1 - finally: def a(): @@ -724,3 +716,65 @@ def overload4(): def overload4(a: int): ... ``` + + +## Preview changes +```diff +--- Stable ++++ Preview +@@ -277,6 +277,7 @@ + + def a(): + return 1 ++ + else: + pass + +@@ -285,6 +286,7 @@ + + def a(): + return 1 ++ + case 1: + + def a(): +@@ -295,6 +297,7 @@ + + def a(): + return 1 ++ + except RuntimeError: + + def a(): +@@ -305,6 +308,7 @@ + + def a(): + return 1 ++ + finally: + + def a(): +@@ -315,18 +319,22 @@ + + def a(): + return 1 ++ + except RuntimeError: + + def a(): + return 1 ++ + except ZeroDivisionError: + + def a(): + return 1 ++ + else: + + def a(): + return 1 ++ + finally: + + def a(): +``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__function.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__function.py.snap index 1c4c84e2a0b5e8..9d108fc7572913 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__function.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__function.py.snap @@ -609,7 +609,6 @@ if True: def f1(): pass # a - else: pass @@ -619,7 +618,6 @@ if True: def f2(): pass # a - else: pass @@ -1044,3 +1042,26 @@ def func[T]( lotsoflongargs5: T, ) -> T: ... ``` + + +## Preview changes +```diff +--- Stable ++++ Preview +@@ -161,6 +161,7 @@ + + def f1(): + pass # a ++ + else: + pass + +@@ -170,6 +171,7 @@ + def f2(): + pass + # a ++ + else: + pass + +``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__if.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__if.py.snap index 7e4a91bf38c35c..9bc4576203c3f6 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__if.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__if.py.snap @@ -399,13 +399,11 @@ if True: def f(): pass # 1 - elif True: def f(): pass # 2 - else: def f(): @@ -609,3 +607,24 @@ if parent_body: # e # f ``` + + +## Preview changes +```diff +--- Stable ++++ Preview +@@ -93,11 +93,13 @@ + def f(): + pass + # 1 ++ + elif True: + + def f(): + pass + # 2 ++ + else: + + def f(): +``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__try.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__try.py.snap index 6a55e8c783b2d2..fad6dd510e4caf 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__try.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__try.py.snap @@ -295,19 +295,16 @@ try: def f(): pass # a - except: def f(): pass # b - else: def f(): pass # c - finally: def f(): @@ -367,3 +364,30 @@ else: finally: pass ``` + + +## Preview changes +```diff +--- Stable ++++ Preview +@@ -117,16 +117,19 @@ + def f(): + pass + # a ++ + except: + + def f(): + pass + # b ++ + else: + + def f(): + pass + # c ++ + finally: + + def f(): +```