Closed kossyrev-bg closed 1 month ago
Ah maybe this isn't a bug, but rather a limitation of TypedDict in general: https://peps.python.org/pep-0589/#use-of-final-values-and-literal-types
If that's the case, then I apologize and you can close out this issue
This behavior is correct. The TypedDict
mechanism is created for static typing, and when you define a TypedDict
, the keys need to be statically defined as string literals. You can't use dynamic expressions in a TypedDict. The key names must be string literals, like this:
MyTest = TypedDict("MyTest", {"one": str, "two": int})
Not surprisingly, mypy generates the same error in this case.
I was going to point you to the original PEP 589, but you already found it. :) Incidentally, the PEP has been superseded by the Python typing spec, which incorporates and builds upon all of the typing-related PEPs.
Environment data
Code Snippet
Expected behavior
No red squigglies.
Actual behavior
Red squigglies:
Note that code runs just fine when I copy-paste the above into a python terminal.
Logs