ruff/crates/ruff_python_formatter/src/comments
Charlie Marsh 55d6fd53cd
Treat comments on open parentheses in return annotations as dangling (#6413)
## Summary

Given:

```python
def double(a: int) -> ( # Hello
    int
):
    return 2*a
```

We currently treat `# Hello` as a trailing comment on the parameters
(`(a: int)`). This PR adds a placement method to instead treat it as a
dangling comment on the function definition itself, so that it gets
formatted at the end of the definition, like:

```python
def double(a: int) -> int:  # Hello
    return 2*a
```

The formatting in this case is unchanged, but it's incorrect IMO for
that to be a trailing comment on the parameters, and that placement
leads to an instability after changing the grouping in #6410.

Fixing this led to a _different_ instability related to tuple return
type annotations, like:

```python
def zrevrangebylex(self, name: _Key, max: _Value, min: _Value, start: int | None = None, num: int | None = None) -> (  # type: ignore[override]
):
    ...
```

(This is a real example.)

To fix, I had to special-case tuples in that spot, though I'm not
certain that's correct.
2023-08-08 16:48:38 -04:00
..
snapshots Rename Arguments to Parameters in the AST (#6253) 2023-08-01 13:53:28 -04:00
debug.rs Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
format.rs Expand parents whenever open-parenthesis comments are present (#6389) 2023-08-08 08:45:20 -04:00
map.rs Correctly associate own-line comments in bodies (#4671) 2023-06-01 08:12:53 +02:00
mod.rs Avoid hard line break after dangling open-parenthesis comments (#6380) 2023-08-07 14:15:32 +00:00
node_key.rs Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
placement.rs Treat comments on open parentheses in return annotations as dangling (#6413) 2023-08-08 16:48:38 -04:00
visitor.rs Add formatting for StmtMatch (#6286) 2023-08-08 18:48:49 +05:30