ruff/crates/ruff_python_formatter/src/expression
Charlie Marsh 53246b725e
Allow return type annotations to use their own parentheses (#6436)
## Summary

This PR modifies our logic for wrapping return type annotations.
Previously, we _always_ wrapped the annotation in parentheses if it
expanded; however, Black only exhibits this behavior when the function
parameters is empty (i.e., it doesn't and can't break). In other cases,
it uses the normal parenthesization rules, allowing nodes to bring their
own parentheses.

For example, given:

```python
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> Set[
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]:
    ...

def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(x) -> Set[
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]:
    ...
```

Black will format as:

```python
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
    Set[
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    ]
):
    ...


def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
    x,
) -> Set[
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
]:
    ...
```

Whereas, prior to this PR, Ruff would format as:

```python
def xxxxxxxxxxxxxxxxxxxxxxxxxxxx() -> (
    Set[
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    ]
):
    ...


def xxxxxxxxxxxxxxxxxxxxxxxxxxxx(
    x,
) -> (
    Set[
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    ]
):
    ...
```

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

## Test Plan

Before:

- `zulip`: 0.99702
- `django`: 0.99784
- `warehouse`: 0.99585
- `build`: 0.75623
- `transformers`: 0.99470
- `cpython`: 0.75988
- `typeshed`: 0.74853

After:

- `zulip`: 0.99724
- `django`: 0.99791
- `warehouse`: 0.99586
- `build`: 0.75623
- `transformers`: 0.99474
- `cpython`: 0.75956
- `typeshed`: 0.74857
2023-08-11 18:19:21 +00:00
..
expr_attribute.rs Avoid breaking call chains unnecessarily (#6488) 2023-08-11 13:33:15 +00:00
expr_await.rs Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
expr_bin_op.rs Call chain formatting in fluent style (#6151) 2023-08-04 13:58:01 +00:00
expr_bool_op.rs Fix formatting of chained boolean operations (#6394) 2023-08-07 12:22:33 -05:00
expr_call.rs Call chain formatting in fluent style (#6151) 2023-08-04 13:58:01 +00:00
expr_compare.rs Replace Formatter<PyFormatContext<'_>> with PyFormatter (#6330) 2023-08-04 10:48:58 +02:00
expr_constant.rs Replace .map_or(false, $closure) with .is_some_and(closure) (#6244) 2023-08-01 19:29:42 +02:00
expr_dict.rs Move empty_parenthesized into the parentheses.rs (#6403) 2023-08-08 19:17:17 +00:00
expr_dict_comp.rs Add a with_dangling_comments to the parenthesized formatter (#6402) 2023-08-07 19:12:12 +00:00
expr_f_string.rs Rename JoinedStr to FString in the AST (#6379) 2023-08-07 17:33:17 +00:00
expr_formatted_value.rs Track formatting all comments 2023-08-10 09:19:27 +02:00
expr_generator_exp.rs Add a with_dangling_comments to the parenthesized formatter (#6402) 2023-08-07 19:12:12 +00:00
expr_if_exp.rs Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
expr_ipy_escape_command.rs Rename Magic* to IpyEscape* (#6395) 2023-08-09 13:28:18 +00:00
expr_lambda.rs Fix formatting of lambda star arguments (#6257) 2023-08-02 19:31:20 +00:00
expr_list.rs Move empty_parenthesized into the parentheses.rs (#6403) 2023-08-08 19:17:17 +00:00
expr_list_comp.rs Add a with_dangling_comments to the parenthesized formatter (#6402) 2023-08-07 19:12:12 +00:00
expr_name.rs Remove Parse trait (#6235) 2023-08-01 18:35:03 +02:00
expr_named_expr.rs Force parentheses for named expressions in more contexts (#6494) 2023-08-11 01:54:46 -04:00
expr_set.rs Add a with_dangling_comments to the parenthesized formatter (#6402) 2023-08-07 19:12:12 +00:00
expr_set_comp.rs Add a with_dangling_comments to the parenthesized formatter (#6402) 2023-08-07 19:12:12 +00:00
expr_slice.rs Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
expr_starred.rs Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
expr_subscript.rs Call chain formatting in fluent style (#6151) 2023-08-04 13:58:01 +00:00
expr_tuple.rs Move empty_parenthesized into the parentheses.rs (#6403) 2023-08-08 19:17:17 +00:00
expr_unary_op.rs Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
expr_yield.rs Replace Formatter<PyFormatContext<'_>> with PyFormatter (#6330) 2023-08-04 10:48:58 +02:00
expr_yield_from.rs Add AnyExpressionYield to consolidate ExprYield and ExprYieldFrom (#6127) 2023-07-27 16:01:16 +00:00
mod.rs Allow return type annotations to use their own parentheses (#6436) 2023-08-11 18:19:21 +00:00
number.rs Replace Formatter<PyFormatContext<'_>> with PyFormatter (#6330) 2023-08-04 10:48:58 +02:00
parentheses.rs Allow return type annotations to use their own parentheses (#6436) 2023-08-11 18:19:21 +00:00
string.rs Rename JoinedStr to FString in the AST (#6379) 2023-08-07 17:33:17 +00:00