Skip to content

False positive: redis.async.PubSub.listen() #8960

Description

@peterschutt

Stub for redis.async.PubSub.listen():

async def listen(self) -> AsyncIterator[Any]: ...

Actual Implementation:

    async def listen(self) -> AsyncIterator:
        """Listen for messages on channels this client has been subscribed to"""
        while self.subscribed:
            response = await self.handle_message(await self.parse_response(block=True))
            if response is not None:
                yield response

Even though signatures are equivalent, mypy interprets the stub to be a coroutine that returns an AsyncIterator.

For example, this is runtime valid, but mypy finds an error:

async for message in pubsub.listen():
    ...

error: "Coroutine[Any, Any, AsyncIterator[Any]]" has no attribute "__aiter__" (not async iterable) [attr-defined]

This is invalid code, but passes mypy:

async for message in await pubsub.listen():
    ...

TypeError: object async_generator can't be used in 'await' expression

References:

Activity

  1. added a commit that references this issue on Oct 21, 2022
    d876af9
  2. added a commit that references this issue on Oct 21, 2022
    25810e1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions