|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from functools import cache, cached_property, wraps |
| 3 | +from functools import cache, cached_property, lru_cache, wraps |
4 | 4 | from typing import Callable, TypeVar |
5 | 5 | from typing_extensions import ParamSpec, assert_type |
6 | 6 |
|
@@ -64,17 +64,43 @@ def func_wrapper(x: int) -> None: ... |
64 | 64 |
|
65 | 65 |
|
66 | 66 | @cache |
67 | | -def check_cached(x: int) -> int: |
| 67 | +def check_cache(x: int) -> int: |
68 | 68 | return x * 2 |
69 | 69 |
|
70 | 70 |
|
71 | | -assert_type(check_cached(3), int) |
| 71 | +assert_type(check_cache(3), int) |
72 | 72 | # Type checkers should check the argument type, but this is currently not |
73 | 73 | # possible. See https://github.com/python/typeshed/issues/6347 and |
74 | 74 | # https://github.com/python/typeshed/issues/11280. |
75 | 75 | # check_cached("invalid") # xtype: ignore |
76 | 76 |
|
77 | | -assert_type(check_cached.cache_info().misses, int) |
| 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 | +@lru_cache(maxsize=32) |
| 90 | +def check_lru_cache_with_maxsize(x: int) -> int: |
| 91 | + return x * 2 |
| 92 | + |
| 93 | +assert_type(check_lru_cache(3), int) |
| 94 | +assert_type(check_lru_cache_with_maxsize(3), int) |
| 95 | +# Type checkers should check the argument type, but this is currently not |
| 96 | +# possible. See https://github.com/python/typeshed/issues/6347 and |
| 97 | +# https://github.com/python/typeshed/issues/11280. |
| 98 | +# check_lru_cache("invalid") # xtype: ignore |
| 99 | +# check_lru_cache_with_maxsize("invalid") # xtype: ignore |
| 100 | + |
| 101 | +assert_type(check_lru_cache.cache_info().misses, int) |
| 102 | +assert_type(check_lru_cache_with_maxsize.cache_info().misses, int) |
| 103 | + |
78 | 104 |
|
79 | 105 | # |
80 | 106 | # Tests for @cached_property |
|
0 commit comments