Skip to content

[pyrefly] Contextualize dict subscript literals for Any - #4391

Open
lyydsheep wants to merge 1 commit into
facebook:mainfrom
lyydsheep:codex/issue-4301-contextual-dict-subscript
Open

[pyrefly] Contextualize dict subscript literals for Any#4391
lyydsheep wants to merge 1 commit into
facebook:mainfrom
lyydsheep:codex/issue-4301-contextual-dict-subscript

Conversation

@lyydsheep

Copy link
Copy Markdown

Fixes #4301

A dict literal assigned through an explicitly annotated dict[str, Any] subscript now receives a soft value hint, avoiding a spurious implicit-any-empty-container diagnostic. Unannotated and narrow dictionary targets retain their existing diagnostics and inference behavior.

Validation:

  • cargo test -p pyrefly test::inference::test_implicit_any
  • cargo test -p pyrefly test::dict
  • python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema

The local .scratch planning files are intentionally not part of this PR.

@meta-cla meta-cla Bot added the cla signed label Aug 1, 2026
@github-actions github-actions Bot added the size/s label Aug 1, 2026
@meta-codesync

meta-codesync Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D114458708. (Because this pull request was imported automatically, there will not be any future comments.)

@IBlackVoid

Copy link
Copy Markdown

Heads up rather than having this show up unannounced — I opened #4409 with a more general take on the same bug. You got here first, and I'd rather flag it than surprise you.

Same idea, but keyed on whether the placeholder can escape rather than on the target's shape, which turned out to matter more than I expected. Measured, both patches applied to the same base and run on the same inputs:

case main #4391 #4409
dict[str, Any] (the report) error fixed fixed
dict[int, Any] error error fixed
MutableMapping[str, Any] error error fixed
defaultdict[str, Any] error error fixed
list[Any] error error fixed
hand-written __setitem__ error error fixed
x = {} then x["y"] = {...} error error error

The key-type condition in the qname match is what narrows yours: the key has nothing to do with whether the value is Any, so dict[int, Any] gets missed. Real code hits this — test_configparser.py does cf["A"] = {} where cf is a MutableMapping, not a builtins.dict.

Digging further also turned up a second root cause behind the same diagnostic: checking against Any never pins the placeholder either, which produces the identical false positive on attribute assignment, return position, default arguments and TypedDict fields.

Genuinely happy to fold this into your PR instead of landing it separately if you'd prefer — the analysis is the part I care about, not whose branch it goes in on.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implicit-any-empty-container false positive when assigning dict containing None to dict[str, Any]

3 participants