Skip to content

Using Literal typed variables as keys for TypedDict causes "TypedDict key must be a string literal" #6262

Closed
@chungwu

Description

@chungwu

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?
    Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

from mypy_extensions import TypedDict
from typing_extensions import Literal

class Test(TypedDict):
  a: str
  b: str

TestKey = Literal['a', 'b']

def read(test: Test, key: TestKey):
  return test[key]
  • What is the actual behavior/output?
    mypy.py:13: error: TypedDict key must be a string literal; expected one of ('a', 'b')

  • What is the behavior/output you expect?
    No type check errors

  • What are the versions of mypy and Python you are using?
    mypy -- 0.660
    python -- 3.6.3

    Do you see the same issue after installing mypy from Git master?
    Yes

  • What are the mypy flags you are using? (For example --strict-optional)
    None

  • If mypy crashed with a traceback, please paste
    the full traceback below.

(You can freely edit this text, please remove all the lines
you believe are unnecessary.)

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions