Index: lib2to3/tests/test_fixers.py =================================================================== --- lib2to3/tests/test_fixers.py (revision 61573) +++ lib2to3/tests/test_fixers.py (working copy) @@ -2913,6 +2913,33 @@ self.check(b, a) +class Test_paren(FixerTestCase): + fixer = "paren" + + def test_0(self): + b = """[i for i in 1, 2 ]""" + a = """[i for i in (1, 2) ]""" + self.check(b, a) + + def test_preserve(self): + b = """[i for i in 1, 2 ]""" + a = """[i for i in (1, 2) ]""" + self.check(b, a) + + def test_1(self): + b = """[i for i in 1, 2, ]""" + a = """[i for i in (1, 2,) ]""" + self.check(b, a) + + def test_unchanged_0(self): + s = """[i for i in (1, 2)]""" + self.unchanged(s) + + def test_unchanged_1(self): + s = """[i for i in foo()]""" + self.unchanged(s) + + if __name__ == "__main__": import __main__ support.run_all_tests(__main__) Index: lib2to3/fixes/fix_paren.py =================================================================== --- lib2to3/fixes/fix_paren.py (revision 0) +++ lib2to3/fixes/fix_paren.py (revision 0) @@ -0,0 +1,31 @@ +"""Fixer that addes parentheses where they are required + +This converts ``[x for x in 1, 2]`` to ``[x for x in (1, 2)]``.""" + +# Local imports +from . import basefix +from .util import Name, ListComp, LParen, RParen + +class FixParen(basefix.BaseFix): + PATTERN = """ + atom< '[' + listmaker< any + comp_for< + 'for' any 'in' + it=testlist_safe< any (',' any)+ [','] + [comp_iter] > + > + > + ']' > + """ + + def transform(self, node, results): + it = results["it"] + + prefix = it.get_prefix() + it.set_prefix("") + it.insert_child(0, LParen()) + it.append_child(RParen()) + it.set_prefix(prefix) + + return node