flake8-bugbear
flake8-bugbear copied to clipboard
Warn about `dict.fromkeys(keys, mutable)`
Suggest the following fix when passing a mutable 2nd arg to dict.fromkeys() since modifications will affect all keys which is a common pitfall.
# bad
dict.fromkeys((1, 2), {})
dict.fromkeys((1, 2), [])
dict.fromkeys((1, 2), set())
# ...
# good
{key: {} for key in (1, 2)}
{key: [] for key in (1, 2)}
{key: set() for key in (1, 2)}
Examples:
Related issue: https://github.com/charliermarsh/ruff/issues/4613