Mark trailing comments in parenthesized tests (#6287)

## Summary

This ensures that we treat `# comment` as parenthesized in contexts
like:

```python
while (
    True
    # comment
):
    pass
```

The same logic applies equally to `for`, `async for`, `if`, `with`, and
`async with`. The general pattern is that you have an expression which
precedes a colon-separated suite.
This commit is contained in:
Charlie Marsh 2023-08-03 16:45:03 -04:00 committed by GitHub
parent 51ff98f9e9
commit 1705fcef36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 37 deletions

View file

@ -193,16 +193,6 @@ class C:
```diff
--- Black
+++ Ruff
@@ -28,8 +28,8 @@
while (
# Just a comment
call()
+ ):
# Another
- ):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
@@ -110,19 +110,20 @@
value, is_going_to_be="too long to fit in a single line", srsly=True
), "Not what we expected"
@ -293,8 +283,8 @@ class C:
while (
# Just a comment
call()
):
# Another
):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,

View file

@ -193,16 +193,6 @@ class C:
```diff
--- Black
+++ Ruff
@@ -28,8 +28,8 @@
while (
# Just a comment
call()
+ ):
# Another
- ):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,
@@ -110,19 +110,20 @@
value, is_going_to_be="too long to fit in a single line", srsly=True
), "Not what we expected"
@ -293,8 +283,8 @@ class C:
while (
# Just a comment
call()
):
# Another
):
print(i)
xxxxxxxxxxxxxxxx = Yyyy2YyyyyYyyyyy(
push_manager=context.request.resource_manager,

View file

@ -144,6 +144,14 @@ else: # 7 preceding: last in body, following: fist in alt body, enclosing: exc
print(3) # 8 preceding: last in body, following: fist in alt body, enclosing: try
finally: # 9 preceding: last in body, following: fist in alt body, enclosing: try
print(3) # 10 preceding: last in body, following: any, enclosing: try
try:
pass
except (
ZeroDivisionError
# comment
):
pass
```
## Output
@ -304,6 +312,14 @@ else: # 7 preceding: last in body, following: fist in alt body, enclosing: exc
print(3) # 8 preceding: last in body, following: fist in alt body, enclosing: try
finally: # 9 preceding: last in body, following: fist in alt body, enclosing: try
print(3) # 10 preceding: last in body, following: any, enclosing: try
try:
pass
except (
ZeroDivisionError
# comment
):
pass
```