-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Using Literal
typed variables as keys for TypedDict causes "TypedDict key must be a string literal"
#6262
Comments
Thanks for the bug report! What's happening in this case is that mypy doesn't yet understand how to handle indexing TypedDicts using Unions of Literals -- it can currently handle indexing using only Literals. It probably should, though. I'll look into making a fix when I find some time later this week/next week. |
Thanks! This is very useful for typing JSON blobs, and having functions that extract information from the JSON blobs using specific keys. It would also be nice to extract a keys type from a
|
This pull request hacks on supports for union math when using Unions of Literals to index into tuples, NamedTuples, and TypedDicts. It also fixes a bug I apparently introduced. Currently, mypy correctly reports an error with this code: class Test(TypedDict): foo: int t: Test # Error: int can't be assigned a str value t.setdefault("foo", "unrelated value") ...but does not report an error with: key: Literal["foo"] t.setdefault(key, "unrelated value") This diff should make mypy report an error in both cases. Resolves python#6262.
This pull request hacks on supports for union math when using Unions of Literals to index into tuples, NamedTuples, and TypedDicts. It also fixes a bug I apparently introduced. Currently, mypy correctly reports an error with this code: class Test(TypedDict): foo: int t: Test # Error: int can't be assigned a str value t.setdefault("foo", "unrelated value") ...but does not report an error with: key: Literal["foo"] t.setdefault(key, "unrelated value") This diff should make mypy report an error in both cases. Resolves python#6262.
This pull request hacks on supports for union math when using Unions of Literals to index into tuples, NamedTuples, and TypedDicts. It also fixes a bug I apparently introduced. Currently, mypy correctly reports an error with this code: class Test(TypedDict): foo: int t: Test # Error: int can't be assigned a str value t.setdefault("foo", "unrelated value") ...but does not report an error with: key: Literal["foo"] t.setdefault(key, "unrelated value") This diff should make mypy report an error in both cases. Resolves python#6262.
This pull request hacks on supports for union math when using Unions of Literals to index into tuples, NamedTuples, and TypedDicts. It also fixes a bug I apparently introduced. Currently, mypy correctly reports an error with this code: class Test(TypedDict): foo: int t: Test # Error: int can't be assigned a str value t.setdefault("foo", "unrelated value") ...but does not report an error with: key: Literal["foo"] t.setdefault(key, "unrelated value") This diff should make mypy report an error in both cases. Resolves python#6262.
This pull request hacks on supports for union math when using Unions of Literals to index into tuples, NamedTuples, and TypedDicts. It also fixes a bug I apparently introduced. Currently, mypy correctly reports an error with this code: class Test(TypedDict): foo: int t: Test # Error: int can't be assigned a str value t.setdefault("foo", "unrelated value") ...but does not report an error with: key: Literal["foo"] t.setdefault(key, "unrelated value") This diff should make mypy report an error in both cases. Resolves #6262.
I have a similar problem, but I try to use a variable as a key: from mypy_extensions import TypedDict
from typing_extensions import Literal, Final
class MyDict(TypedDict):
field_name: int
d: MyDict
d = {'field_name': 1}
name_in_var = 'field_name'
d = {name_in_var: 1}
name_in_literal2: Literal['field_name'] = 'field_name'
d = {name_in_literal2: 1}
name_in_final: Final = 'field_name'
d = {name_in_final: 1} And then get errors:
@Michael0x2a It is expected behavior or bug linked to this issue? |
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.
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.)
The text was updated successfully, but these errors were encountered: