|
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 |
|
7 | 7 | P = ParamSpec("P") |
8 | 8 | T_co = TypeVar("T_co", covariant=True) |
9 | 9 |
|
| 10 | +# |
| 11 | +# Tests for @wraps |
| 12 | +# |
| 13 | + |
10 | 14 |
|
11 | 15 | def my_decorator(func: Callable[P, T_co]) -> Callable[P, T_co]: |
12 | 16 | @wraps(func) |
@@ -54,6 +58,57 @@ def func_wrapper(x: int) -> None: ... |
54 | 58 | func_wrapper(3) |
55 | 59 |
|
56 | 60 |
|
| 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 | + |
57 | 112 | class A: |
58 | 113 | def __init__(self, x: int): |
59 | 114 | self.x = x |
|
0 commit comments