You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pyrefly constructs an anonymous TypedDict for any small literal dict where all keys are literal strings, but this can lead to confusing error messages e.g.
d= {'a': 42}
d['a'] ='b'
errors with
Literal['b'] is not assignable to TypedDict key a with type int [bad-typed-dict-key]
I think this is confusing since the user didn't specify any TypedDict. I would personally prefer a bad-assignment or bad-argument-type. Other type checkers produce one of these two, or equivalent.
Thanks for the clear report and sandbox link, @QEDady! Confirmed this reproduces on HEAD with default preset.
Comparison with other type checkers, For:
d = {'a': 42}
d['a'] = 'b'
mypy infers d: dict[str, int] and produces:
error: Incompatible types in assignment (expression has type "str", target has type "int") [assignment]
pyright infers d: dict[str, int] and produces:
error: Type "str" is not assignable to type "int"
"str" is not assignable to "int" (reportAssignmentType)
pyrefly infers an anonymous TypedDict and produces:
ERROR Literal['b'] is not assignable to TypedDict key a with type int [bad-typed-dict-key]
Neither mypy nor pyright mentions "TypedDict" because they treat the literal dict as dict[str, int] and emit a straightforward assignment-type error. Pyrefly's anonymous TypedDict inference gives more per-key precision (each key tracks its own type independently), but the error message references a concept the user never introduced.
@yangdanny97 do we change how Pyrefly emits errors here or do we change how Pyrefly infers types from Dicts. I think changing the error message to match what the user wants (emitting bad-assignment) should be the better choice.
Activity
NathanTempest commentedon May 14, 2026
Thanks for the clear report and sandbox link, @QEDady! Confirmed this reproduces on HEAD with default preset.
Comparison with other type checkers, For:
mypy infers d: dict[str, int] and produces:
error: Incompatible types in assignment (expression has type "str", target has type "int") [assignment]
pyright infers d: dict[str, int] and produces:
error: Type "str" is not assignable to type "int"
"str" is not assignable to "int" (reportAssignmentType)
pyrefly infers an anonymous TypedDict and produces:
ERROR
Literal['b']is not assignable to TypedDict keyawith typeint[bad-typed-dict-key]Neither mypy nor pyright mentions "TypedDict" because they treat the literal dict as dict[str, int] and emit a straightforward assignment-type error. Pyrefly's anonymous TypedDict inference gives more per-key precision (each key tracks its own type independently), but the error message references a concept the user never introduced.
@yangdanny97 do we change how Pyrefly emits errors here or do we change how Pyrefly infers types from Dicts. I think changing the error message to match what the user wants (emitting bad-assignment) should be the better choice.
yangdanny97 commentedon May 19, 2026
i have a fix coming, which does the following 3 things
improve typed dict error messages