Skip to content

Commit f30821c

Browse files
[functools] Allow method override with @cache decorator (#15238)
1 parent 63ea326 commit f30821c

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

stdlib/@tests/test_cases/check_functools.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from functools import cached_property, wraps
3+
from functools import cache, cached_property, wraps
44
from typing import Callable, TypeVar
55
from typing_extensions import ParamSpec, assert_type
66

@@ -96,3 +96,15 @@ class Y(X):
9696
@cached_property
9797
def some(self) -> Child: # safe override
9898
return Child()
99+
100+
101+
class CachedParent:
102+
@cache
103+
def method(self) -> Parent:
104+
return Parent()
105+
106+
107+
class CachedChild(CachedParent):
108+
@cache
109+
def method(self) -> Child:
110+
return Child()

stdlib/functools.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ class _CacheParameters(TypedDict):
5454
typed: bool
5555

5656
@final
57-
class _lru_cache_wrapper(Generic[_T]):
58-
__wrapped__: Callable[..., _T]
59-
def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ...
57+
class _lru_cache_wrapper(Generic[_T_co]):
58+
__wrapped__: Callable[..., _T_co]
59+
def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T_co: ...
6060
def cache_info(self) -> _CacheInfo: ...
6161
def cache_clear(self) -> None: ...
6262
def cache_parameters(self) -> _CacheParameters: ...
63-
def __copy__(self) -> _lru_cache_wrapper[_T]: ...
64-
def __deepcopy__(self, memo: Any, /) -> _lru_cache_wrapper[_T]: ...
63+
def __copy__(self) -> _lru_cache_wrapper[_T_co]: ...
64+
def __deepcopy__(self, memo: Any, /) -> _lru_cache_wrapper[_T_co]: ...
6565

6666
@overload
6767
def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _T]], _lru_cache_wrapper[_T]]: ...

0 commit comments

Comments
 (0)