Commit ed10a30e authored by Benjamin Peterson's avatar Benjamin Peterson

enumerate only requires an iterable (closes #16573)

Patch by Jonathan Kotta.
parent ac26a2e7
...@@ -165,7 +165,7 @@ def parenthesize(node): ...@@ -165,7 +165,7 @@ def parenthesize(node):
consuming_calls = set(["sorted", "list", "set", "any", "all", "tuple", "sum", consuming_calls = set(["sorted", "list", "set", "any", "all", "tuple", "sum",
"min", "max"]) "min", "max", "enumerate"])
def attr_chain(obj, attr): def attr_chain(obj, attr):
"""Follow an attribute chain. """Follow an attribute chain.
...@@ -192,14 +192,14 @@ p0 = """for_stmt< 'for' any 'in' node=any ':' any* > ...@@ -192,14 +192,14 @@ p0 = """for_stmt< 'for' any 'in' node=any ':' any* >
p1 = """ p1 = """
power< power<
( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' | ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' |
'any' | 'all' | (any* trailer< '.' 'join' >) ) 'any' | 'all' | 'enumerate' | (any* trailer< '.' 'join' >) )
trailer< '(' node=any ')' > trailer< '(' node=any ')' >
any* any*
> >
""" """
p2 = """ p2 = """
power< power<
'sorted' ( 'sorted' | 'enumerate' )
trailer< '(' arglist<node=any any*> ')' > trailer< '(' arglist<node=any any*> ')' >
any* any*
> >
...@@ -207,14 +207,14 @@ power< ...@@ -207,14 +207,14 @@ power<
pats_built = False pats_built = False
def in_special_context(node): def in_special_context(node):
""" Returns true if node is in an environment where all that is required """ Returns true if node is in an environment where all that is required
of it is being itterable (ie, it doesn't matter if it returns a list of it is being iterable (ie, it doesn't matter if it returns a list
or an itterator). or an iterator).
See test_map_nochange in test_fixers.py for some examples and tests. See test_map_nochange in test_fixers.py for some examples and tests.
""" """
global p0, p1, p2, pats_built global p0, p1, p2, pats_built
if not pats_built: if not pats_built:
p1 = patcomp.compile_pattern(p1)
p0 = patcomp.compile_pattern(p0) p0 = patcomp.compile_pattern(p0)
p1 = patcomp.compile_pattern(p1)
p2 = patcomp.compile_pattern(p2) p2 = patcomp.compile_pattern(p2)
pats_built = True pats_built = True
patterns = [p0, p1, p2] patterns = [p0, p1, p2]
......
...@@ -2981,6 +2981,10 @@ class Test_filter(FixerTestCase): ...@@ -2981,6 +2981,10 @@ class Test_filter(FixerTestCase):
self.unchanged(a) self.unchanged(a)
a = """sorted(filter(f, 'abc'), key=blah)[0]""" a = """sorted(filter(f, 'abc'), key=blah)[0]"""
self.unchanged(a) self.unchanged(a)
a = """enumerate(filter(f, 'abc'))"""
self.unchanged(a)
a = """enumerate(filter(f, 'abc'), start=1)"""
self.unchanged(a)
a = """for i in filter(f, 'abc'): pass""" a = """for i in filter(f, 'abc'): pass"""
self.unchanged(a) self.unchanged(a)
a = """[x for x in filter(f, 'abc')]""" a = """[x for x in filter(f, 'abc')]"""
...@@ -3089,6 +3093,10 @@ class Test_map(FixerTestCase): ...@@ -3089,6 +3093,10 @@ class Test_map(FixerTestCase):
self.unchanged(a) self.unchanged(a)
a = """sorted(map(f, 'abc'), key=blah)[0]""" a = """sorted(map(f, 'abc'), key=blah)[0]"""
self.unchanged(a) self.unchanged(a)
a = """enumerate(map(f, 'abc'))"""
self.unchanged(a)
a = """enumerate(map(f, 'abc'), start=1)"""
self.unchanged(a)
a = """for i in map(f, 'abc'): pass""" a = """for i in map(f, 'abc'): pass"""
self.unchanged(a) self.unchanged(a)
a = """[x for x in map(f, 'abc')]""" a = """[x for x in map(f, 'abc')]"""
...@@ -3152,6 +3160,10 @@ class Test_zip(FixerTestCase): ...@@ -3152,6 +3160,10 @@ class Test_zip(FixerTestCase):
self.unchanged(a) self.unchanged(a)
a = """sorted(zip(a, b), key=blah)[0]""" a = """sorted(zip(a, b), key=blah)[0]"""
self.unchanged(a) self.unchanged(a)
a = """enumerate(zip(a, b))"""
self.unchanged(a)
a = """enumerate(zip(a, b), start=1)"""
self.unchanged(a)
a = """for i in zip(a, b): pass""" a = """for i in zip(a, b): pass"""
self.unchanged(a) self.unchanged(a)
a = """[x for x in zip(a, b)]""" a = """[x for x in zip(a, b)]"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment