ruff/crates/ruff_python_formatter/src
Charlie Marsh d616c9b870
Avoid omitting optional parentheses for argument-less parentheses (#6484)
## Summary

This PR fixes some misformattings around optional parentheses for
expressions.

I first noticed that we were misformatting this:

```python
return (
    unicodedata.normalize("NFKC", s1).casefold()
    == unicodedata.normalize("NFKC", s2).casefold()
)
```

The above is stable Black formatting, but we were doing:
```python
return unicodedata.normalize("NFKC", s1).casefold() == unicodedata.normalize(
    "NFKC", s2
).casefold()
```

Above, the "last" expression is a function call, so our
`can_omit_optional_parentheses` was returning `true`...

However, it turns out that Black treats function calls differently
depending on whether or not they have arguments -- presumedly because
they'll never split empty parentheses, and so they're functionally
non-useful. On further investigation, I believe this applies to all
parenthesized expressions. If Black can't split on the parentheses, it
doesn't leverage them when removing optional parentheses.

## Test Plan

Nice increase in similarity scores.

Before:

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

After:

- `zulip`: 0.99705
- `django`: 0.99795
- `warehouse`: 0.99600
- `build`: 0.75623
- `transformers`: 0.99471
- `cpython`: 0.75989
- `typeshed`: 0.74853
2023-08-11 17:58:42 +00:00
..
comments Add formatting for MatchCase (#6360) 2023-08-11 19:20:25 +05:30
expression Avoid omitting optional parentheses for argument-less parentheses (#6484) 2023-08-11 17:58:42 +00:00
module Add empty lines before nested functions and classes (#6206) 2023-08-01 15:30:59 +00:00
other Add formatting for MatchCase (#6360) 2023-08-11 19:20:25 +05:30
pattern Add formatting for MatchCase (#6360) 2023-08-11 19:20:25 +05:30
snapshots Remove exception-handler lexing from unused-bound-exception fix (#5851) 2023-07-18 18:27:46 +00:00
statement Avoid omitting optional parentheses for argument-less parentheses (#6484) 2023-08-11 17:58:42 +00:00
type_param Replace Formatter<PyFormatContext<'_>> with PyFormatter (#6330) 2023-08-04 10:48:58 +02:00
builders.rs Move empty_parenthesized into the parentheses.rs (#6403) 2023-08-08 19:17:17 +00:00
cli.rs Formatter: Add SourceType to context to enable special formatting for stub files (#6331) 2023-08-04 11:52:26 +00:00
context.rs Implement DerefMut for WithNodeLevel (#6443) 2023-08-11 10:41:48 +00:00
generated.rs Rename Magic* to IpyEscape* (#6395) 2023-08-09 13:28:18 +00:00
lib.rs Track formatting all comments 2023-08-10 09:19:27 +02:00
main.rs Formatter: Add SourceType to context to enable special formatting for stub files (#6331) 2023-08-04 11:52:26 +00:00
options.rs Improve Ruff Formatter Interoperability (#6472) 2023-08-10 14:39:53 +02:00
prelude.rs Accept any Into<AnyNodeRef> as Comments arguments (#5205) 2023-06-20 16:49:21 +00:00