22
33from dataclasses import dataclass
44from typing import (
5- TYPE_CHECKING ,
65 Any ,
76 Callable ,
87 Concatenate ,
3635Ts = TypeVarTuple ("Ts" )
3736
3837
39- class apply (Generic [T_APPLY ]):
38+ class apply (Generic [T , R ]):
4039 """Make a function callable by using `@` operator.
4140
4241 This is the `@` version of `... | fn` in `better_functools.pipe.Composition`.
4342
4443 >>> "1234" @ apply(int)
4544 1234
45+
46+ prepending with `~` will return the null coalescing version.
47+
48+ >>> None @ ~apply(int) is None
49+ True
4650 """
4751
48- def __init__ (self , fn : T_APPLY ) -> None :
52+ def __init__ (self , fn : Callable [[ T ], R ] ) -> None :
4953 self .fn = fn
5054
51- if TYPE_CHECKING :
52- __call__ : T_APPLY
53- __rmatmul__ : T_APPLY
54- else :
55+ def __call__ (self , val : T ) -> R :
56+ return self .fn (val )
5557
56- def __call__ (self , * args , ** kwargs ):
57- return self .fn (* args , ** kwargs )
58+ __rmatmul__ = __call__
5859
59- __rmatmul__ = __call__
60+ def __invert__ (self ) -> apply [T | None , R | None ]:
61+ def _fn (v : T | None ) -> R | None :
62+ if v is None :
63+ return None
64+ return self .fn (v )
65+
66+ return apply (_fn )
6067
6168
6269@dataclass
@@ -235,7 +242,7 @@ def __rmatmul__(self, left: T | None) -> T | Self:
235242"""
236243
237244
238- def static (fn : Callable [Concatenate [T , P ], R ]) -> Callable [P , apply [Callable [[ T ] , R ] ]]:
245+ def static (fn : Callable [Concatenate [T , P ], R ]) -> Callable [P , apply [T , R ]]:
239246 """*Experimental*: Make a bound method static.
240247
241248 This makes it easier to chain the method.
@@ -259,7 +266,7 @@ def static(fn: Callable[Concatenate[T, P], R]) -> Callable[P, apply[Callable[[T]
259266 - Does not work well with MyPy.
260267 """
261268
262- def _outer (* args : P .args , ** kwargs : P .kwargs ) -> apply [Callable [[ T ] , R ] ]:
269+ def _outer (* args : P .args , ** kwargs : P .kwargs ) -> apply [T , R ]:
263270 @apply
264271 def _inner (first : T ) -> R :
265272 return fn (first , * args , ** kwargs )
0 commit comments