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

Support inferring dict literals to be a union of TypedDicts #13274

Closed
RobertCraigie opened this issue Jul 28, 2022 · 3 comments · Fixed by #14505
Closed

Support inferring dict literals to be a union of TypedDicts #13274

RobertCraigie opened this issue Jul 28, 2022 · 3 comments · Fixed by #14505

Comments

@RobertCraigie
Copy link

Feature

from __future__ import annotations
from typing import TypedDict

class A(TypedDict, total=False):
    name: str

class B(TypedDict, total=False):
    name: str

def foo(data: A | B) -> None:
    ...

# this should not report an error
foo({"name": "Robert"})  # error: Type of TypedDict is ambiguous, could be any of ("A", "B")

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 be A | 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:

from __future__ import annotations
from typing import TypedDict

class A(TypedDict, total=False):
    foo: C

class B(TypedDict, total=False):
    foo: D

class C(TypedDict, total=False):
    c: str

class D(TypedDict, total=False):
    d: str

def foo(data: A | B) -> None:
    ...

foo({"foo": {"c": "foo"}})  # error: Type of TypedDict is ambiguous, could be any of ("A", "B")

In this case, there is no ambiguity, the expression can be safely inferred to be of type A.

@RobertCraigie
Copy link
Author

RobertCraigie commented Oct 5, 2022

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 Foo and Bar do not have any required fields. Mypy reports these errors:

tmp.py:10: error: Type of TypedDict is ambiguous, could be any of ("Foo", "Bar")
tmp.py:10: error: Incompatible default for argument "body" (default has type "Dict[<nothing>, <nothing>]", argument has type "Union[Foo, Bar]")

@rattrayalex
Copy link

Possibly related: #8533

@jepperaskdk
Copy link

I'm also getting this (python3.6, mypy=0.971)

from typing import Union
from typing_extensions import Literal, TypedDict


class A(TypedDict):
    type: Literal['a']
    value: bool


class B(TypedDict):
    type: Literal['b']
    value: str


Response = Union[A, B]


def method(message: Response) -> None:
    ...


method({'type': 'a', 'value': True})
# error: Type of TypedDict is ambiguous, could be any of ("A", "B")
# error: Argument 1 to "method" has incompatible type "Dict[str, object]"; expected "Union[A, B]"


method({'type': 'b', 'value': "abc"})
# error: Type of TypedDict is ambiguous, could be any of ("A", "B")
# error: Argument 1 to "method" has incompatible type "Dict[str, str]"; expected "Union[A, B]"

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants