Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
peterschutt opened this issue Oct 21, 2022 · 0 comments · Fixed by #8961
Closed

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

peterschutt opened this issue Oct 21, 2022 · 0 comments · Fixed by #8961

Comments

@peterschutt
Copy link
Contributor

peterschutt commented Oct 21, 2022

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:

peterschutt added a commit to peterschutt/typeshed that referenced this issue Oct 21, 2022
Without the `yield` statement in the method body, these are interpreted by mypy as coroutines that return an `AsyncIterator`.

Closes python#8960
JelleZijlstra pushed a commit that referenced this issue Oct 21, 2022
…stubs. (#8961)

Without the `yield` statement in the method body, these are interpreted by mypy as coroutines that return an `AsyncIterator`.

Closes #8960
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant