mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-06 07:50:32 +00:00
Disabled ContentTypesFilter by default (#668)
* Disabled ContentTypesFilter by default * Rename file * Update docs
This commit is contained in:
parent
5851e32266
commit
18a93aab60
6 changed files with 21 additions and 18 deletions
1
CHANGES/668.misc
Normal file
1
CHANGES/668.misc
Normal file
|
|
@ -0,0 +1 @@
|
|||
Disable ContentType filter by default
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
=====
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue