diff --git a/CHANGES/927.bugfix.rst b/CHANGES/927.bugfix.rst new file mode 100644 index 00000000..c16ae945 --- /dev/null +++ b/CHANGES/927.bugfix.rst @@ -0,0 +1 @@ +Fixed the ability to compare the state, now comparison to copy of the state will return `True`. diff --git a/aiogram/dispatcher/fsm/state.py b/aiogram/dispatcher/fsm/state.py index 6b29833b..6c76913f 100644 --- a/aiogram/dispatcher/fsm/state.py +++ b/aiogram/dispatcher/fsm/state.py @@ -54,6 +54,16 @@ class State: return True return raw_state == self.state + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return self.state == other.state + if isinstance(other, str): + return self.state == other + return NotImplemented + + def __hash__(self) -> int: + return hash(self.state) + class StatesGroupMeta(type): __parent__: "Optional[Type[StatesGroup]]" diff --git a/tests/test_dispatcher/test_filters/test_state.py b/tests/test_dispatcher/test_filters/test_state.py index 2d8acda0..28bd578f 100644 --- a/tests/test_dispatcher/test_filters/test_state.py +++ b/tests/test_dispatcher/test_filters/test_state.py @@ -1,7 +1,9 @@ +from copy import copy from inspect import isclass import pytest +from aiogram.dispatcher.event.handler import FilterObject from aiogram.dispatcher.filters import StateFilter from aiogram.dispatcher.fsm.state import State, StatesGroup from aiogram.types import Update @@ -50,3 +52,23 @@ class TestStateFilter: async def test_filter(self, state, current_state, result): f = StateFilter(state=state) assert bool(await f(obj=Update(update_id=42), raw_state=current_state)) is result + + @pytestmark + async def test_create_filter_from_state(self): + FilterObject(callback=State(state="state")) + + @pytestmark + async def test_state_copy(self): + class SG(StatesGroup): + state = State() + + assert SG.state == copy(SG.state) + + assert SG.state == "SG:state" + assert "SG:state" == SG.state + + assert State() == State() + assert SG.state != 1 + + states = {SG.state: "OK"} + assert states.get(copy(SG.state)) == "OK"