Skip to content

Commit

Permalink
Enforce valid py formatter option names
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Dec 6, 2023
1 parent b4a050c commit fadaddd
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def g():
# hi
...

# FIXME(#8905): Uncomment, leads to unstable formatting
# def h():
# ...
# # bye
def h():
...
# bye
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"line_length": 6}
{"line_width": 6}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"line_length": 0}
{"line_width": 1}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ def g():
# hi
...
# FIXME(#8905): Uncomment, leads to unstable formatting
# def h():
# ...
# # bye
def h():
...
# bye
```

## Black Differences
Expand All @@ -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
Expand All @@ -71,10 +59,9 @@ def g():
# hi
...
# FIXME(#8905): Uncomment, leads to unstable formatting
# def h():
# ...
# # bye
def h():
...
# bye
```

## Black Output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ class MyClass:
# fmt: on
def method():
print ( "str" )
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
```

## Black Differences

```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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
```


Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fadaddd

Please sign in to comment.