hikari-py/hikari

GatewayBot.stream produces typing errors

Closed this issue · 6 comments

Steps to reproduce

  1. Create a GatewayBot
  2. Open a stream (I have only tested with InteractionCreateEvent's
  3. With or without filter, they both give typing errors.
  4. Deprecated async or sync both produce these errors.
with bot.stream(hikari.InteractionCreateEvent, 120).filter(
        lambda e: (
            isinstance(e.interaction, hikari.ComponentInteraction)
            and e.interaction.user == author
            and e.interaction.message == message
        )
    ) as stream:
        for event in stream:
            ...

Expected result

Everything works as expected, without typing errors. This is the exact syntax used in the hikari examples.

Actual result

Everything works as expected except for when using filter this error is displayed:

Object of type "LazyIterator[InteractionCreateEvent]" cannot be used with "with" because it does not implement __enter__

and when not using filter:

"EventStream[InteractionCreateEvent]" is not iterable "__next__" method not defined on type "LazyIterator[InteractionCreateEvent]"

System info

hikari (2.0.0.dev104) [79548984]
located at /home/projects/hikari-lightbulb/.venv/lib/python3.10/site-packages/hikari
CPython 3.10.0 GCC 11.1.0
Linux love 5.10.79-1-lts #1 SMP Fri, 12 Nov 2021 19:04:00 +0000 x86_64

Further info

We discussed this briefly, but I wanted to get it onto the issue tracker as it's quite annoying.

Checklist

  • I have made sure to remove ANY sensitive information (bot token, passwords, credentials, personal details, etc.).
  • I have searched the issue tracker and have made sure it's not a duplicate. If it is a follow up of another issue, I have specified it.

Should this only be async, and thus only have an __aenter__? Or by what you said dyou mean it happens with both?

Should this only be async, and thus only have an __aenter__? Or by what you said dyou mean it happens with both?

It's actually deprecated, see this fab6c6c

I believe the given code snippet is still incorrect though as it needs to be

async for event in stream:
    ...

Not

for event in stream:
    ...

Correct me if I'm wrong though

You are correct about that, it should be async for event in stream. That does not resolve the issue though.

Relevant pr opened that fixes this is #917

I wanted to try out some things before it got merged to try and improve the interface overall. Hopefully I will get to it in one of the upcoming days

Closed by #917