| 1 |
"""Fixer that adds parentheses where they are required |
| 2 |
|
| 3 |
This converts ``[x for x in 1, 2]`` to ``[x for x in (1, 2)]``.""" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from .. import fixer_base |
| 9 |
from ..fixer_util import LParen, RParen |
| 10 |
|
| 11 |
|
| 12 |
class FixParen(fixer_base.BaseFix): |
| 13 |
BM_compatible = True |
| 14 |
|
| 15 |
PATTERN = """ |
| 16 |
atom< ('[' | '(') |
| 17 |
(listmaker< any |
| 18 |
comp_for< |
| 19 |
'for' NAME 'in' |
| 20 |
target=testlist_safe< any (',' any)+ [','] |
| 21 |
> |
| 22 |
[any] |
| 23 |
> |
| 24 |
> |
| 25 |
| |
| 26 |
testlist_gexp< any |
| 27 |
comp_for< |
| 28 |
'for' NAME 'in' |
| 29 |
target=testlist_safe< any (',' any)+ [','] |
| 30 |
> |
| 31 |
[any] |
| 32 |
> |
| 33 |
>) |
| 34 |
(']' | ')') > |
| 35 |
""" |
| 36 |
|
| 37 |
def transform(self, node, results): |
| 38 |
target = results["target"] |
| 39 |
|
| 40 |
lparen = LParen() |
| 41 |
lparen.prefix = target.prefix |
| 42 |
target.prefix = "" |
| 43 |
target.insert_child(0, lparen) |
| 44 |
target.append_child(RParen()) |