Skip to content

Incorrect type inference in yoda-style comparison leads to spurious error #9333

Closed
@oremanj

Description

@oremanj

When I type-check the following:

from typing import Awaitable, Generic, TypeVar

T = TypeVar("T")

class Foo(Generic[T]):
    contents: T

def get_from(foo: Foo[T]) -> Awaitable[T]:
    async def get_it() -> T:
        return foo.contents
    return get_it()

async def test() -> None:
    foo = Foo[int]()
    foo.contents = 42
    assert 42 == await get_from(foo)

the final line yields a mypy error error: Argument 1 to "get_from" has incompatible type "Foo[int]"; expected "Foo[object]"

I can reproduce this on a variety of mypy and Python versions on mypy-play.net; I couldn't find any that didn't exhibit this behavior. But if I reverse the comparison order (assert await get_from(foo) == 42), or make get_from() return T instead of Awaitable[T], the bug goes away.

I can still reproduce this bug if I replace get_from() with

async def get_from(foo: Foo[T]) -> T:
    return foo.contents

but without the async and await it goes away. I doubt it actually has anything to do with async; I would imagine it just needs a sufficiently complex return type after desugaring.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions