Skip to content

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

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

Closed
oremanj opened this issue Aug 21, 2020 · 2 comments
Closed
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@oremanj
Copy link
Contributor

oremanj commented Aug 21, 2020

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.

@ilevkivskyi
Copy link
Member

It's hard to tell what is going on here, but it does look like a bug.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Sep 8, 2020
@hauntsaninja
Copy link
Collaborator

Looks like this got fixed in #12631

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests

3 participants