ยปCore Development>Code coverage>Lib/lib2to3/fixes/fix_paren.py

# Python code coverage for Lib/lib2to3/fixes/fix_paren.py

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