Skip to content

Support more than just string literals with TypedDict indexing #7867

Closed

Description

@wadetregaskis-linkedin
from mypy_extensions import TypedDict

T = TypedDict('T', {'a': float, 'b': float, 'c': float})

foo: T = {'a': 0.0, 'b': 0.0, 'c': 0.0}

for key in ('a', 'b', 'c'):
    foo[key] = 1.0

mypy currently (0.730) complains about that last line, saying error: TypedDict key must be a string literal; expected one of ('a', 'b', 'c'). It's understandable, but limiting - it'd be great if mypy could recognise that key is in fact one of the valid keys for T.

Activity

  1. wadetregaskis-linkedin commented on Nov 4, 2019

    @wadetregaskis-linkedin
    Author

    Aaaaand now I see this juicy-looking patch that went into 0.740, which might be exactly this improvement. Testing now (against my real code, not the hugely simplified case I provided previously)…

  2. wadetregaskis-linkedin commented on Nov 5, 2019

    @wadetregaskis-linkedin
    Author

    Unfortunately, no, mypy 0.740 hasn't addressed this limitation.

  3. ilevkivskyi commented on Nov 8, 2019

    @ilevkivskyi
    Member

    This looks like really low priority.

  4. jamesbraza commented on Dec 15, 2020

    @jamesbraza
    Contributor

    Seems related: #7178

    Also, +1 on this issue, I think this will be a nice feature.

  5. 97littleleaf11 commented on Jan 7, 2022

    @97littleleaf11
    Collaborator

    There is a duplicate issue #9230 with more discussion. Let's just close this one!

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions