22from collections .abc import Callable
33from functools import wraps
44from inspect import iscoroutinefunction
5+ from types import MethodType
56from typing import Any , Protocol , runtime_checkable
67
78from injection ._core .common .asynchronous import Caller
89from injection ._core .module import Module , mod
910
10- type AsFunctionWrappedType [** P , T ] = type [_Callable [P , T ]]
11+ type AsFunctionWrappedType [** P , T ] = type [AsFunctionCallable [P , T ]]
1112
1213
1314@runtime_checkable
14- class _Callable [** P , T ](Protocol ):
15+ class AsFunctionCallable [** P , T ](Protocol ):
1516 __slots__ = ()
1617
1718 @abstractmethod
18- def __call__ (self , * args : P .args , ** kwargs : P .kwargs ) -> T :
19+ def call (self , * args : P .args , ** kwargs : P .kwargs ) -> T :
1920 raise NotImplementedError
2021
2122
@@ -29,28 +30,27 @@ def asfunction[**P, T](
2930 module = module or mod ()
3031
3132 def decorator (wp : AsFunctionWrappedType [P , T ]) -> Callable [P , T ]:
32- get_method = wp .__call__ .__get__
33- method = get_method (NotImplemented )
34- factory : Caller [..., Callable [P , T ]] = module .make_injected_function (
33+ fake_method = MethodType (wp .call , NotImplemented )
34+ factory : Caller [..., AsFunctionCallable [P , T ]] = module .make_injected_function (
3535 wp ,
3636 threadsafe = threadsafe ,
3737 ).__inject_metadata__
3838
3939 wrapper : Callable [P , T ]
4040
41- if iscoroutinefunction (method ):
41+ if iscoroutinefunction (fake_method ):
4242
43- @wraps (method )
43+ @wraps (fake_method )
4444 async def wrapper (* args : P .args , ** kwargs : P .kwargs ) -> Any :
4545 self = await factory .acall ()
46- return await get_method ( self ) (* args , ** kwargs )
46+ return await self . call (* args , ** kwargs ) # type: ignore[misc]
4747
4848 else :
4949
50- @wraps (method )
50+ @wraps (fake_method )
5151 def wrapper (* args : P .args , ** kwargs : P .kwargs ) -> T :
5252 self = factory .call ()
53- return get_method ( self ) (* args , ** kwargs )
53+ return self . call (* args , ** kwargs )
5454
5555 wrapper .__name__ = wp .__name__
5656 wrapper .__qualname__ = wp .__qualname__
0 commit comments