Skip to content

Commit

Permalink
Add Async classes to typing stub.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Williams committed Dec 1, 2015
1 parent 30af935 commit 86893da
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion stdlib/3/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,27 @@ class Hashable(metaclass=ABCMeta):
@abstractmethod
def __hash__(self) -> int: ...

class Awaitable(Generic[_T_co]):
@abstractmethod
def __await__(self) -> Iterator[_T_co]:...

This comment has been minimized.

Copy link
@vlasovskikh

vlasovskikh Dec 1, 2015

Awaitable should be parameterized with three generic types and return a Generator:

class Awaitable(Generic[T_co, T_contra, V_co]):
    def __await__(self) -> Generator[T_co, T_contra, V_co]: ...

Otherwise it won't be possible to describe the return type of an async def function that returns a value after await for a coroutine object finishes, e. g.:

from asyncio import Future, sleep
from typing import Awaitable


async def f(x: int) -> Awaitable[Future, Any, int]:
    await asyncio.sleep(x)
    return x

async def g():
    x = await f(1)  # What's the type of `x`? We need int to be a parameter of the `Awaitable`

(There is a proposal python/typing#119 to allow users to specify only the type of the return statement for async def functions so this complicated type would be used only inside a static type checker.)


class AsyncIterable(Generic[_T_co]):
@abstractmethod
def __anext__(self) -> Awaitable[_T_co]:...

class AsyncIterator(AsyncIterable[_T_co], Generic[_T_co]):
@abstractmethod
def __anext__(self) -> Awaitable[_T_co]:...
def __aiter__(self) -> 'AsyncIterator'[_T_co]:...

This comment has been minimized.

Copy link
@JukkaL

JukkaL Dec 1, 2015

Quotes should include the part in [...].


class Iterable(Generic[_T_co]):
@abstractmethod
def __iter__(self) -> Iterator[_T_co]: ...

class Iterator(Iterable[_T_co], Generic[_T_co]):
@abstractmethod
def __next__(self) -> _T_co: ...
def __iter__(self) -> Iterator[_T_co]: ...
def __iter__(self) -> 'Iterator'[_T_co]: ...

This comment has been minimized.

Copy link
@JukkaL

JukkaL Dec 1, 2015

This is a similar quoting issue.


class Container(Generic[_T_co]):
@abstractmethod
Expand Down

0 comments on commit 86893da

Please sign in to comment.