MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
187 stars 19 forks source link

[New Rule] `if k in d: del d[k]` #195

Open InSyncWithFoo opened 1 week ago

InSyncWithFoo commented 1 week ago

Explanation

When removing a key from a dictionary, there's no need to check for its existence, as the end results will be the same anyway. Thus, dict.pop(key, None) is preferred.

Also see this issue at @astral-sh/ruff.

Example

if key in dictionary:
    del dictionary[key]

# Good
dictionary.pop(key, None)