Fix parenthesized detection for tuples (#6599)

## Summary

This PR fixes our code for detecting whether a tuple has its own
parentheses, which is necessary when attempting to preserve parentheses.
As-is, we were getting some cases wrong, like `(a := 1), (b := 3))` --
the detection code inferred that this _was_ parenthesized, and so
wrapped the entire thing in an unnecessary set of parentheses.

## Test Plan

`cargo test`

Before:

| project      | similarity index |
|--------------|------------------|
| cpython      | 0.75472          |
| django       | 0.99804          |
| transformers | 0.99618          |
| twine        | 0.99876          |
| typeshed     | 0.74288          |
| warehouse    | 0.99601          |
| zulip        | 0.99727          |

After:
| project      | similarity index |
|--------------|------------------|
| cpython      | 0.75473          |
| django       | 0.99804 |
| transformers | 0.99618          |
| twine        | 0.99876          |
| typeshed     | 0.74288          |
| warehouse    | 0.99601          |
| zulip        | 0.99727          |
This commit is contained in:
Charlie Marsh 2023-08-16 09:20:48 -04:00 committed by GitHub
parent daac31d2b9
commit 95f78821ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 71 deletions

View file

@ -1,54 +0,0 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/py_39/pep_572_py39.py
---
## Input
```py
# Unparenthesized walruses are now allowed in set literals & set comprehensions
# since Python 3.9
{x := 1, 2, 3}
{x4 := x**5 for x in range(7)}
# We better not remove the parentheses here (since it's a 3.10 feature)
x[(a := 1)]
x[(a := 1), (b := 3)]
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -4,4 +4,4 @@
{x4 := x**5 for x in range(7)}
# We better not remove the parentheses here (since it's a 3.10 feature)
x[(a := 1)]
-x[(a := 1), (b := 3)]
+x[((a := 1), (b := 3))]
```
## Ruff Output
```py
# Unparenthesized walruses are now allowed in set literals & set comprehensions
# since Python 3.9
{x := 1, 2, 3}
{x4 := x**5 for x in range(7)}
# We better not remove the parentheses here (since it's a 3.10 feature)
x[(a := 1)]
x[((a := 1), (b := 3))]
```
## Black Output
```py
# Unparenthesized walruses are now allowed in set literals & set comprehensions
# since Python 3.9
{x := 1, 2, 3}
{x4 := x**5 for x in range(7)}
# We better not remove the parentheses here (since it's a 3.10 feature)
x[(a := 1)]
x[(a := 1), (b := 3)]
```

View file

@ -9,6 +9,8 @@ a1 = 1, 2
a2 = (1, 2)
a3 = (1, 2), 3
a4 = ((1, 2), 3)
a5 = (1), (2)
a6 = ((1), (2))
# Wrapping parentheses checks
b1 = (("Michael", "Ende"), ("Der", "satanarchäolügenialkohöllische", "Wunschpunsch"), ("Beelzebub", "Irrwitzer"), ("Tyrannja", "Vamperl"),)
@ -79,6 +81,8 @@ a1 = 1, 2
a2 = (1, 2)
a3 = (1, 2), 3
a4 = ((1, 2), 3)
a5 = (1), (2)
a6 = ((1), (2))
# Wrapping parentheses checks
b1 = (

View file

@ -38,6 +38,16 @@ for (x, y) in (z, w):
# type comment
for x in (): # type: int
...
# Tuple parentheses for iterable.
for x in 1, 2, 3:
pass
for x in (1, 2, 3):
pass
for x in 1, 2, 3,:
pass
```
## Output
@ -76,6 +86,20 @@ for x, y in (z, w):
# type comment
for x in (): # type: int
...
# Tuple parentheses for iterable.
for x in 1, 2, 3:
pass
for x in (1, 2, 3):
pass
for x in (
1,
2,
3,
):
pass
```