In dict instead of in dict.keys()

Python pattern

Rewrite in dict.keys() to in dict. Rule SIM118 from flake8-simplify.

Limitations:

  • The change is not applied to for loops.

Apply with the Grit CLI
grit apply in_dict_keys

Replace in dict.keys() with in dict

BEFORE
found = key in foo.keys()

if name in names.keys():
    print(f"{name} found")

# TODO: this for loop should also be simplified
for name in names.keys():
    print(name)

key in sorted(foo.keys())[:10]

(a, b) in foo.items()
AFTER
found = key in foo

if name in names:
    print(f"{name} found")

# TODO: this for loop should also be simplified
for name in names.keys():
    print(name)

key in sorted(foo.keys())[:10]

(a, b) in foo.items()