Fix unexpected behavior of sequences in StateFilter (#791)

* Fix sequence check behavior in StateFilter

* Add sequence cases to StateFilter tests

* Add the changelog
This commit is contained in:
Daniil Kovalenko 2021-12-29 08:39:28 +07:00 committed by GitHub
parent 7c14a6b16b
commit 6e39f9fada
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 4 deletions

1
CHANGES/791.bugfix Normal file
View file

@ -0,0 +1 @@
Fixed unexpected behavior of sequences in the StateFilter.

View file

@ -37,11 +37,12 @@ class StateFilter(BaseFilter):
allowed_states = cast(Sequence[StateType], self.state) allowed_states = cast(Sequence[StateType], self.state)
for allowed_state in allowed_states: for allowed_state in allowed_states:
if isinstance(allowed_state, str) or allowed_state is None: if isinstance(allowed_state, str) or allowed_state is None:
if allowed_state == "*": if allowed_state == "*" or raw_state == allowed_state:
return True return True
return raw_state == allowed_state
elif isinstance(allowed_state, (State, StatesGroup)): elif isinstance(allowed_state, (State, StatesGroup)):
return allowed_state(event=obj, raw_state=raw_state) if allowed_state(event=obj, raw_state=raw_state):
return True
elif isclass(allowed_state) and issubclass(allowed_state, StatesGroup): elif isclass(allowed_state) and issubclass(allowed_state, StatesGroup):
return allowed_state()(event=obj, raw_state=raw_state) if allowed_state()(event=obj, raw_state=raw_state):
return True
return False return False

View file

@ -41,6 +41,9 @@ class TestStateFilter:
[[None], None, True], [[None], None, True],
[None, "state", False], [None, "state", False],
[[], "state", False], [[], "state", False],
[[State("state"), "state"], "state", True],
[[MyGroup(), State("state")], "@:state", True],
[[MyGroup, State("state")], "state", False],
], ],
) )
@pytestmark @pytestmark