-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Support inferring dict literals to be a union of TypedDicts #13274
Labels
Comments
I have encountered this error again with a slightly different code sample: from __future__ import annotations
from typing_extensions import TypedDict
class Foo(TypedDict, total=False):
foo: str
class Bar(TypedDict, total=False):
bar: str
def foo(body: Foo | Bar = {}) -> None:
... This is a completely valid expression as both
|
Possibly related: #8533 |
I'm also getting this (python3.6, mypy=0.971)
|
This was referenced Jan 20, 2023
JukkaL
pushed a commit
that referenced
this issue
Jan 23, 2023
Fixes #14481 (regression) Fixes #13274 Fixes #8533 Most notably, if literal matches multiple items in union, it is not an error, it is only an error if it matches none of them, so I adjust the error message accordingly. An import caveat is that an unrelated error like `{"key": 42 + "no"}` can cause no item to match (an hence an extra error), but I think it is fine, since we still show the actual error, and avoiding this would require some dirty hacks. Also note there was an (obvious) bug in one of the fixtures, that caused one of repros not repro in tests, fixing it required tweaking an unrelated test.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feature
Pitch
While the code example provided is nonsensical, I ran into this error using real world types. In the more abstract sense, inferring the
{"name": "foo"}
expression to beA | B
should be supported as it is type safe. On top of that, Pyright also supports this pattern and infers the expression correctly.There is also a "bug" with the check in that it doesn't respect nested types, consider this case:
In this case, there is no ambiguity, the expression can be safely inferred to be of type
A
.The text was updated successfully, but these errors were encountered: