Disabled ContentTypesFilter by default (#668)

* Disabled ContentTypesFilter by default

* Rename file

* Update docs
This commit is contained in:
Alex Root Junior 2021-08-20 02:39:03 +03:00 committed by GitHub
parent 5851e32266
commit 18a93aab60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 18 deletions

1
CHANGES/668.misc Normal file
View file

@ -0,0 +1 @@
Disable ContentType filter by default

View file

@ -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

View file

@ -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
=====

View file

@ -37,6 +37,8 @@ Filters can be:
- Subclass of :ref:`BaseFilter <filters-base>`
- Instances of :ref:`MagicFilter <magic-filters>`
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.

View file

@ -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()

View file

@ -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],