From 137a481c72399b92128584cb1526d3fd3386c609 Mon Sep 17 00:00:00 2001 From: darksidecat <58224121+darksidecat@users.noreply.github.com> Date: Mon, 23 Aug 2021 01:01:01 +0300 Subject: [PATCH] move mypy hack to utils, add lru_cache configuration --- aiogram/types/update.py | 13 +++---------- aiogram/utils/mypy_hacks.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 aiogram/utils/mypy_hacks.py diff --git a/aiogram/types/update.py b/aiogram/types/update.py index 0fed38b6..fc1ac03c 100644 --- a/aiogram/types/update.py +++ b/aiogram/types/update.py @@ -1,8 +1,8 @@ from __future__ import annotations -import functools -from typing import TYPE_CHECKING, Callable, Optional, TypeVar, cast +from typing import TYPE_CHECKING, Optional, cast +from ..utils.mypy_hacks import lru_cache from .base import TelegramObject if TYPE_CHECKING: # pragma: no cover @@ -16,13 +16,6 @@ if TYPE_CHECKING: # pragma: no cover from .pre_checkout_query import PreCheckoutQuery from .shipping_query import ShippingQuery -T = TypeVar("T") - - -def lru_cache(func: Callable[..., T]) -> T: - """fix lru_cache annotation doesn't work with a property""" - return functools.lru_cache()(func) # type: ignore - class Update(TelegramObject): """ @@ -66,7 +59,7 @@ class Update(TelegramObject): return hash((type(self), self.update_id)) @property # type: ignore - @lru_cache + @lru_cache() def event_type(self) -> str: """ Detect update type diff --git a/aiogram/utils/mypy_hacks.py b/aiogram/utils/mypy_hacks.py new file mode 100644 index 00000000..b992f386 --- /dev/null +++ b/aiogram/utils/mypy_hacks.py @@ -0,0 +1,17 @@ +import functools +from collections import Callable +from typing import TypeVar + +T = TypeVar("T") + + +def lru_cache(maxsize: int = 128, typed: bool = False) -> Callable[[T], T]: + """ + fix: lru_cache annotation doesn't work with a property + this hack is only needed for the property, so type annotations are as they are + """ + + def wrapper(func: T) -> T: + return functools.lru_cache(maxsize, typed)(func) # type: ignore + + return wrapper