Coverage for src/remedapy/decorator.py: 100%

1 import inspect

2 from collections.abc import Callable

3 from typing import Any, Concatenate, ParamSpec, TypeVar

4

5 DataT = TypeVar('DataT')

6 P = ParamSpec('P')

7 R = TypeVar('R')

8

9

10 def make_data_last(

11 f: Callable[Concatenate[DataT, P], R],

12) -> Callable[Concatenate[DataT, P], R] | Callable[P, Callable[[DataT], R]]:

13 number_of_positional_params = sum(

14 1 for x in inspect.signature(f).parameters.values() if x.kind == inspect.Parameter.POSITIONAL_ONLY

15 )

16

17 def inner(*args: Any, **kwargs: Any) -> R | Callable[[DataT], R]:

18 if len(args) == number_of_positional_params:

19 return f(*args, **kwargs)

20 return lambda data: f(data, *args, **kwargs)

21

22 return inner # pyright: ignore[reportReturnType]