flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

Warn about `dict.fromkeys(keys, mutable)`

Open janosh opened this issue 2 years ago • 0 comments

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

janosh avatar May 28 '23 15:05 janosh