python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.31k stars 2.8k forks source link

Type annotation for `__await__` method. #17617

Open trivialfis opened 2 months ago

trivialfis commented 2 months ago

Bug Report

With the latest mypy 1.11.0, it reports errors for awaiting an awaitable object. For previous versions, there was no error.

To Reproduce

from typing import Any, Awaitable, Generator

class MyClass:
    def __init__(self) -> None:
        self.foo = "bar"

    def __await__(self) -> Generator["MyClass"]:
        yield self

async def fn() -> None:
    test = await MyClass()

Expected Behavior

No error, or a more concise error that uses the yield type of __await__ instead of __init__ (None).

Actual Behavior

Function does not return a value (it only ever returns None)  [func-returns-value]

Your Environment

sobolevn commented 2 months ago

Shouldn't this be Generator[Any, None, "MyClass"]?

trivialfis commented 1 month ago

Wouldn't it make MyClass the return type instead of the yield type?