diff --git a/CHANGES/668.misc b/CHANGES/668.misc new file mode 100644 index 00000000..22fc86e9 --- /dev/null +++ b/CHANGES/668.misc @@ -0,0 +1 @@ +Disable ContentType filter by default diff --git a/aiogram/dispatcher/filters/content_types.py b/aiogram/dispatcher/filters/content_types.py index 177ecdae..dc96ca23 100644 --- a/aiogram/dispatcher/filters/content_types.py +++ b/aiogram/dispatcher/filters/content_types.py @@ -11,10 +11,9 @@ from .base import BaseFilter class ContentTypesFilter(BaseFilter): """ Is useful for handling specific types of messages (For example separate text and stickers handlers). - This is always automatically adds to the filters list for message handlers. """ - content_types: Optional[Union[Sequence[str], str]] = None + content_types: Union[Sequence[str], str] """Sequence of allowed content types""" @validator("content_types") @@ -32,7 +31,4 @@ class ContentTypesFilter(BaseFilter): return value async def __call__(self, message: Message) -> Union[bool, Dict[str, Any]]: - if not self.content_types: # pragma: no cover - # Is impossible but needed for valid typechecking - self.content_types = [ContentType.TEXT] return ContentType.ANY in self.content_types or message.content_type in self.content_types diff --git a/docs/dispatcher/filters/content_types.rst b/docs/dispatcher/filters/content_types.rst index 639f2996..c28e6b8a 100644 --- a/docs/dispatcher/filters/content_types.rst +++ b/docs/dispatcher/filters/content_types.rst @@ -15,11 +15,6 @@ Can be imported: Or used from filters factory by passing corresponding arguments to handler registration line -.. warning:: - **Please be patient!** - - If no one content type filter is specified the :code:`["text"]` value is automatically will be used. - Usage ===== diff --git a/docs/dispatcher/filters/index.rst b/docs/dispatcher/filters/index.rst index 010cb0c5..1b970d86 100644 --- a/docs/dispatcher/filters/index.rst +++ b/docs/dispatcher/filters/index.rst @@ -37,6 +37,8 @@ Filters can be: - Subclass of :ref:`BaseFilter ` +- Instances of :ref:`MagicFilter ` + Filters should return bool or dict. If the dictionary is passed as result of filter - resulted data will be propagated to the next filters and handler as keywords arguments. @@ -70,4 +72,7 @@ For example if you need to make simple text filter: .. note:: - Bound filters is always recursive propagates to the nested routers. + Bound filters is always recursive propagates to the nested routers but will be available + in nested routers only after attaching routers so that's mean you will need to + include routers before registering handlers. + diff --git a/docs/dispatcher/router.rst b/docs/dispatcher/router.rst index 3e1a1b69..ea13eea7 100644 --- a/docs/dispatcher/router.rst +++ b/docs/dispatcher/router.rst @@ -35,6 +35,19 @@ Update Message ------- + +.. attention:: + + Be attentive with filtering this event + + You should expect than this event can be with different set's of attributes in different cases + + (For example text, sticker and document is always is different content types of message) + + Recommended way to check field availability before usage or use + :class:`aiogram.dispatcher.filters.content_types.ContentTypesFilter` + + .. code-block:: python @router.message() diff --git a/tests/test_dispatcher/test_filters/test_content_types.py b/tests/test_dispatcher/test_filters/test_content_types.py index 8d1706b2..046073c4 100644 --- a/tests/test_dispatcher/test_filters/test_content_types.py +++ b/tests/test_dispatcher/test_filters/test_content_types.py @@ -16,12 +16,6 @@ class MinimalMessage: class TestContentTypesFilter: - async def test_validator_empty(self): - filter_ = ContentTypesFilter() - assert not filter_.content_types - await filter_(cast(Message, MinimalMessage(ContentType.TEXT))) - assert filter_.content_types == [ContentType.TEXT] - def test_validator_empty_list(self): filter_ = ContentTypesFilter(content_types=[]) assert filter_.content_types == [] @@ -46,7 +40,6 @@ class TestContentTypesFilter: @pytest.mark.parametrize( "values,content_type,result", [ - [[], ContentType.TEXT, True], [[ContentType.TEXT], ContentType.TEXT, True], [[ContentType.PHOTO], ContentType.TEXT, False], [[ContentType.ANY], ContentType.TEXT, True],