From eeacd1019ae17948dc21106cb6848fcdb5ad2bb1 Mon Sep 17 00:00:00 2001 From: darksidecat Date: Sun, 19 Jun 2022 01:39:11 +0300 Subject: [PATCH] Fix the ability to copy the state, now copying the state will return the same state. --- CHANGES/927.bugfix.rst | 1 + aiogram/dispatcher/fsm/state.py | 6 ++++++ tests/test_dispatcher/test_filters/test_state.py | 12 ++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 CHANGES/927.bugfix.rst diff --git a/CHANGES/927.bugfix.rst b/CHANGES/927.bugfix.rst new file mode 100644 index 00000000..4fba5a42 --- /dev/null +++ b/CHANGES/927.bugfix.rst @@ -0,0 +1 @@ +Fixed the ability to copy the state, now copying the state will return the same state. diff --git a/aiogram/dispatcher/fsm/state.py b/aiogram/dispatcher/fsm/state.py index 6b29833b..b1841a4b 100644 --- a/aiogram/dispatcher/fsm/state.py +++ b/aiogram/dispatcher/fsm/state.py @@ -54,6 +54,12 @@ class State: return True return raw_state == self.state + def __copy__(self): + return self + + def __deepcopy__(self, memo=None): + return self + 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..e127430d 100644 --- a/tests/test_dispatcher/test_filters/test_state.py +++ b/tests/test_dispatcher/test_filters/test_state.py @@ -1,3 +1,4 @@ +from copy import deepcopy, copy from inspect import isclass import pytest @@ -50,3 +51,14 @@ 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_state_copy(self): + class SG(StatesGroup): + state = State() + + assert SG.state is deepcopy(SG.state) + assert SG.state is copy(SG.state) + + assert SG is copy(SG) + assert SG is deepcopy(SG)