Respect parenthesized generators in has_own_parentheses (#8100)

## Summary

When analyzing:

```python
if "root" not in (
    long_tree_name_tree.split("/")[0]
    for long_tree_name_tree in really_really_long_variable_name
):
    msg = "Could not find root. Please try a different forest."
    raise ValueError(msg)
```

We missed that the generator expression is parenthesized, because the
parentheses are _part_ of the generator -- so
`is_expression_parenthesized` returns `False`. We needed to take into
account that generators and tuples may or may not be parenthesized when
determining whether we can omit parentheses while splitting an
expression.

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

## Test Plan

No changes in similarity.

Before:

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

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |

After:

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

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |
This commit is contained in:
Charlie Marsh 2023-10-22 16:58:25 -07:00 committed by GitHub
parent bcaac9693b
commit 95702e408f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 20 deletions

View file

@ -475,9 +475,8 @@ aaaaaaaaaaaaaa + {
aaaaaaaaaaaaaa + [
a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
]
(
aaaaaaaaaaaaaa
+ (a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)
aaaaaaaaaaaaaa + (
a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
)
aaaaaaaaaaaaaa + {
a for x in bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

View file

@ -143,7 +143,7 @@ if not \
a:
pass
# Regression: https://github.com/astral-sh/ruff/issues/5338
# Regression test for: https://github.com/astral-sh/ruff/issues/5338
if a and not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa & aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
pass
@ -168,7 +168,7 @@ if True:
):
pass
# https://github.com/astral-sh/ruff/issues/7448
# Regression test for: https://github.com/astral-sh/ruff/issues/7448
x = (
# a
not # b
@ -178,6 +178,14 @@ x = (
True
)
)
# Regression test for: https://github.com/astral-sh/ruff/issues/8090
if "root" not in (
long_tree_name_tree.split("/")[0]
for long_tree_name_tree in really_really_long_variable_name
):
msg = "Could not find root. Please try a different forest."
raise ValueError(msg)
```
## Output
@ -328,7 +336,7 @@ if (
if not a:
pass
# Regression: https://github.com/astral-sh/ruff/issues/5338
# Regression test for: https://github.com/astral-sh/ruff/issues/5338
if (
a
and not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
@ -357,7 +365,7 @@ if True:
):
pass
# https://github.com/astral-sh/ruff/issues/7448
# Regression test for: https://github.com/astral-sh/ruff/issues/7448
x = (
# a
# b
@ -367,6 +375,14 @@ x = (
True
)
)
# Regression test for: https://github.com/astral-sh/ruff/issues/8090
if "root" not in (
long_tree_name_tree.split("/")[0]
for long_tree_name_tree in really_really_long_variable_name
):
msg = "Could not find root. Please try a different forest."
raise ValueError(msg)
```