PyCQA / flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

janosh opened this issue · 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: astral-sh/ruff#4613