python / python/typeshed

Awaitable.__await__ expects a Generator where an Iterator will do

Open
#5,125 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: deferred stubs: false positive
Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

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: https://github.com/python/typeshed/pull/5194#issuecomment-816339179

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Awaitable definition involved in the mypy diagnostic, then compare it with Deferred.iter and await in src/twisted/internet/defer.py around lines 894-920. Done means the declared return types accept this iterator implementation without the reported assignment error, with the relevant type checks passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.