google / pytype

A static type analyzer for Python code
https://google.github.io/pytype
Other
4.76k stars 277 forks source link

merge-pyi: wrong inferred return type for coroutine #546

Open Arvind2222 opened 4 years ago

Arvind2222 commented 4 years ago

Essentially If I specify the return type, There is something which seems faulty.

from typing import Optional

class Response:
    def __init__(self, data: str) -> None:
        self.resp_value = data
        self.some_value = dict(data)

class Request:
    def __init__(self, sem):
        self.sem = sem

    async def fetch_some_data(self) -> Optional[Response]:
        if self.sem:
            return Response("someData")
        return None

async def make_get_request(sem="Some arg"):
    data = await Request(sem).fetch_some_data()
    return data

The pyi generate with the pytype is

# (generated with --quick)

from typing import Any, Coroutine, Dict, Optional, Union

class Request:
    sem: Any
    def __init__(self, sem) -> None: ...
    def fetch_some_data(self) -> Coroutine[Any, Any, Optional[Response]]: ...

class Response:
    resp_value: str
    some_value: Dict[Union[int, slice], str]
    def __init__(self, data: str) -> None: ...

def make_get_request(sem = ...) -> **coroutine**: ...

class Response: def init(self, data: str) -> None: self.resp_value = data self.some_value = dict(data)

class Request: def init(self, sem) -> None: self.sem = sem

async def fetch_some_data(self) -> Optional[Response]:
    if self.sem:
        return Response("someData")
    return None

async def make_get_request(sem="Some arg") -> coroutine: data = await Request(sem).fetch_some_data() return data

rchen152 commented 4 years ago

Thanks for the report! I wonder if we're simplifying Coroutine[Any, Any, Any] to coroutine (which would be fine for pyi files for pytype's own consumption but wrong for merge-pyi).

Arvind2222 commented 4 years ago

Exactly!

Arvind2222 commented 4 years ago

previously I submitted a PR for pyi but didn't check for merge-pyi. But it went completely in the wrong direction though