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

1 from collections.abc import Callable

2 from typing import TypeVar, overload

3

4 from remedapy.local_types import NotSet

5

6 T = TypeVar('T')

7 Fallback = TypeVar('Fallback')

8

9

10@overload

11 def default_to(data: T | None, fallback: Fallback, /) -> T | Fallback: ...

12

13

14@overload

15 def default_to(fallback: Fallback, /) -> Callable[[T | None], T | Fallback]: ...

16

17

18 def default_to(

19 first: T | None | Fallback,

20 second: Fallback = NotSet,

21) -> T | Fallback | Callable[[T | None], T | Fallback]:

22 if second is NotSet:

23 return lambda x: default_to(x, first) # pyright: ignore[reportReturnType]

24 data = first

25 fallback = second

26 return data if data is not None else fallback