-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add inline option to subroutine decorator
- Loading branch information
Showing
1 changed file
with
19 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |