reagento / dishka

Cute DI framework with agreeable API and everything you need
https://dishka.readthedocs.io
Apache License 2.0
423 stars 47 forks source link

Can't get container in aiogram handler when auto_inject is enabled #214

Closed Vladyslav49 closed 2 months ago

Vladyslav49 commented 3 months ago

Handler:

@dp.message(CommandStart())
async def on_start(message: Message, dishka_container: AsyncContainer) -> None: ...

Traceback:

Traceback (most recent call last):
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 309, in _process_update
    response = await self.feed_update(bot, update, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 158, in feed_update
    response = await self.update.wrap_outer_middleware(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/middlewares/error.py", line 25, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/middlewares/user_context.py", line 49, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/fsm/middleware.py", line 42, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/aiogram.py", line 46, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/event/telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/event/handler.py", line 43, in call
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 276, in _listen_update
    return await self.propagate_event(update_type=update_type, event=event, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 142, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/aiogram.py", line 46, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 137, in _wrapped
    return await self._propagate_event(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 162, in _propagate_event
    response = await observer.trigger(event, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/event/telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/aiogram.py", line 58, in __call__
    callback=inject(old_handler.callback),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/aiogram.py", line 28, in inject
    return wrap_injection(
           ^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/base.py", line 129, in wrap_injection
    autoinjected_func.__signature__ = Signature(
                                      ^^^^^^^^^^
  File "/usr/lib/python3.12/inspect.py", line 3068, in __init__
    raise ValueError(msg)
ValueError: duplicate parameter name: 'dishka_container'
Forceres commented 3 months ago

Handler:

@dp.message(CommandStart())
async def on_start(message: Message, dishka_container: AsyncContainer) -> None: ...

Traceback:

Traceback (most recent call last):
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 309, in _process_update
    response = await self.feed_update(bot, update, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 158, in feed_update
    response = await self.update.wrap_outer_middleware(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/middlewares/error.py", line 25, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/middlewares/user_context.py", line 49, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/fsm/middleware.py", line 42, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/aiogram.py", line 46, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/event/telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/event/handler.py", line 43, in call
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/dispatcher.py", line 276, in _listen_update
    return await self.propagate_event(update_type=update_type, event=event, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 142, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/aiogram.py", line 46, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 137, in _wrapped
    return await self._propagate_event(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/router.py", line 162, in _propagate_event
    response = await observer.trigger(event, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/aiogram/dispatcher/event/telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/aiogram.py", line 58, in __call__
    callback=inject(old_handler.callback),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/aiogram.py", line 28, in inject
    return wrap_injection(
           ^^^^^^^^^^^^^^^
  File "/home/vlad/PycharmProjects/pythonProject/.venv/lib/python3.12/site-packages/dishka/integrations/base.py", line 129, in wrap_injection
    autoinjected_func.__signature__ = Signature(
                                      ^^^^^^^^^^
  File "/usr/lib/python3.12/inspect.py", line 3068, in __init__
    raise ValueError(msg)
ValueError: duplicate parameter name: 'dishka_container'

Looks like you shouldn't try to pass dishka_container itself as a func argument, but you can pass more exact dependency:

async def on_start(
  message: Message,
  settings: Annotated[Settings, FromDishka()], // example
) -> None:
  pass