[3.6] bpo-28837: Fix lib2to3 handling of map/zip/filter calls when fo… by Mariatta · Pull Request #2235 · python/cpython
# XXX This (rare) case is not supported ## b = """x = filter(f, 'abc')[0]""" ## a = """x = list(filter(f, 'abc'))[0]""" ## self.check(b, a) def test_filter_trailers(self): b = """x = filter(None, 'abc')[0]""" a = """x = [_f for _f in 'abc' if _f][0]""" self.check(b, a)
b = """x = len(filter(f, 'abc')[0])""" a = """x = len(list(filter(f, 'abc'))[0])""" self.check(b, a)
b = """x = filter(lambda x: x%2 == 0, range(10))[0]""" a = """x = [x for x in range(10) if x%2 == 0][0]""" self.check(b, a)
# Note the parens around x b = """x = filter(lambda (x): x%2 == 0, range(10))[0]""" a = """x = [x for x in range(10) if x%2 == 0][0]""" self.check(b, a)
def test_filter_nochange(self): a = """b.join(filter(f, 'abc'))"""
def test_map_trailers(self): b = """x = map(f, 'abc')[0]""" a = """x = list(map(f, 'abc'))[0]""" self.check(b, a)
b = """x = map(None, l)[0]""" a = """x = list(l)[0]""" self.check(b, a)
b = """x = map(lambda x:x, l)[0]""" a = """x = [x for x in l][0]""" self.check(b, a)
b = """x = map(f, 'abc')[0][1]""" a = """x = list(map(f, 'abc'))[0][1]""" self.check(b, a)
def test_trailing_comment(self): b = """x = map(f, 'abc') # foo""" a = """x = list(map(f, 'abc')) # foo"""
# XXX This (rare) case is not supported ## b = """x = map(f, 'abc')[0]""" ## a = """x = list(map(f, 'abc'))[0]""" ## self.check(b, a)
def test_map_nochange(self): a = """b.join(map(f, 'abc'))""" self.unchanged(a)
def test_zip_basic(self): b = """x = zip()""" a = """x = list(zip())""" self.check(b, a)
b = """x = zip(a, b, c)""" a = """x = list(zip(a, b, c))""" self.check(b, a)
def test_zip_trailers(self): b = """x = zip(a, b, c)[0]""" a = """x = list(zip(a, b, c))[0]""" self.check(b, a)
b = """x = zip(a, b, c)[0][1]""" a = """x = list(zip(a, b, c))[0][1]""" self.check(b, a)
def test_zip_nochange(self): a = """b.join(zip(a, b))""" self.unchanged(a)