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 # By Taek Joo Kim and Benjamin Peterson
6
7 # Local imports
8 from .. import fixer_base
9 from ..fixer_util import LParen, RParen
10
11 # XXX This doesn't support nested for loops like [x for x in 1, 2 for x in 1, 2]
12 class ESC[4;38;5;81mFixParen(ESC[4;38;5;149mfixer_baseESC[4;38;5;149m.ESC[4;38;5;149mBaseFix):
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 = "" # Make it hug the parentheses
43 target.insert_child(0, lparen)
44 target.append_child(RParen())