mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Refactor code to pass the Lint
This commit is contained in:
parent
7125c2b39f
commit
7f7c1820e5
3 changed files with 8 additions and 9 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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()])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue