From ee6548d7dd1b04421d802ddf78ef1ae47cbd95ff Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 6 Dec 2023 16:15:06 +0900 Subject: [PATCH] Enforce valid format options in spec tests (#9021) --- .../test/fixtures/black/cases/ignore_pyi.pyi | 7 ++-- .../cases/line_ranges_fmt_off_decorator.py | 9 ++++ .../line_ranges_fmt_off_decorator.py.expect | 11 ++++- .../black/cases/linelength6.options.json | 2 +- .../black/cases/power_op_newline.options.json | 2 +- .../test/fixtures/import_black_tests.py | 3 +- crates/ruff_python_formatter/src/options.rs | 2 +- ...k_compatibility@cases__ignore_pyi.pyi.snap | 25 +++-------- ...ses__line_ranges_fmt_off_decorator.py.snap | 42 +++++++++++++++++-- ...patibility@cases__power_op_newline.py.snap | 19 +++++---- 10 files changed, 83 insertions(+), 39 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/ignore_pyi.pyi b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/ignore_pyi.pyi index 64230590670a4..5e643ea38e4f7 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/ignore_pyi.pyi +++ b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/ignore_pyi.pyi @@ -15,7 +15,6 @@ def g(): # hi ... -# FIXME(#8905): Uncomment, leads to unstable formatting -# def h(): -# ... -# # bye +def h(): + ... + # bye diff --git a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/line_ranges_fmt_off_decorator.py b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/line_ranges_fmt_off_decorator.py index 3c9e616d83a98..8ae63e21716b8 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/line_ranges_fmt_off_decorator.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/line_ranges_fmt_off_decorator.py @@ -9,3 +9,12 @@ class MyClass: # fmt: on def method(): print ( "str" ) + + @decor( + a=1, + # fmt: off + b=(2, 3), + # fmt: on + ) + def func(): + pass diff --git a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/line_ranges_fmt_off_decorator.py.expect b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/line_ranges_fmt_off_decorator.py.expect index 326b48df6b4f4..905336810bff8 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/line_ranges_fmt_off_decorator.py.expect +++ b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/line_ranges_fmt_off_decorator.py.expect @@ -1,4 +1,4 @@ -# flags: --line-ranges=12-12 +# flags: --line-ranges=12-12 --line-ranges=21-21 # NOTE: If you need to modify this file, pay special attention to the --line-ranges= # flag above as it's formatting specifically these lines. @@ -10,3 +10,12 @@ class MyClass: # fmt: on def method(): print("str") + + @decor( + a=1, + # fmt: off + b=(2, 3), + # fmt: on + ) + def func(): + pass diff --git a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/linelength6.options.json b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/linelength6.options.json index f6d0b5fa4c54c..d5eda5fc94062 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/linelength6.options.json +++ b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/linelength6.options.json @@ -1 +1 @@ -{"line_length": 6} \ No newline at end of file +{"line_width": 6} \ No newline at end of file diff --git a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/power_op_newline.options.json b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/power_op_newline.options.json index 80ad04dcfc445..70fa4ec1b938a 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/black/cases/power_op_newline.options.json +++ b/crates/ruff_python_formatter/resources/test/fixtures/black/cases/power_op_newline.options.json @@ -1 +1 @@ -{"line_length": 0} \ No newline at end of file +{"line_width": 1} \ No newline at end of file diff --git a/crates/ruff_python_formatter/resources/test/fixtures/import_black_tests.py b/crates/ruff_python_formatter/resources/test/fixtures/import_black_tests.py index 31d1515aefe3e..b196876e7063e 100755 --- a/crates/ruff_python_formatter/resources/test/fixtures/import_black_tests.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/import_black_tests.py @@ -48,7 +48,8 @@ def import_fixture(fixture: Path, fixture_set: str): if "--line-length=" in flags: [_, length_and_rest] = flags.split("--line-length=", 1) length = length_and_rest.split(" ", 1)[0] - options["line_length"] = int(length) + length = int(length) + options["line_width"] = 1 if length == 0 else length if "--skip-magic-trailing-comma" in flags: options["magic_trailing_comma"] = "ignore" diff --git a/crates/ruff_python_formatter/src/options.rs b/crates/ruff_python_formatter/src/options.rs index 8306afd82c431..a07fabbb9a795 100644 --- a/crates/ruff_python_formatter/src/options.rs +++ b/crates/ruff_python_formatter/src/options.rs @@ -11,7 +11,7 @@ use std::str::FromStr; #[cfg_attr( feature = "serde", derive(serde::Serialize, serde::Deserialize), - serde(default) + serde(default, deny_unknown_fields) )] pub struct PyFormatOptions { /// Whether we're in a `.py` file or `.pyi` file, which have different rules. diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__ignore_pyi.pyi.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__ignore_pyi.pyi.snap index ed9ee11309184..0d405b5b573a2 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__ignore_pyi.pyi.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__ignore_pyi.pyi.snap @@ -22,10 +22,9 @@ def g(): # hi ... -# FIXME(#8905): Uncomment, leads to unstable formatting -# def h(): -# ... -# # bye +def h(): + ... + # bye ``` ## Black Differences @@ -41,17 +40,6 @@ def g(): class y: ... # comment # whitespace doesn't matter (note the next line has a trailing space and tab) -@@ -13,6 +12,7 @@ - # hi - ... - --def h(): -- ... -- # bye -+# FIXME(#8905): Uncomment, leads to unstable formatting -+# def h(): -+# ... -+# # bye ``` ## Ruff Output @@ -71,10 +59,9 @@ def g(): # hi ... -# FIXME(#8905): Uncomment, leads to unstable formatting -# def h(): -# ... -# # bye +def h(): + ... + # bye ``` ## Black Output diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__line_ranges_fmt_off_decorator.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__line_ranges_fmt_off_decorator.py.snap index 4ac42448f3593..d14b92cc08c31 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__line_ranges_fmt_off_decorator.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__line_ranges_fmt_off_decorator.py.snap @@ -16,6 +16,15 @@ class MyClass: # fmt: on def method(): print ( "str" ) + + @decor( + a=1, + # fmt: off + b=(2, 3), + # fmt: on + ) + def func(): + pass ``` ## Black Differences @@ -23,8 +32,8 @@ class MyClass: ```diff --- Black +++ Ruff -@@ -1,12 +1,10 @@ --# flags: --line-ranges=12-12 +@@ -1,15 +1,13 @@ +-# flags: --line-ranges=12-12 --line-ranges=21-21 # NOTE: If you need to modify this file, pay special attention to the --line-ranges= # flag above as it's formatting specifically these lines. @@ -37,6 +46,15 @@ class MyClass: def method(): - print("str") + print ( "str" ) + + @decor( + a=1, +@@ -18,4 +16,4 @@ + # fmt: on + ) + def func(): +- pass ++ pass ``` ## Ruff Output @@ -52,12 +70,21 @@ class MyClass: # fmt: on def method(): print ( "str" ) + + @decor( + a=1, + # fmt: off + b=(2, 3), + # fmt: on + ) + def func(): + pass ``` ## Black Output ```python -# flags: --line-ranges=12-12 +# flags: --line-ranges=12-12 --line-ranges=21-21 # NOTE: If you need to modify this file, pay special attention to the --line-ranges= # flag above as it's formatting specifically these lines. @@ -69,6 +96,15 @@ class MyClass: # fmt: on def method(): print("str") + + @decor( + a=1, + # fmt: off + b=(2, 3), + # fmt: on + ) + def func(): + pass ``` diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__power_op_newline.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__power_op_newline.py.snap index 1796017091aa5..bb1b6eed95ecb 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__power_op_newline.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__power_op_newline.py.snap @@ -13,21 +13,24 @@ importA;()<<0**0# ```diff --- Black +++ Ruff -@@ -1,6 +1,2 @@ - importA --( -- () -- << 0 +@@ -2,5 +2,5 @@ + ( + () + << 0 - ** 0 --) # -+() << 0**0 # ++ **0 + ) # ``` ## Ruff Output ```python importA -() << 0**0 # +( + () + << 0 + **0 +) # ``` ## Black Output