Skip to content

dict.items() not converted to list(dict.items()) when iterated over #176

Open
@hniksic

Description

@hniksic

It appears that python-modernize doesn't convert for k, v in d.items() to for k, v in list(d.items()), assuming that the use of items() was an accident in Python 2. This is not always the case - consider the following two functions:

def foo1(d):
    return d.items()

def foo2(d):
    for k, v in d.items():
        if v % 2 == 0:
            del d[k]

When python-modernize is run on the code, it correctly modifies foo1 to return list(d.items()), but it doesn't modify foo2 to iterate over list(d.items()). In our code base this led to many subtle bugs because our Python 2 code was very careful to only use items() (and keys() and values()) because the dictionary was being modified.

Is there a way to opt out of the special-handling of items inside for?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions