Skip to content

Commit 6834e8d

Browse files
authored
feat: ✨ Allow future annotations with @inject
1 parent 091b777 commit 6834e8d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

injection/core/module.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727

2828
from injection.common.event import Event, EventChannel, EventListener
29-
from injection.common.lazy import LazyMapping
29+
from injection.common.lazy import Lazy, LazyMapping
3030
from injection.exceptions import (
3131
ModuleCircularUseError,
3232
ModuleError,
@@ -515,13 +515,11 @@ def decorator(wp):
515515
return decorator(wrapped) if wrapped else decorator
516516

517517
def __decorator(self, function: Callable[..., Any], /) -> Callable[..., Any]:
518-
signature = inspect.signature(function, eval_str=True)
519-
binder = Binder(signature).update(self.__module)
520-
self.__module.add_listener(binder)
518+
lazy_binder = Lazy[Binder](lambda: self.__new_binder(function))
521519

522520
@wraps(function)
523521
def wrapper(*args, **kwargs):
524-
arguments = binder.bind(*args, **kwargs)
522+
arguments = lazy_binder.value.bind(*args, **kwargs)
525523
return function(*arguments.args, **arguments.kwargs)
526524

527525
return wrapper
@@ -531,6 +529,12 @@ def __class_decorator(self, cls: type, /) -> type:
531529
type.__setattr__(cls, "__init__", self.__decorator(init_function))
532530
return cls
533531

532+
def __new_binder(self, function: Callable[..., Any]) -> Binder:
533+
signature = inspect.signature(function, eval_str=True)
534+
binder = Binder(signature).update(self.__module)
535+
self.__module.add_listener(binder)
536+
return binder
537+
534538

535539
@final
536540
@dataclass(repr=False, frozen=True, slots=True)
@@ -552,7 +556,7 @@ def decorator(wp):
552556
if auto_inject:
553557
wp = self.__module.inject(wp)
554558

555-
@lambda function: function()
559+
@lambda fn: fn()
556560
def references():
557561
if reference := self.__get_reference(wp):
558562
yield reference

0 commit comments

Comments
 (0)