iter states in states group (#666)

* iter states in states group

* fix type hint

* remove empty line

* add changes for doc
This commit is contained in:
Andrey Tikhonov 2021-09-05 23:55:38 +03:00 committed by GitHub
parent 714ac8896c
commit 90b3a99039
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 1 deletions

2
CHANGES/666.feature Normal file
View file

@ -0,0 +1,2 @@
Ability to iterate over all states in StatesGroup.
Aiogram already had in check for states group so this is relative feature.

View file

@ -1,5 +1,5 @@
import inspect
from typing import Any, Optional, Tuple, Type, no_type_check
from typing import Any, Iterator, Optional, Tuple, Type, no_type_check
from ...types import TelegramObject
@ -118,6 +118,9 @@ class StatesGroupMeta(type):
def __str__(self) -> str:
return f"<StatesGroup '{self.__full_group_name__}'>"
def __iter__(self) -> Iterator[State]:
return iter(self.__all_states__)
class StatesGroup(metaclass=StatesGroupMeta):
@classmethod

View file

@ -150,6 +150,13 @@ class TestStatesGroup:
assert MyGroup.MyNestedGroup.get_root() is MyGroup
def test_iterable(self):
class Group(StatesGroup):
x = State()
y = State()
assert set(Group) == {Group.x, Group.y}
def test_empty_filter(self):
class MyGroup(StatesGroup):
pass