core/general: move cached_property into compat, use standard implementation from python3.8

This commit is contained in:
Dima Gerasimov 2022-05-31 13:06:29 +01:00 committed by karlicoss
parent 711157e0f5
commit 4e59a65f9a
6 changed files with 38 additions and 27 deletions

View file

@ -128,13 +128,6 @@ def test_make_dict() -> None:
assert d == {0: 0, 1: 1, 2: 0, 3: 1, 4: 0}
Cl = TypeVar('Cl')
R = TypeVar('R')
def cproperty(f: Callable[[Cl], R]) -> R:
return property(functools.lru_cache(maxsize=1)(f)) # type: ignore
# https://stackoverflow.com/a/12377059/706389
def listify(fn=None, wrapper=list):
"""
@ -638,3 +631,6 @@ class DummyExecutor(Executor):
def shutdown(self, wait: bool=True) -> None: # type: ignore[override]
self._shutdown = True
# legacy deprecated import
from .compat import cached_property as cproperty

View file

@ -90,3 +90,16 @@ def removeprefix(text: str, prefix: str) -> str:
if text.startswith(prefix):
return text[len(prefix):]
return text
# can remove after python3.8
if sys.version_info[:2] >= (3, 8):
from functools import cached_property
else:
from typing import TypeVar, Callable
Cl = TypeVar('Cl')
R = TypeVar('R')
def cached_property(f: Callable[[Cl], R]) -> R:
return property(functools.lru_cache(maxsize=1)(f)) # type: ignore
del Cl
del R