Skip to content

Commit 44dac88

Browse files
authored
Add tests for @functools.cache (#14768)
Closes: #11280
1 parent 00615be commit 44dac88

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

stdlib/@tests/test_cases/check_functools.py

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

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

77
P = ParamSpec("P")
88
T_co = TypeVar("T_co", covariant=True)
99

10+
#
11+
# Tests for @wraps
12+
#
13+
1014

1115
def my_decorator(func: Callable[P, T_co]) -> Callable[P, T_co]:
1216
@wraps(func)
@@ -54,6 +58,57 @@ def func_wrapper(x: int) -> None: ...
5458
func_wrapper(3)
5559

5660

61+
#
62+
# Tests for @cache
63+
#
64+
65+
66+
@cache
67+
def check_cache(x: int) -> int:
68+
return x * 2
69+
70+
71+
assert_type(check_cache(3), int)
72+
# Type checkers should check the argument type, but this is currently not
73+
# possible. See https://github.com/python/typeshed/issues/6347 and
74+
# https://github.com/python/typeshed/issues/11280.
75+
# check_cached("invalid") # xtype: ignore
76+
77+
assert_type(check_cache.cache_info().misses, int)
78+
79+
80+
#
81+
# Tests for @lru_cache
82+
#
83+
84+
85+
@lru_cache
86+
def check_lru_cache(x: int) -> int:
87+
return x * 2
88+
89+
90+
@lru_cache(maxsize=32)
91+
def check_lru_cache_with_maxsize(x: int) -> int:
92+
return x * 2
93+
94+
95+
assert_type(check_lru_cache(3), int)
96+
assert_type(check_lru_cache_with_maxsize(3), int)
97+
# Type checkers should check the argument type, but this is currently not
98+
# possible. See https://github.com/python/typeshed/issues/6347 and
99+
# https://github.com/python/typeshed/issues/11280.
100+
# check_lru_cache("invalid") # xtype: ignore
101+
# check_lru_cache_with_maxsize("invalid") # xtype: ignore
102+
103+
assert_type(check_lru_cache.cache_info().misses, int)
104+
assert_type(check_lru_cache_with_maxsize.cache_info().misses, int)
105+
106+
107+
#
108+
# Tests for @cached_property
109+
#
110+
111+
57112
class A:
58113
def __init__(self, x: int):
59114
self.x = x

0 commit comments

Comments
 (0)