-
Notifications
You must be signed in to change notification settings - Fork 767
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
Assignment to TypedDict field does not handle bidirectional type inference properly #1645
Comments
I think you mean PEP 589 is very clear that a
Passing a dict to the constructor of a TypedDict class doesn't make sense because that's redundant. In your code example, you're allocating a dict object and then attempting to construct a second copy of that same dict. It's the equivalent of Here's how a TypedDict type is meant to be used. x: Test = {"thing": True, "stuff": "test"} |
Sorry, I misread the source for the metaclass. You are correct I wanted Runtime behavior is however as I described (and thus against the PEP, but thats an argument for the core devs). I wanted to use it due to I guess a different edgecase or misunderstanding in behaviour where: from typing import Optional, TypedDict
class Thing(TypedDict):
thing: bool
stuff: str
class Thing2(TypedDict):
other_thing: Optional[Thing]
stuff: str
thing2: Thing2 = {"other_thing": None, "stuff": "a"}
thing2['other_thing'] = {"thing": False, "stuff": "a"}
# ^^^^^^^^^^^^^^^^
# Could not assign item in TypedDict
# Type "dict[str, Unknown]" cannot be assigned to type "Thing | None"
# "dict[str, Unknown]" is incompatible with "Thing"
# Type cannot be assigned to type "None"
# PylancereportTypedDictNotRequiredAccess Thus my first solution would be to wrap it in In the code Im working on I had gone with creating a new var and annotating it appropriately, then using that in the assignment, but its an extra step that feels quite pointless. |
I agree that this code should work. I've fixed the underlying reason for this error message. This will be fixed in the next release of pylance. |
fixed in 2021.8.1 |
Environment data
Expected behaviour
Pylance understands that TypedDict is a wrapper around dict at runtime and
__call__
on any TypedDict is just a call to dict()which will happily accept a dict as its argument.
Actual behaviour
Pylance complains that you cannot pass a dict to SomeTypedDict(), stating that instead one must use key/value args
Logs
Python Language Server Log
Code Snippet / Additional information
The text was updated successfully, but these errors were encountered: