move mypy hack to utils, add lru_cache configuration

This commit is contained in:
darksidecat 2021-08-23 01:01:01 +03:00
parent 7c5c034cc1
commit 137a481c72
2 changed files with 20 additions and 10 deletions

View file

@ -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

View file

@ -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