Skip to content

Commit b7fab3e

Browse files
committed
Fix: replace dict.iteritems regardless of context.
1 parent 3d99d07 commit b7fab3e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

libmodernize/fixes/fix_dict_six.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ def transform_iter(self, node, results):
3030
assert method_name[4:] in (u'keys', u'items', u'values'), repr(method)
3131
head = [n.clone() for n in head]
3232
tail = [n.clone() for n in tail]
33-
special = not tail and self.in_special_context(node, isiter)
3433
new = pytree.Node(syms.power, head)
35-
if not special:
36-
new.prefix = u''
37-
new = fixer_util.Call(name, [new])
34+
new.prefix = u''
35+
new = fixer_util.Call(name, [new])
3836
if tail:
3937
new = pytree.Node(syms.power, [new] + tail)
4038
new.prefix = node.prefix

tests/test_fix_dict_six.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import absolute_import
22

3-
from utils import check_on_input
3+
from libmodernize.tests.utils import check_on_input
44

55

66
TYPES = 'keys', 'items', 'values'
@@ -35,6 +35,16 @@
3535
pass
3636
""")
3737

38+
DICT_ITER_IN_LOOP = ("""\
39+
for k in x.iter{type}():
40+
pass
41+
""", """\
42+
from __future__ import absolute_import
43+
import six
44+
for k in six.iter{type}(x):
45+
pass
46+
""")
47+
3848
CHAINED_CALLS = ("""\
3949
(x + y).foo().iter{type}().bar()
4050
""", """\
@@ -60,5 +70,8 @@ def test_dict_plain():
6070
def test_dict_in_loop():
6171
check_on_input(*DICT_IN_LOOP)
6272

73+
def test_dict_iter_in_loop():
74+
check_all_types(*DICT_ITER_IN_LOOP)
75+
6376
def test_chained_calls():
6477
check_all_types(*CHAINED_CALLS)

0 commit comments

Comments
 (0)