Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
A dict display with an unpacked conditional mapping, e.g. {**({'a': v} if c else {'b': v})}, was inferred as dict[str, str] even under a Mapping[LiteralString, str] hint, because the unpack operand was inferred without the dict's contextual hint and unions of mappings were unpacked by solving a single Mapping[K, V] against the whole union.
Propagate the decomposed key and value hints into the '**' operand, and fall back to unpacking a union of mappings member-wise only when the ordinary mapping extraction does not recognize the union. This keeps the literal key types of the conditional's dict-literal branches without changing the inferred types of unpacked unions that the existing extraction already handles.
ImBinarry
force-pushed
the
fix/dict-unpack-literalstring-keys
branch
from
November 14, 2026 08:22
9f577e4 to
f491fe3
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5632
{**({"left": value} if condition else {"right": value})}was inferred asdict[str, str]and rejected by aMapping[LiteralString, str]return, even though every key is a literal string. Both simpler spellings work ({**{"left": value}}and the conditional without**), so only the composition was broken. Turns out the**operand got no hint, so the branches lost their literal keys before unpacking; it now gets that hint, and mapping unions unpack member-wise. This also fixes{**c}for an explicitly annotated union of dicts.Test Plan
New
test_dict_unpack_conditional_literalstring_keysinpyrefly/lib/test/dict.rs(issue repro + negative controls) fails on main, passes with the fix.cargo test -p pyrefly --lib test::dictand the other**modules: 1,076 passedcargo fmt -- --checkclean; CLI on the repro: 0 errors, negative control still reportsbad-returnNot run locally: full lib suite and clippy (CI covers both).