1 | n/a | """Fixer that addes parentheses where they are required |
---|
2 | n/a | |
---|
3 | n/a | This converts ``[x for x in 1, 2]`` to ``[x for x in (1, 2)]``.""" |
---|
4 | n/a | |
---|
5 | n/a | # By Taek Joo Kim and Benjamin Peterson |
---|
6 | n/a | |
---|
7 | n/a | # Local imports |
---|
8 | n/a | from .. import fixer_base |
---|
9 | n/a | from ..fixer_util import LParen, RParen |
---|
10 | n/a | |
---|
11 | n/a | # XXX This doesn't support nested for loops like [x for x in 1, 2 for x in 1, 2] |
---|
12 | n/a | class FixParen(fixer_base.BaseFix): |
---|
13 | n/a | BM_compatible = True |
---|
14 | n/a | |
---|
15 | n/a | PATTERN = """ |
---|
16 | n/a | atom< ('[' | '(') |
---|
17 | n/a | (listmaker< any |
---|
18 | n/a | comp_for< |
---|
19 | n/a | 'for' NAME 'in' |
---|
20 | n/a | target=testlist_safe< any (',' any)+ [','] |
---|
21 | n/a | > |
---|
22 | n/a | [any] |
---|
23 | n/a | > |
---|
24 | n/a | > |
---|
25 | n/a | | |
---|
26 | n/a | testlist_gexp< any |
---|
27 | n/a | comp_for< |
---|
28 | n/a | 'for' NAME 'in' |
---|
29 | n/a | target=testlist_safe< any (',' any)+ [','] |
---|
30 | n/a | > |
---|
31 | n/a | [any] |
---|
32 | n/a | > |
---|
33 | n/a | >) |
---|
34 | n/a | (']' | ')') > |
---|
35 | n/a | """ |
---|
36 | n/a | |
---|
37 | n/a | def transform(self, node, results): |
---|
38 | n/a | target = results["target"] |
---|
39 | n/a | |
---|
40 | n/a | lparen = LParen() |
---|
41 | n/a | lparen.prefix = target.prefix |
---|
42 | n/a | target.prefix = "" # Make it hug the parentheses |
---|
43 | n/a | target.insert_child(0, lparen) |
---|
44 | n/a | target.append_child(RParen()) |
---|