Repository: aiogram/aiogram
Pull request #1114 (2023-02-05 14:07:40)

Replaced error `TypeError: TelegramEventObserver.__call__() got an unexpected keyword argument '<name>'` with a more understandable
# Description

The most common question from the newbies who migrating from 2.x version to 3.x that is `TypeError: TelegramEventObserver.__call__() got an unexpected keyword argument '<name>'`.

This error caused in due to keyword-filters is no more supported (#942).

This pull-request adds an customized error with explanation why you can't use keyword filters with link to the documentation.

# Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have added tests that prove my fix is effective or that my feature works as expected
- [x] New and existing unit tests pass locally with my changes
- [x] My changes generate no new warnings or errors
- [x] My changes are compatible with minimum requirements of the project
- [x] I have made corresponding changes to the documentation


github-actions[bot]: # :heavy_check_mark: Changelog found.

Thank you for adding a description of the changes
codecov[bot]: # [Codecov](https://codecov.io/gh/aiogram/aiogram/pull/1114?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) Report
> Merging [#1114](https://codecov.io/gh/aiogram/aiogram/pull/1114?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) (ab16790) into [dev-3.x](https://codecov.io/gh/aiogram/aiogram/commit/d07e8fc4071c5cd5a4c4ee5fe7e4b2002fe4cb7a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) (d07e8fc) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/aiogram/aiogram/pull/1114/graphs/tree.svg?width=650&height=150&src=pr&token=cFqEr3p9MX&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram)](https://codecov.io/gh/aiogram/aiogram/pull/1114?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram)

```diff
@@            Coverage Diff            @@
##           dev-3.x     #1114   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          340       340           
  Lines         8823      8829    +6     
=========================================
+ Hits          8823      8829    +6     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `100.00% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/aiogram/aiogram/pull/1114?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) | Coverage Δ | |
|---|---|---|
| [aiogram/dispatcher/event/telegram.py](https://codecov.io/gh/aiogram/aiogram/pull/1114?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram#diff-YWlvZ3JhbS9kaXNwYXRjaGVyL2V2ZW50L3RlbGVncmFtLnB5) | `100.00% <100.00%> (ø)` | |
| [aiogram/exceptions.py](https://codecov.io/gh/aiogram/aiogram/pull/1114?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram#diff-YWlvZ3JhbS9leGNlcHRpb25zLnB5) | `100.00% <100.00%> (ø)` | |


Patch:
diff --git a/CHANGES/1114.misc.rst b/CHANGES/1114.misc.rst
new file mode 100644
index 00000000..ba1661ae
--- /dev/null
+++ b/CHANGES/1114.misc.rst
@@ -0,0 +1,2 @@
+Replaced error :code:`TypeError: TelegramEventObserver.__call__() got an unexpected keyword argument '<name>'`
+with a more understandable one for developers and with a link to the documentation.
diff --git a/aiogram/dispatcher/event/telegram.py b/aiogram/dispatcher/event/telegram.py
index 8dd56a71..8005c418 100644
--- a/aiogram/dispatcher/event/telegram.py
+++ b/aiogram/dispatcher/event/telegram.py
@@ -3,11 +3,11 @@ from __future__ import annotations
 from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
 
 from aiogram.dispatcher.middlewares.manager import MiddlewareManager
-
-from ...filters.base import Filter
-from ...types import TelegramObject
 from .bases import REJECTED, UNHANDLED, MiddlewareType, SkipHandler
 from .handler import CallbackType, FilterObject, HandlerObject
+from ...exceptions import UnsupportedKeywordArgument
+from ...filters.base import Filter
+from ...types import TelegramObject
 
 if TYPE_CHECKING:
     from aiogram.dispatcher.router import Router
@@ -58,10 +58,21 @@ class TelegramEventObserver:
         callback: CallbackType,
         *filters: CallbackType,
         flags: Optional[Dict[str, Any]] = None,
+        **kwargs: Any,
     ) -> CallbackType:
         """
         Register event handler
         """
+        if kwargs:
+            raise UnsupportedKeywordArgument(
+                "Passing any additional keyword arguments to the registrar method "
+                "is not supported.\n"
+                "This error may be caused when you are trying to register filters like in 2.x "
+                "version of this framework, if it's true just look at correspoding "
+                "documentation pages.\n"
+                f"Please remove the {set(kwargs.keys())} arguments from this call.\n"
+            )
+
         if flags is None:
             flags = {}
 
@@ -118,13 +129,14 @@ class TelegramEventObserver:
         self,
         *filters: CallbackType,
         flags: Optional[Dict[str, Any]] = None,
+        **kwargs: Any,
     ) -> Callable[[CallbackType], CallbackType]:
         """
         Decorator for registering event handlers
         """
 
         def wrapper(callback: CallbackType) -> CallbackType:
-            self.register(callback, *filters, flags=flags)
+            self.register(callback, *filters, flags=flags, **kwargs)
             return callback
 
         return wrapper
diff --git a/aiogram/exceptions.py b/aiogram/exceptions.py
index 96d0c0db..dcf433c6 100644
--- a/aiogram/exceptions.py
+++ b/aiogram/exceptions.py
@@ -2,6 +2,7 @@ from typing import Any, Optional
 
 from aiogram.methods import TelegramMethod
 from aiogram.methods.base import TelegramType
+from aiogram.utils.link import docs_url
 
 
 class AiogramError(Exception):
@@ -28,6 +29,10 @@ class CallbackAnswerException(AiogramError):
     pass
 
 
+class UnsupportedKeywordArgument(DetailedAiogramError):
+    url = docs_url("migration_2_to_3.html", fragment_="filtering-events")
+
+
 class TelegramAPIError(DetailedAiogramError):
     def __init__(
         self,


Test patch:
diff --git a/tests/test_dispatcher/test_event/test_telegram.py b/tests/test_dispatcher/test_event/test_telegram.py
index 37f66357..1f892de0 100644
--- a/tests/test_dispatcher/test_event/test_telegram.py
+++ b/tests/test_dispatcher/test_event/test_telegram.py
@@ -9,9 +9,11 @@ from aiogram.dispatcher.event.bases import REJECTED, SkipHandler
 from aiogram.dispatcher.event.handler import HandlerObject
 from aiogram.dispatcher.event.telegram import TelegramEventObserver
 from aiogram.dispatcher.router import Router
+from aiogram.exceptions import UnsupportedKeywordArgument
 from aiogram.filters import Filter
 from aiogram.types import Chat, Message, User
 
+
 # TODO: Test middlewares in routers tree
 
 
@@ -82,6 +84,11 @@ class TestTelegramEventObserver:
         assert f2 in callbacks
         assert MyFilter1(test="PASS") in callbacks
 
+    def test_keyword_filters_is_not_supported(self):
+        router = Router()
+        with pytest.raises(UnsupportedKeywordArgument):
+            router.message.register(lambda e: True, commands=["test"])
+
     def test_register_decorator(self):
         router = Router()
         observer = router.message