Skip to content

Confusing errors with anonymous TypedDict #3210

Closed

Description

@QEDady

Describe the Bug

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.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxAAQDqMUAxrgLYzUAuunAFuwAqkATjDBRS1AMoYcBADS8IcakuqpqxVAHMYAHXQ0A7nxHVSuAK7VGGaoaEQOMBXB6pTM6rHfo1eCxycpMQQ6FoqvoIcPLjohPqUCdQAEjAeHtaWxLDUYDCoHBYiyrhgispoWHj4iEkAtNQA4jCBIqhQdRwQbEHE7Ix8jADWoeF5MDiow-XUAKpw7CIAbvlQAPocwTAAFACUnNyhcH2MgaF5QiKYvfAzMPh9DmzogYaOPLzsAJIAIgCiuXyhWKvAKdnYcAsxGIuCEgW2MEIWkIagCuGYLGyLWc1C0uE6%2BMwolCjggsQUMRWQgULUYuxmtAcTjUWGoQgskVI0VimSJ1EwEBEp3EETMliE1GwQlwhgWQn0%2BjA0pYvVGKkxsMCACoFayALzUYAAclQRsQ1AALAAmAC%2B%2BkwAG0TUaALrUA1G7BGkByEBkERiUiELgsKAUGiCAMiyqyfCZdCQLRFApkuJJSQwdg8DgcYhwRAAegL-tE4kIsK0BZg6ALmHRcALzATECTQhTsQLuVhaiWqGgqGwOSbieTXR5uGIY-QcHi6DI3PQdSpcFT7uouhAAGZCABGK0b-QOtLSoRwF36DndGFwiZ1AVCrorNdGlvoWEwI36N8cOoiACOFiCreQwwKQdRTIw8DKB6hg%2BJ%2B6AgDavpTI%2BMAAGLQDAFAxtUJDkDaQA

(Only applicable for extension issues) IDE Information

No response

Activity

  1. NathanTempest commented on May 14, 2026

    @NathanTempest
    Contributor

    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.

  2. yangdanny97 commented on May 19, 2026

    @yangdanny97
    Collaborator

    i have a fix coming, which does the following 3 things

    1. make invalid assignments report bad-assignment rather than bad-typed-dict-key, the key exists but the value is wrong
    2. print out anonymous typed dict errors differently, omitting mentions of TypedDict
    3. for anonymous typed dict index errors, emit bad-index rather than bad-typed-dict-key
  3. added a commit that references this issue on May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions