Skip to content
57 changes: 56 additions & 1 deletion stdlib/@tests/test_cases/check_functools.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from __future__ import annotations

from functools import cached_property, wraps
from functools import cache, cached_property, lru_cache, wraps
from typing import Callable, TypeVar
from typing_extensions import ParamSpec, assert_type

P = ParamSpec("P")
T_co = TypeVar("T_co", covariant=True)

#
# Tests for @wraps
#


def my_decorator(func: Callable[P, T_co]) -> Callable[P, T_co]:
@wraps(func)
Expand Down Expand Up @@ -54,6 +58,57 @@ def func_wrapper(x: int) -> None: ...
func_wrapper(3)


#
# Tests for @cache
#


@cache
def check_cache(x: int) -> int:
return x * 2


assert_type(check_cache(3), int)
# Type checkers should check the argument type, but this is currently not
# possible. See https://github.com/python/typeshed/issues/6347 and
# https://github.com/python/typeshed/issues/11280.
# check_cached("invalid") # xtype: ignore

assert_type(check_cache.cache_info().misses, int)


#
# Tests for @lru_cache
#


@lru_cache
def check_lru_cache(x: int) -> int:
return x * 2


@lru_cache(maxsize=32)
def check_lru_cache_with_maxsize(x: int) -> int:
return x * 2


assert_type(check_lru_cache(3), int)
assert_type(check_lru_cache_with_maxsize(3), int)
# Type checkers should check the argument type, but this is currently not
# possible. See https://github.com/python/typeshed/issues/6347 and
# https://github.com/python/typeshed/issues/11280.
# check_lru_cache("invalid") # xtype: ignore
# check_lru_cache_with_maxsize("invalid") # xtype: ignore

assert_type(check_lru_cache.cache_info().misses, int)
assert_type(check_lru_cache_with_maxsize.cache_info().misses, int)


#
# Tests for @cached_property
#


class A:
def __init__(self, x: int):
self.x = x
Expand Down