|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import ast
|
4 |
| -import sys |
5 |
| -from typing import Iterable |
| 4 | +from collections.abc import Iterable |
6 | 5 |
|
7 | 6 | from tokenize_rt import Offset
|
8 | 7 | from tokenize_rt import Token
|
|
15 | 14 | from add_trailing_comma._token_helpers import fix_brace
|
16 | 15 |
|
17 | 16 |
|
18 |
| -if sys.version_info >= (3, 9): # pragma: >=3.9 cover |
19 |
| - def _fix_with(i: int, tokens: list[Token]) -> None: |
| 17 | +def _fix_with(i: int, tokens: list[Token]) -> None: |
| 18 | + i += 1 |
| 19 | + if tokens[i].name == 'UNIMPORTANT_WS': |
20 | 20 | i += 1
|
21 |
| - if tokens[i].name == 'UNIMPORTANT_WS': |
22 |
| - i += 1 |
23 |
| - if tokens[i].src == '(': |
24 |
| - fix = find_simple(i, tokens) |
25 |
| - # only fix if outer parens are for the with items (next is ':') |
26 |
| - if fix is not None and tokens[fix.braces[-1] + 1].src == ':': |
27 |
| - fix_brace(tokens, fix, add_comma=True, remove_comma=True) |
| 21 | + if tokens[i].src == '(': |
| 22 | + fix = find_simple(i, tokens) |
| 23 | + # only fix if outer parens are for the with items (next is ':') |
| 24 | + if fix is not None and tokens[fix.braces[-1] + 1].src == ':': |
| 25 | + fix_brace(tokens, fix, add_comma=True, remove_comma=True) |
28 | 26 |
|
29 |
| - @register(ast.With) |
30 |
| - def visit_With( |
31 |
| - state: State, |
32 |
| - node: ast.With, |
33 |
| - ) -> Iterable[tuple[Offset, TokenFunc]]: |
34 |
| - yield ast_to_offset(node), _fix_with |
| 27 | + |
| 28 | +@register(ast.With) |
| 29 | +def visit_With( |
| 30 | + state: State, |
| 31 | + node: ast.With, |
| 32 | +) -> Iterable[tuple[Offset, TokenFunc]]: |
| 33 | + yield ast_to_offset(node), _fix_with |
0 commit comments