Skip to content

Fix transformation of non-iter dict methods in for loops #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libmodernize/fixes/fix_dict_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ def transform(self, node, results):
return super(FixDictSix, self).transform(node, results)
else:
return self.transform_iter(method_name, node, results['head'])

def in_special_context(self, node, isiter):
# Redefined from parent class to make "for x in d.items()" count as
# in special context; 2to3 only counts for loops as special context
# for the iter* methods.
return super(FixDictSix, self).in_special_context(node, True)
11 changes: 11 additions & 0 deletions tests/test_fix_dict_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
list(x.{type}())
""")

DICT_IN_LOOP = ("""\
for k in x.items():
pass
""", """\
for k in x.items():
pass
""")


def check_all_types(input, output):
for type_ in TYPES:
Expand All @@ -40,3 +48,6 @@ def test_dict_view():

def test_dict_plain():
check_all_types(*DICT_PLAIN)

def test_dict_in_loop():
check_on_input(*DICT_IN_LOOP)