import random
from typing import Callable, ParamSpec, TypeVar
T = TypeVar("T")
P = ParamSpec("P")
def some_global_condition() -> bool:
return random.choice([True, False])
def my_return_type_preserving_decorator(val: T) -> Callable[[Callable[P, T]], Callable[P, T]]:
def decorator(func: Callable[P, T]) -> Callable[P, T]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
if some_global_condition():
return val
return func(*args, **kwargs)
return wrapper
return decorator
@my_return_type_preserving_decorator((True, True, True))
def my_func(n: int) -> tuple[bool, bool, bool]:
return n > 10, n > 100, n > 1000
x = my_func(5)
for r in x:
print("yes" if r else "no")