Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypedDict doesn't support Union[Literal] as key "string literal" #7752

Open
b0g3r opened this issue Oct 19, 2019 · 1 comment
Open

TypedDict doesn't support Union[Literal] as key "string literal" #7752

b0g3r opened this issue Oct 19, 2019 · 1 comment

Comments

@b0g3r
Copy link

b0g3r commented Oct 19, 2019

Possibly bug.
Related: #7644

Reproducible example

from typing_extensions import Literal, TypedDict

class TDict(TypedDict):
    KEY1: int
    KEY2: int

d1: TDict = {'KEY1': 1, 'KEY2': 2}

key2: Literal['KEY1']
d2: TDict = {key2: 1, 'KEY2': 2}

key3: Literal['KEY1', 'KEY2']
d3: TDict = {key3: 1, 'KEY2': 2}  # error: Expected TypedDict key to be string literal

I use mypy 0.740 and python3.6

@ilevkivskyi
Copy link
Member

The code you wrote is actually unsafe. However, a safe variant with total=False doesn't type-check either. We can potentially support this, but I think this is low priority as I can't imagine many scenarios where this can be useful.

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

No branches or pull requests

3 participants