From 2cc15b3dc706eb8585b9658bf67b38da215e2e38 Mon Sep 17 00:00:00 2001 From: Bobby Lat Date: Mon, 20 Jan 2025 10:15:13 +0800 Subject: [PATCH] feat: add inline option to subroutine decorator --- src/_algopy_testing/decorators/subroutine.py | 22 +++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/_algopy_testing/decorators/subroutine.py b/src/_algopy_testing/decorators/subroutine.py index 7c3845a..282386e 100644 --- a/src/_algopy_testing/decorators/subroutine.py +++ b/src/_algopy_testing/decorators/subroutine.py @@ -1,9 +1,25 @@ from collections.abc import Callable -from typing import ParamSpec, TypeVar +from functools import partial, wraps +from typing import Literal, ParamSpec, TypeVar, overload _P = ParamSpec("_P") _R = TypeVar("_R") -def subroutine(sub: Callable[_P, _R]) -> Callable[_P, _R]: - return sub +@overload +def subroutine(sub: Callable[_P, _R], /) -> Callable[_P, _R]: ... +@overload +def subroutine( + *, inline: bool | Literal["auto"] = "auto" +) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ... +def subroutine( + sub: Callable[_P, _R] | None = None, *, inline: bool | Literal["auto"] = "auto" +) -> Callable[_P, _R] | Callable[[Callable[_P, _R]], Callable[_P, _R]]: + if sub is None: + return partial(subroutine, inline=inline) + + @wraps(sub) + def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R: + return sub(*args, **kwargs) + + return wrapper