Skip to content

deduplicate keys in dict.fromkeys inference#3132

Open
nabhan06 wants to merge 1 commit into
pylint-dev:mainfrom
nabhan06:dict-fromkeys-dedup
Open

deduplicate keys in dict.fromkeys inference#3132
nabhan06 wants to merge 1 commit into
pylint-dev:mainfrom
nabhan06:dict-fromkeys-dedup

Conversation

@nabhan06

@nabhan06 nabhan06 commented Jul 6, 2026

Copy link
Copy Markdown

Type of Changes

Type
🐛 Bug fix

Description

infer_dict_fromkeys keeps one entry per element without deduplicating, so dict.fromkeys("aab") infers a dict with keys "a", "a", "b" while the runtime call returns {"a": None, "b": None}; the same happens for list/tuple/dict inputs. Because each character becomes its own Const node, this also means dict.fromkeys("x" * 10 ** 8) (a compact string the multiplication guard deliberately allows) expands into ~10^8 dict nodes while analyzing untrusted code, for what is really a single-key dict. The fix deduplicates the keys by value in each branch, matching dict.fromkeys semantics, which drops the inferred dict back to its real keys and removes the per-character node blowup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant