Skip to content

Commit 547841e

Browse files
authored
refactoring: ♻️ replace call with __call__ in asfunction
1 parent 6241065 commit 547841e

4 files changed

Lines changed: 48 additions & 58 deletions

File tree

documentation/integrations/unlisted-framework.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ issues.
1313
If your framework inspects function signatures, things get a bit trickier. This is because dependencies can't be present
1414
in function parameters.
1515

16-
To solve this, you can define a class with a `call` method (where dependencies are injected when the class is
16+
To solve this, you can define a class with a `__call__` method (where dependencies are injected when the class is
1717
instantiated), and use the `asfunction` decorator to turn it into a function.
1818

19-
The resulting function will have the same signature as the `call` method, but without the `self` parameter.
19+
The resulting function will have the same signature as the `__call__` method, but without the `self` parameter.
2020

2121
Example:
2222

@@ -28,7 +28,7 @@ from injection import asfunction
2828
class DoSomething(NamedTuple):
2929
service: MyService
3030

31-
def call(self):
31+
def __call__(self):
3232
self.service.do_work()
3333
```
3434

injection/_core/asfunction.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
from abc import abstractmethod
21
from collections.abc import Callable
32
from functools import wraps
43
from inspect import iscoroutinefunction
5-
from typing import Any, Protocol, runtime_checkable
4+
from typing import Any
65

76
from injection._core.common.asynchronous import Caller
87
from injection._core.module import Module, mod
98

10-
type AsFunctionWrappedType[**P, T] = type[AsFunctionCallable[P, T]]
11-
12-
13-
@runtime_checkable
14-
class AsFunctionCallable[**P, T](Protocol):
15-
__slots__ = ()
16-
17-
@abstractmethod
18-
def call(self, *args: P.args, **kwargs: P.kwargs) -> T:
19-
raise NotImplementedError
9+
type AsFunctionWrappedType[**P, T] = type[Callable[P, T]]
2010

2111

2212
def asfunction[**P, T](
@@ -29,8 +19,8 @@ def asfunction[**P, T](
2919
module = module or mod()
3020

3121
def decorator(wp: AsFunctionWrappedType[P, T]) -> Callable[P, T]:
32-
fake_method = wp.call.__get__(NotImplemented)
33-
factory: Caller[..., AsFunctionCallable[P, T]] = module.make_injected_function(
22+
fake_method = wp.__call__.__get__(NotImplemented, wp)
23+
factory: Caller[..., Callable[P, T]] = module.make_injected_function(
3424
wp,
3525
threadsafe=threadsafe,
3626
).__inject_metadata__
@@ -42,14 +32,14 @@ def decorator(wp: AsFunctionWrappedType[P, T]) -> Callable[P, T]:
4232
@wraps(fake_method)
4333
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> Any:
4434
self = await factory.acall()
45-
return await self.call(*args, **kwargs) # type: ignore[misc]
35+
return await self(*args, **kwargs) # type: ignore[misc]
4636

4737
else:
4838

4939
@wraps(fake_method)
5040
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
5141
self = factory.call()
52-
return self.call(*args, **kwargs)
42+
return self(*args, **kwargs)
5343

5444
wrapper.__name__ = wp.__name__
5545
wrapper.__qualname__ = wp.__qualname__

tests/core/test_asfunction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Dependency: ...
1212
class SyncFunction(NamedTuple):
1313
dependency: Dependency
1414

15-
def call(self):
15+
def __call__(self):
1616
return self.dependency
1717

1818
assert isinstance(SyncFunction(), Dependency)
@@ -28,7 +28,7 @@ async def dependency_recipe() -> Dependency:
2828
class AsyncFunction(NamedTuple):
2929
dependency: Dependency
3030

31-
async def call(self):
31+
async def __call__(self):
3232
return self.dependency
3333

3434
assert isinstance(await AsyncFunction(), Dependency)

0 commit comments

Comments
 (0)