Skip to content

Commit

Permalink
Refactor code to pass the Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew000 committed Oct 25, 2022
1 parent 7125c2b commit 7f7c182
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
5 changes: 3 additions & 2 deletions aiogram/fsm/storage/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from contextlib import asynccontextmanager
from dataclasses import dataclass
from typing import Any, AsyncGenerator, Dict, Optional, Union
from typing import Any, AsyncGenerator, Dict, Hashable, Optional, Union

from aiogram import Bot
from aiogram.fsm.state import State
Expand Down Expand Up @@ -91,10 +91,11 @@ async def close(self) -> None: # pragma: no cover


class BaseEventIsolation(ABC):
_locks: Dict[Hashable, Any]

@abstractmethod
@asynccontextmanager
async def lock(self, bot: Bot, key: StorageKey) -> AsyncGenerator[None, None]:
self._locks: Dict[StorageKey, Any]
"""
Isolate events with lock.
Will be used as context manager
Expand Down
9 changes: 3 additions & 6 deletions aiogram/fsm/storage/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ async def get_data(self, bot: Bot, key: StorageKey) -> Dict[str, Any]:


class DisabledEventIsolation(BaseEventIsolation):
def __init__(self) -> None:
self._locks: DefaultDict[Hashable, Lock]

@asynccontextmanager
async def lock(self, bot: Bot, key: StorageKey) -> AsyncGenerator[None, None]:
yield
Expand All @@ -74,9 +71,9 @@ async def lock(self, bot: Bot, key: StorageKey) -> AsyncGenerator[None, None]:
async def close(self) -> None:
self._locks.clear()

def _cleanup(self, key: Hashable):
if self._locks[key]._waiters is None:
def _cleanup(self, key: Hashable) -> None:
if self._locks[key]._waiters is None: # type: ignore[attr-defined]
del self._locks[key]

elif len(self._locks[key]._waiters) == 0:
elif len(self._locks[key]._waiters) == 0: # type: ignore[attr-defined]
del self._locks[key]
3 changes: 2 additions & 1 deletion tests/test_fsm/storage/test_isolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ async def test_lock(

for _ in range(100):
tasks.append(
asyncio.create_task(self._some_task(isolation, bot, self.random_storage_key(bot))))
asyncio.create_task(self._some_task(isolation, bot, self.random_storage_key(bot)))
)
await asyncio.sleep(0.01)

await asyncio.gather(*[task for task in tasks if not task.done()])
Expand Down

0 comments on commit 7f7c182

Please sign in to comment.