Skip to content

Cannot resolve TypeDict into Union *only* when the Union contains an Awaitable #11324

@iwahbe

Description

@iwahbe

Describe the bug

I'm seeing pyright fail to type a TypedDict only when it's resolved into a union that includes a third type (in my example, Awaitable[T]). Here is my repro:

from typing import Awaitable, Callable, Generic, TypedDict, TypeVar, Union

T = TypeVar("T")
U = TypeVar("U")

Input = Union[T, Awaitable[T], "Output[T]"]


class Output(Generic[T]):
    def apply(self, func: Callable[[T], Input[U]]) -> "Output[U]": ...


class D(TypedDict):
    key: str


def accept(x: Input[list[D]]) -> None: ...


# OK: dict literal directly in call has downward type context.
accept([{"key": "v"}])

# ERROR: dict literal inside apply lambda is inferred as dict[str, str], not D.
accept(Output[list[D]]().apply(lambda _: [{"key": "v"}]))

The confusing this is that this makes it type-check without errors:

-Input = Union[T, Awaitable[T], "Output[T]"]
+Input = Union[T, "Output[T]"]
Full non-erroring code
from typing import Awaitable, Callable, Generic, TypedDict, TypeVar, Union

T = TypeVar("T")
U = TypeVar("U")

Input = Union[T, "Output[T]"]


class Output(Generic[T]):
    def apply(self, func: Callable[[T], Input[U]]) -> "Output[U]": ...


class D(TypedDict):
    key: str


def accept(x: Input[list[D]]) -> None: ...


# OK: dict literal directly in call has downward type context.
accept([{"key": "v"}])

# ERROR: dict literal inside apply lambda is inferred as dict[str, str], not D.
accept(Output[list[D]]().apply(lambda _: [{"key": "v"}]))

This was observed running the pyright CLI at version 1.1.408.


Notes:

mypy --strict (at version 0.931) type-checks both programs without issue.

#10908 implies that these programs should never type-check, but that obviously the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions