Skip to content

Commit

Permalink
Fix not all args are passed to handler function invocation (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgfilim1 committed Jul 18, 2021
1 parent 125fc22 commit 4599913
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion aiogram/dispatcher/event/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def _prepare_kwargs(self, kwargs: Dict[str, Any]) -> Dict[str, Any]:
if self.spec.varkw:
return kwargs

return {k: v for k, v in kwargs.items() if k in self.spec.args}
return {
k: v for k, v in kwargs.items() if k in self.spec.args or k in self.spec.kwonlyargs
}

async def call(self, *args: Any, **kwargs: Any) -> Any:
wrapped = partial(self.callback, *args, **self._prepare_kwargs(kwargs))
Expand Down
14 changes: 14 additions & 0 deletions tests/test_dispatcher/test_event/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ async def callback3(foo: int, **kwargs):
return locals()


async def callback4(foo: int, *, bar: int, baz: int):
return locals()


class Filter(BaseFilter):
async def __call__(self, foo: int, bar: int, baz: int) -> Union[bool, Dict[str, Any]]:
return locals()
Expand Down Expand Up @@ -95,11 +99,21 @@ def callback2(foo, bar, baz):
{"foo": 42, "spam": True, "baz": "fuz", "bar": "test"},
{"foo": 42, "baz": "fuz", "bar": "test"},
),
pytest.param(
functools.partial(callback2, bar="test"),
{"foo": 42, "spam": True, "baz": "fuz"},
{"foo": 42, "baz": "fuz"},
),
pytest.param(
callback3,
{"foo": 42, "spam": True, "baz": "fuz", "bar": "test"},
{"foo": 42, "spam": True, "baz": "fuz", "bar": "test"},
),
pytest.param(
callback4,
{"foo": 42, "spam": True, "baz": "fuz", "bar": "test"},
{"foo": 42, "baz": "fuz", "bar": "test"},
),
pytest.param(
Filter(), {"foo": 42, "spam": True, "baz": "fuz"}, {"foo": 42, "baz": "fuz"}
),
Expand Down

0 comments on commit 4599913

Please sign in to comment.