Commit Graph

70 Commits

Author SHA1 Message Date
Brent Westbrook 2cb98d4cdb
working for Micha's exact example 2025-12-11 10:34:35 -05:00
Brent Westbrook 9d7d94c4f0
factor out FormatBody 2025-12-10 16:03:06 -05:00
Brent Westbrook 553b45e27f
move dangling comment handling back out of placement.rs
Revert "re-apply 'pass preview to handle_lambda_comment'"

This reverts commit 33fcca9c53.
2025-12-10 15:58:48 -05:00
Brent Westbrook 9f9b76b035
move preview comment handling mostly into placement.rs 2025-12-10 09:38:15 -05:00
Brent Westbrook 54c768989d
clean up deref 2025-12-09 17:42:25 -05:00
Brent Westbrook 829f10521f
Merge branch 'brent/fix-kwargs' into brent/indent-lambda-params 2025-12-09 17:26:25 -05:00
Brent Westbrook 2e0ee2e0a9
update lambda comment 2025-12-09 17:20:18 -05:00
Brent Westbrook d722155762
make comments leading on parameter_s_ 2025-12-09 16:45:26 -05:00
Brent Westbrook 4ffbd496e3
Merge branch 'main' into brent/indent-lambda-params 2025-12-09 14:34:28 -05:00
Brent Westbrook 0bec5c0362
Fix comment placement in lambda parameters (#21868)
Summary
--

This PR makes two changes to comment placement in lambda parameters.
First, we
now insert a line break if the first parameter has a leading comment:

```py
# input
(
    lambda
    * # comment 2
    x:
    x
)

# main
(
    lambda # comment 2
    *x: x
)

# this PR
(
    lambda
	# comment 2
    *x: x
)
```

Note the missing space in the output from main. This case is currently
unstable
on main. Also note that the new formatting is more consistent with our
stable
formatting in cases where the lambda has its own dangling comment:

```py
# input
(
    lambda # comment 1
    * # comment 2
    x:
    x
)

# output
(
    lambda  # comment 1
    # comment 2
    *x: x
)
```

and when a parameter without a comment precedes the split `*x`:

```py
# input
(
    lambda y,
    * # comment 2
    x:
    x
)

# output
(
    lambda y,
    # comment 2
    *x: x
)
```

This does change the stable formatting, but I think such cases are rare
(expecting zero hits in the ecosystem report), this fixes an existing
instability, and it should not change any code we've previously
formatted.

Second, this PR modifies the comment placement such that `# comment 2`
in these
outputs is still a leading comment on the parameter. This is also not
the case
on main, where it becomes a [dangling lambda
comment](https://play.ruff.rs/3b29bb7e-70e4-4365-88e0-e60fe1857a35?secondary=Comments).
This doesn't cause any
instability that I'm aware of on main, but it does cause problems when
trying to
adjust the placement of dangling lambda comments in #21385. Changing the
placement in this way should not affect any formatting here.

Test Plan
--

New lambda tests, plus existing tests covering the cases above with
multiple
comments around the parameters (see lambda.py 122-143, and 122-205 or so
more
broadly)

I also checked manually that the comments are now leading on the
parameter:

```shell
❯ cargo run --bin ruff_python_formatter -- --emit stdout --target-version 3.10 --print-comments <<EOF
(
    lambda
        # comment 2
    *x: x
)
EOF
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/ruff_python_formatter --emit stdout --target-version 3.10 --print-comments`
# Comment decoration: Range, Preceding, Following, Enclosing, Comment
21..32, None, Some((Parameters, 37..39)), (ExprLambda, 6..42), "# comment 2"
{
    Node {
        kind: Parameter,
        range: 37..39,
        source: `*x`,
    }: {
        "leading": [
            SourceComment {
                text: "# comment 2",
                position: OwnLine,
                formatted: true,
            },
        ],
        "dangling": [],
        "trailing": [],
    },
}
(
    lambda
    # comment 2
    *x: x
)
```

But I didn't see a great place to put a test like this. Is there
somewhere I can assert this comment placement since it doesn't affect
any formatting yet? Or is it okay to wait until we use this in #21385?
2025-12-09 14:07:48 -05:00
Brent Westbrook ea30464a2d
clippy 2025-12-09 08:52:39 -05:00
Brent Westbrook c7b1089631
apply patch
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-12-09 08:51:59 -05:00
Brent Westbrook e8540d9b08
format new dangling comments 2025-12-08 12:56:57 -05:00
Brent Westbrook f20f3e0d49
fix assignment instability without parameters too 2025-12-05 16:08:37 -05:00
Brent Westbrook df42aa29b5
Reapply "try parenthesizing the body without parameters"
This reverts commit 4585a0b3e4.
2025-12-05 16:04:23 -05:00
Brent Westbrook 0710e0bc3e
fix assignment instability with dangling comments 2025-12-05 16:03:46 -05:00
Brent Westbrook 1531c94b4e
revert the last two commits, back to a stable formatting 2025-12-05 15:44:21 -05:00
Brent Westbrook 86406c0bb1
wip 2025-12-05 15:41:49 -05:00
Brent Westbrook 80852c1769
pairing 2025-12-05 15:40:47 -05:00
Brent Westbrook 4d76cd7b15
factor out parameters_have_comments 2025-12-05 14:57:54 -05:00
Brent Westbrook 3c481ba0ed
factor out preview variable 2025-12-05 14:52:16 -05:00
Brent Westbrook 4585a0b3e4
Revert "try parenthesizing the body without parameters"
this is unstable because it moves the comment into a new set of parentheses,
which then means the lambda itself can be unparenthesized

```diff
-a = (
-    lambda: (  # Dangling
-        1
-    )
+a = lambda: (  # Dangling
+    1
 )
```
2025-12-05 13:59:55 -05:00
Brent Westbrook c62390d18c
try parenthesizing the body without parameters 2025-12-05 13:58:33 -05:00
Brent Westbrook fe255d1ad2
leading_comments in block 2025-12-05 13:39:37 -05:00
Brent Westbrook c7666423ba
block indent and trailing comments 2025-12-05 13:22:09 -05:00
Brent Westbrook dc240a1574
clippy 2025-12-05 09:32:46 -05:00
Brent Westbrook 43b53edcab
improve dangling header comment placement 2025-12-05 09:21:48 -05:00
Brent Westbrook 219bbd1ce0
check comment case first 2025-12-05 08:26:58 -05:00
Brent Westbrook 08b1da3ab0
mirror comment handling from `maybe_parenthesize_expression`
and update comment
2025-12-04 15:44:36 -05:00
Brent Westbrook 2e84402f69
add comments and some supporting tests 2025-12-04 10:08:46 -05:00
Brent Westbrook afb01ce84d
combine preview checks 2025-12-04 09:50:39 -05:00
Brent Westbrook 7dddcc85bc
remove comment
I don't think we can move the `fits_expanded` call into the assignment
formatting because that would wrap the whole lambda in a `fits_expanded`, when we
just want to wrap the lambda body in it instead. if I understand correctly, we'd
need to duplicate basically this whole function to inject `fits_expanded` in the
right place for the lambda formatting in assignments
2025-12-03 14:27:42 -05:00
Brent Westbrook 04963a6b6b
expand parent if the lambda body breaks 2025-12-03 14:16:16 -05:00
Brent Westbrook a3400a017a
use parenthesize_if_expands for fluent call chains 2025-12-03 11:51:02 -05:00
Brent Westbrook 9ef9d0302d
fix another ecosystem call expansion 2025-12-03 10:06:48 -05:00
Brent Westbrook 6f6c09c72a
fix snapshot changes for cases with comments 2025-12-03 09:45:00 -05:00
Brent Westbrook efa372b379
apply Micha's patch, fixing everything?
Co-authored-by: Micha Reiser <micha@reiser.io>
2025-12-03 09:14:52 -05:00
Brent Westbrook 24e15bfd95
exclude call and subscript expressions from has_own_parentheses 2025-12-02 11:28:19 -05:00
Brent Westbrook 07bcf41a34
fix binary expression in lambda in return 2025-12-02 11:22:55 -05:00
Brent Westbrook 62c968c826
rough draft of ExprLambdaLayout::Assignment 2025-12-02 11:22:55 -05:00
Brent Westbrook 1b58643040
wip: parenthesize long lambda bodies 2025-12-02 11:22:55 -05:00
Brent Westbrook 8cc884428d
keep lambda parameters on a single line 2025-12-02 11:22:55 -05:00
Ibraheem Ahmed c9dff5c7d5
[ty] AST garbage collection (#18482)
## Summary

Garbage collect ASTs once we are done checking a given file. Queries
with a cross-file dependency on the AST will reparse the file on demand.
This reduces ty's peak memory usage by ~20-30%.

The primary change of this PR is adding a `node_index` field to every
AST node, that is assigned by the parser. `ParsedModule` can use this to
create a flat index of AST nodes any time the file is parsed (or
reparsed). This allows `AstNodeRef` to simply index into the current
instance of the `ParsedModule`, instead of storing a pointer directly.

The indices are somewhat hackily (using an atomic integer) assigned by
the `parsed_module` query instead of by the parser directly. Assigning
the indices in source-order in the (recursive) parser turns out to be
difficult, and collecting the nodes during semantic indexing is
impossible as `SemanticIndex` does not hold onto a specific
`ParsedModuleRef`, which the pointers in the flat AST are tied to. This
means that we have to do an extra AST traversal to assign and collect
the nodes into a flat index, but the small performance impact (~3% on
cold runs) seems worth it for the memory savings.

Part of https://github.com/astral-sh/ty/issues/214.
2025-06-13 08:40:11 -04:00
Micha Reiser 6a1e555537
Upgrade to Rust 1.78 (#11260) 2024-05-03 12:46:21 +00:00
Charlie Marsh d685107638
Move {AnyNodeRef, AstNode} to ruff_python_ast crate root (#8030)
This is a do-over of https://github.com/astral-sh/ruff/pull/8011, which
I accidentally merged into a non-`main` branch. Sorry!
2023-10-18 00:01:18 +00:00
Charlie Marsh 4c4eceee36
Add dangling comment handling for `lambda` expressions (#7493)
## Summary

This PR adds dangling comment handling for `lambda` expressions. In
short, comments around the `lambda` and the `:` are all considered
dangling. Comments that come between the `lambda` and the `:` may be
moved after the colon for simplicity (this is an odd position for a
comment anyway), unless they also precede the lambda parameters, in
which case they're formatted before the parameters.

Closes https://github.com/astral-sh/ruff/issues/7470.

## Test Plan

`cargo test`

No change in similarity.

Before:

| project | similarity index | total files | changed files |

|--------------|------------------:|------------------:|------------------:|
| cpython | 0.76083 | 1789 | 1632 |
| django | 0.99982 | 2760 | 37 |
| transformers | 0.99957 | 2587 | 398 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99983 | 3496 | 18 |
| warehouse | 0.99929 | 648 | 16 |
| zulip | 0.99962 | 1437 | 22 |

After:

| project | similarity index | total files | changed files |

|--------------|------------------:|------------------:|------------------:|
| cpython | 0.76083 | 1789 | 1632 |
| django | 0.99982 | 2760 | 37 |
| transformers | 0.99957 | 2587 | 398 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99983 | 3496 | 18 |
| warehouse | 0.99929 | 648 | 16 |
| zulip | 0.99962 | 1437 | 22 |
2023-09-19 15:23:51 -04:00
Charlie Marsh 8ab2519717
Respect parentheses for precedence in `await` (#7468)
## Summary

We were using `Parenthesize::IfBreaks` universally for `await`, but
dropping parentheses can change the AST due to precedence. It turns out
that Black's rules aren't _exactly_ the same as operator precedence
(e.g., they leave parentheses around `await ([1, 2, 3])`, although they
aren't strictly required).

Closes https://github.com/astral-sh/ruff/issues/7467.

## Test Plan

`cargo test`

No change in similarity.

Before:

| project | similarity index | total files | changed files |

|--------------|------------------:|------------------:|------------------:|
| cpython | 0.76083 | 1789 | 1632 |
| django | 0.99982 | 2760 | 37 |
| transformers | 0.99957 | 2587 | 398 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99983 | 3496 | 18 |
| warehouse | 0.99929 | 648 | 16 |
| zulip | 0.99962 | 1437 | 22 |

After:

| project | similarity index | total files | changed files |

|--------------|------------------:|------------------:|------------------:|
| cpython | 0.76083 | 1789 | 1632 |
| django | 0.99982 | 2760 | 37 |
| transformers | 0.99957 | 2587 | 398 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99983 | 3496 | 18 |
| warehouse | 0.99929 | 648 | 16 |
| zulip | 0.99962 | 1437 | 22 |
2023-09-18 09:56:41 -04:00
qdegraaf 05951dd338
Fix inconsistent `expr_lambda` formatting (#6318) 2023-09-08 09:40:58 +00:00
Micha Reiser c05e4628b1
Introduce Token element (#7048) 2023-09-02 10:05:47 +02:00
Charlie Marsh edb9b0c62a
Use the formatter prelude in more files (#6882)
Removes a bunch of imports that are made redundant by the prelude.
2023-08-25 16:51:07 -04:00