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

Can't use message.answer within asyncio.gather #1467

Open
2 tasks done
leshchenko1979 opened this issue Apr 21, 2024 · 3 comments
Open
2 tasks done

Can't use message.answer within asyncio.gather #1467

leshchenko1979 opened this issue Apr 21, 2024 · 3 comments
Labels
bug Something is wrong with the framework

Comments

@leshchenko1979
Copy link
Contributor

Checklist

  • I am sure the error is coming from aiogram code
  • I have searched in the issue tracker for similar bug reports, including closed ones

Operating system

Latest Ubuntu

Python version

3.12.3

aiogram version

latest

Expected behavior

await asyncio.gather(message.answer("Hi"), state.clear())

Current behavior

  File "/usr/local/lib/python3.12/asyncio/tasks.py", line 830, in gather
    if arg not in arg_to_fut:
       ^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'SendMessage'

Steps to reproduce

await asyncio.gather(message.answer("Hi"), state.clear())

Code example

await asyncio.gather(message.answer("Hi"), state.clear())

Logs

No response

Additional information

No response

@leshchenko1979 leshchenko1979 added the bug Something is wrong with the framework label Apr 21, 2024
@Olegt0rr
Copy link
Contributor

Olegt0rr commented Apr 21, 2024

Workaround

tasks = [
    asyncio.create_task(message.answer("Hi")),
    asyncio.create_task(state.clear()),
]
await asyncio.gather(*tasks)

@leshchenko1979
Copy link
Contributor Author

leshchenko1979 commented Apr 24, 2024

I get the same error message when running:

async with asyncio.TaskGroup() as tg:
    tg.create_task(message.answer("Hi!"))

Although the below works fine:

await asyncio.create_task(message.answer("Hi!"))

@srbssv
Copy link

srbssv commented May 15, 2024

Doesn't work for me:

await asyncio.create_task(message.answer("Hi!"))

The error:

TypeError: a coroutine was expected, got SendMessage

The above mentioned workaround with asyncio.gather(*tasks) didn't work as well.

  • Aiogram version: 3.6.0
  • Python version: 3.12.1

UPD:
However, if I wrap message.answer() with coroutine function, the message sending works fine:

async def send_message(text: str, message: Message):
    await message.answer(text)

...
await asyncio.create_task(send_message("hi", message))

UPD 2:
Another workaround:

await asyncio.create_task(message.answer("Hi!").emit(message.bot))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is wrong with the framework
Projects
None yet
Development

No branches or pull requests

3 participants