Skip to content

Commit

Permalink
Fix formatting lambda with empty arguments (#5944)
Browse files Browse the repository at this point in the history
**Summary** Fix implemented in
astral-sh/RustPython-Parser#35: Previously,
empty lambda arguments (e.g. `lambda: 1`) would get the range of the
entire expression, which leads to incorrect comment placement. Now empty
lambda arguments get an empty range between the `lambda` and the `:`
tokens.

**Test Plan** Added a regression test.

149 instances of unstable formatting remaining.

```
$ cargo run --bin ruff_dev --release -- format-dev --stability-check --error-file formatter-ecosystem-errors.txt --multi-project target/checkouts > formatter-ecosystem-progress.txt
$ rg "Unstable formatting" target/formatter-ecosystem-errors.txt | wc -l
149
```
  • Loading branch information
konstin committed Jul 21, 2023
1 parent 519dbdf commit 972f9a9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ wsl = { version = "0.1.0" }
# v1.0.1
libcst = { git = "https://github.com/Instagram/LibCST.git", rev = "3cacca1a1029f05707e50703b49fe3dd860aa839", default-features = false }

ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" , default-features = false, features = ["num-bigint"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c", default-features = false, features = ["num-bigint"] }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c", default-features = false }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" , default-features = false, features = ["full-lexer", "num-bigint"] }
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc" , default-features = false, features = ["num-bigint"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc", default-features = false, features = ["num-bigint"] }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc", default-features = false }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc" , default-features = false, features = ["full-lexer", "num-bigint"] }

[profile.release]
lto = "fat"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@
a = (
lambda # Dangling
: 1
)
)

# Regression test: lambda empty arguments ranges were too long, leading to unstable
# formatting
(lambda:(#
),)
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ z) # Trailing
a = (
lambda # Dangling
: 1
)```
)
# Regression test: lambda empty arguments ranges were too long, leading to unstable
# formatting
(lambda:(#
),)
```

## Output
```py
Expand Down Expand Up @@ -130,6 +136,13 @@ lambda x: lambda y: lambda z: (
a = (
lambda: 1 # Dangling
)
# Regression test: lambda empty arguments ranges were too long, leading to unstable
# formatting
(
lambda: ( #
),
)
```


Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ruff_python_ast = { path = "../crates/ruff_python_ast" }
ruff_python_formatter = { path = "../crates/ruff_python_formatter" }
similar = { version = "2.2.1" }

rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "db04fd415774032e1e2ceb03bcbf5305e0d22c8c" , default-features = false, features = ["full-lexer", "num-bigint"] }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1c08c5984d31be9af9e2cd10dfe8e3d32d6eb0bc" , default-features = false, features = ["full-lexer", "num-bigint"] }

# Prevent this from interfering with workspaces
[workspace]
Expand Down

0 comments on commit 972f9a9

Please sign in to comment.