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

@ -3,6 +3,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"),)

View file

@ -32,3 +32,13 @@ 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