Refactor code to pass the Lint

This commit is contained in:
andrew000 2022-10-25 23:42:10 +03:00
parent 7125c2b39f
commit 7f7c1820e5
No known key found for this signature in database
GPG key ID: D332A306AAA27181
3 changed files with 8 additions and 9 deletions

View file

@ -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
@ -91,10 +91,11 @@ class BaseStorage(ABC):
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

View file

@ -47,9 +47,6 @@ class MemoryStorage(BaseStorage):
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
@ -74,9 +71,9 @@ class SimpleEventIsolation(BaseEventIsolation):
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]

View file

@ -48,7 +48,8 @@ class TestLockIsolations:
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()])