Skip to content

Awaitable.__await__ expects a Generator where an Iterator will do #5125

Open
@wsanchez

Description

@wsanchez

In Twisted, the Deferred class has set __await__ = __iter__, where __iter__ is typed thusly:

    def __iter__(self) -> "Deferred[_DeferredResultT]":

Deferred, insofar as it has an __iter__ method, is thus itself an Iterable. It also implements __next__, so is an Iterator.

Mypy is yielding this error:

src/twisted/internet/defer.py:919:17: error: Incompatible types in assignment (expression has type "Callable[[], Deferred[_DeferredResultT]]", base class "Awaitable" defined the type as
"Callable[[], Generator[Any, None, _DeferredResultT]]")  [assignment]

I suspect that perhaps Awaitable.__await__ could instead be more flexibly typed to return a Iterator[_T] instead of a Generator[Any, None, _T]… as that (a) seemingly works in this case and (b) the documentation for object.__await__ and PEP 492 say that it must return an iterator and not specifically a generator.


Deferred: mypy problem: #5194 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: deferredIssue or PR deferred until some precondition is fixedstubs: false positiveType checkers report false errors

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions