From 7f7c1820e544ad33cae79152bbeb99efd041e7f2 Mon Sep 17 00:00:00 2001 From: andrew000 <11490628+andrew000@users.noreply.github.com> Date: Tue, 25 Oct 2022 23:42:10 +0300 Subject: [PATCH] Refactor code to pass the Lint --- aiogram/fsm/storage/base.py | 5 +++-- aiogram/fsm/storage/memory.py | 9 +++------ tests/test_fsm/storage/test_isolation.py | 3 ++- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/aiogram/fsm/storage/base.py b/aiogram/fsm/storage/base.py index eea53560..08aadda6 100644 --- a/aiogram/fsm/storage/base.py +++ b/aiogram/fsm/storage/base.py @@ -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 diff --git a/aiogram/fsm/storage/memory.py b/aiogram/fsm/storage/memory.py index c3dfbb23..45213976 100644 --- a/aiogram/fsm/storage/memory.py +++ b/aiogram/fsm/storage/memory.py @@ -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] diff --git a/tests/test_fsm/storage/test_isolation.py b/tests/test_fsm/storage/test_isolation.py index bd3e680a..417d7441 100644 --- a/tests/test_fsm/storage/test_isolation.py +++ b/tests/test_fsm/storage/test_isolation.py @@ -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()])