Enforce valid format options in spec tests (#9021)

This commit is contained in:
Micha Reiser 2023-12-06 16:15:06 +09:00 committed by GitHub
parent b4a050c21d
commit ee6548d7dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 83 additions and 39 deletions

View file

@ -15,7 +15,6 @@ def g():
# hi # hi
... ...
# FIXME(#8905): Uncomment, leads to unstable formatting def h():
# def h(): ...
# ... # bye
# # bye

View file

@ -9,3 +9,12 @@ class MyClass:
# fmt: on # fmt: on
def method(): def method():
print ( "str" ) print ( "str" )
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass

View file

@ -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= # NOTE: If you need to modify this file, pay special attention to the --line-ranges=
# flag above as it's formatting specifically these lines. # flag above as it's formatting specifically these lines.
@ -10,3 +10,12 @@ class MyClass:
# fmt: on # fmt: on
def method(): def method():
print("str") print("str")
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass

View file

@ -1 +1 @@
{"line_length": 6} {"line_width": 6}

View file

@ -1 +1 @@
{"line_length": 0} {"line_width": 1}

View file

@ -48,7 +48,8 @@ def import_fixture(fixture: Path, fixture_set: str):
if "--line-length=" in flags: if "--line-length=" in flags:
[_, length_and_rest] = flags.split("--line-length=", 1) [_, length_and_rest] = flags.split("--line-length=", 1)
length = length_and_rest.split(" ", 1)[0] 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: if "--skip-magic-trailing-comma" in flags:
options["magic_trailing_comma"] = "ignore" options["magic_trailing_comma"] = "ignore"

View file

@ -11,7 +11,7 @@ use std::str::FromStr;
#[cfg_attr( #[cfg_attr(
feature = "serde", feature = "serde",
derive(serde::Serialize, serde::Deserialize), derive(serde::Serialize, serde::Deserialize),
serde(default) serde(default, deny_unknown_fields)
)] )]
pub struct PyFormatOptions { pub struct PyFormatOptions {
/// Whether we're in a `.py` file or `.pyi` file, which have different rules. /// Whether we're in a `.py` file or `.pyi` file, which have different rules.

View file

@ -22,10 +22,9 @@ def g():
# hi # hi
... ...
# FIXME(#8905): Uncomment, leads to unstable formatting def h():
# def h(): ...
# ... # bye
# # bye
``` ```
## Black Differences ## Black Differences
@ -41,17 +40,6 @@ def g():
class y: ... # comment class y: ... # comment
# whitespace doesn't matter (note the next line has a trailing space and tab) # 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 ## Ruff Output
@ -71,10 +59,9 @@ def g():
# hi # hi
... ...
# FIXME(#8905): Uncomment, leads to unstable formatting def h():
# def h(): ...
# ... # bye
# # bye
``` ```
## Black Output ## Black Output

View file

@ -16,6 +16,15 @@ class MyClass:
# fmt: on # fmt: on
def method(): def method():
print ( "str" ) print ( "str" )
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
``` ```
## Black Differences ## Black Differences
@ -23,8 +32,8 @@ class MyClass:
```diff ```diff
--- Black --- Black
+++ Ruff +++ Ruff
@@ -1,12 +1,10 @@ @@ -1,15 +1,13 @@
-# 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= # NOTE: If you need to modify this file, pay special attention to the --line-ranges=
# flag above as it's formatting specifically these lines. # flag above as it's formatting specifically these lines.
@ -37,6 +46,15 @@ class MyClass:
def method(): def method():
- print("str") - print("str")
+ print ( "str" ) + print ( "str" )
@decor(
a=1,
@@ -18,4 +16,4 @@
# fmt: on
)
def func():
- pass
+ pass
``` ```
## Ruff Output ## Ruff Output
@ -52,12 +70,21 @@ class MyClass:
# fmt: on # fmt: on
def method(): def method():
print ( "str" ) print ( "str" )
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
``` ```
## Black Output ## Black Output
```python ```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= # NOTE: If you need to modify this file, pay special attention to the --line-ranges=
# flag above as it's formatting specifically these lines. # flag above as it's formatting specifically these lines.
@ -69,6 +96,15 @@ class MyClass:
# fmt: on # fmt: on
def method(): def method():
print("str") print("str")
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
``` ```

View file

@ -13,21 +13,24 @@ importA;()<<0**0#
```diff ```diff
--- Black --- Black
+++ Ruff +++ Ruff
@@ -1,6 +1,2 @@ @@ -2,5 +2,5 @@
importA (
-( ()
- () << 0
- << 0
- ** 0 - ** 0
-) # + **0
+() << 0**0 # ) #
``` ```
## Ruff Output ## Ruff Output
```python ```python
importA importA
() << 0**0 # (
()
<< 0
**0
) #
``` ```
## Black Output ## Black Output