Skip to content

fix: accept handlers with **kwargs in inject - #47

Merged
lesnik512 merged 1 commit into
mainfrom
fix/inject-var-keyword
Oct 17, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix/inject-var-keyword

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #45

inject appended its keyword-only modern_di_container parameter to the end of the wrapper signature. For a handler ending in **kwargs, that put a keyword-only parameter after the variadic keyword one, and inspect.Signature raised ValueError at decoration time.

Changes:

  • The added parameter now goes before a trailing VAR_KEYWORD parameter. Signatures without **kwargs come out exactly as before.
  • The wrapper drops FromDI parameter names from the incoming kwargs before calling the handler. aiogram passes the whole data dict to any callback whose signature has **kwargs, so a FromDI parameter named like a data key (for example bot) would otherwise fail with "got multiple values for keyword argument". The resolved dependency takes that name, which matches what already happens without **kwargs, where aiogram filters data by the visible signature and never sends those names. The audit repro for this issue used exactly that case, so fixing only the signature would have traded the decoration error for a dispatch error.
  • The container entry is still removed from kwargs when inject added the parameter, so it does not leak into the handler's **kwargs. A handler that declares modern_di_container itself still receives it.

Tests (each failed before the fix):

  • A handler with a FromDI parameter and **kwargs gets its dependency through a real dispatch, and kwargs holds aiogram's bot but not modern_di_container.
  • The wrapper signature keeps **kwargs last, and a handler without **kwargs ends with the keyword-only container parameter as before.
  • A FromDI parameter named bot next to **kwargs resolves from the container, and other aiogram data still reaches kwargs.

@lesnik512
lesnik512 merged commit 63b9be5 into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the fix/inject-var-keyword branch October 17, 2026 10:25
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 this pull request may close these issues.

@inject on a handler with **kwargs and a FromDI parameter raises ValueError at decoration

1 participant