aiogram/aiogram/filters/state.py
Alex Root Junior f4251382e8
Remove filters factory, introduce docs translation (#978)
* Rewrite filters

* Update README.rst

* Fixed tests

* Small optimization of the Text filter (TY to @bomzheg)

* Remove dataclass slots argument in due to the only Python 3.10 has an slots argument

* Fixed mypy

* Update tests

* Disable Python 3.11

* Fixed #1013: Empty mention should be None instead of empty string.

* Added #990 to the changelog

* Added #942 to the changelog

* Fixed coverage

* Update poetry and dependencies

* Fixed mypy

* Remove deprecated code

* Added more tests, update pyproject.toml

* Partial update docs

* Added initial Docs translation files

* Added more changes

* Added log message when connection is established in polling process

* Fixed action

* Disable lint for PyPy

* Added changelog for docs translation
2022-10-02 00:04:31 +03:00

41 lines
1.4 KiB
Python

from inspect import isclass
from typing import Any, Dict, Optional, Sequence, Type, Union, cast
from aiogram.filters.base import Filter
from aiogram.fsm.state import State, StatesGroup
from aiogram.types import TelegramObject
StateType = Union[str, None, State, StatesGroup, Type[StatesGroup]]
class StateFilter(Filter):
"""
State filter
"""
def __init__(self, *states: StateType) -> None:
if not states:
raise ValueError("At least one state is required")
self.states = states
def __str__(self) -> str:
return self._signature_to_string(
*self.states,
)
async def __call__(
self, obj: Union[TelegramObject], raw_state: Optional[str] = None
) -> Union[bool, Dict[str, Any]]:
allowed_states = cast(Sequence[StateType], self.states)
for allowed_state in allowed_states:
if isinstance(allowed_state, str) or allowed_state is None:
if allowed_state == "*" or raw_state == allowed_state:
return True
elif isinstance(allowed_state, (State, StatesGroup)):
if allowed_state(event=obj, raw_state=raw_state):
return True
elif isclass(allowed_state) and issubclass(allowed_state, StatesGroup):
if allowed_state()(event=obj, raw_state=raw_state):
return True
return False