Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions injection/_core/asfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Protocol

from injection._core.common.asynchronous import Caller
from injection._core.module import Module, mod
from injection._core.module import InjectMetadata, Module, mod


class _AsFunctionCallable[**P, T](Protocol):
Expand All @@ -23,15 +23,14 @@ def asfunction[**P, T](
) -> Any:
def decorator(wp: AsFunctionWrappedType[P, T]) -> Callable[P, T]:
fake_method = wp.__call__.__get__(NotImplemented, wp)
factory: Caller[..., Callable[P, T]] = (module or mod())._metadata(
wp,
threadsafe,
)
metadata: InjectMetadata[..., Callable[P, T]] = (
module or mod()
).create_metadata(wp, threadsafe)

wrapper: Callable[P, T] = (
_wrap_async(factory) # type: ignore[arg-type, assignment]
_wrap_async(metadata) # type: ignore[arg-type, assignment]
if iscoroutinefunction(fake_method)
else _wrap_sync(factory)
else _wrap_sync(metadata)
)
wrapper = update_wrapper(wrapper, fake_method)

Expand Down
23 changes: 12 additions & 11 deletions injection/_core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ def decorator(wp: Callable[P, T]) -> Callable[P, T]:

return decorator(wrapped) if wrapped else decorator

def create_metadata[**P, T](
self,
wrapped: Callable[P, T],
/,
threadsafe: bool | None = None,
) -> InjectMetadata[P, T]:
return InjectMetadata(wrapped, threadsafe).listen(self)

if TYPE_CHECKING: # pragma: no cover

@overload
Expand All @@ -387,7 +395,7 @@ def make_injected_function[**P, T](
/,
threadsafe: bool | None = None,
) -> InjectedFunction[P, T]:
metadata = self._metadata(wrapped, threadsafe)
metadata = self.create_metadata(wrapped, threadsafe)

if iscoroutinefunction(wrapped):
return AsyncInjectedFunction(metadata) # type: ignore[arg-type, return-value]
Expand All @@ -400,7 +408,7 @@ def make_async_factory[T](
/,
threadsafe: bool | None = None,
) -> Callable[..., Awaitable[T]]:
return self._metadata(wrapped, threadsafe).acall
return self.create_metadata(wrapped, threadsafe).acall

async def afind_instance[T](
self,
Expand Down Expand Up @@ -514,7 +522,7 @@ def aget_lazy_instance[T, Default](
threadsafe: bool | None = None,
) -> Awaitable[T | Default]:
return SimpleAwaitable(
self._metadata(
self.create_metadata(
lambda instance=default: instance,
threadsafe=threadsafe,
)
Expand Down Expand Up @@ -550,7 +558,7 @@ def get_lazy_instance[T, Default](
threadsafe: bool | None = None,
) -> Invertible[T | Default]:
return SimpleInvertible(
self._metadata(
self.create_metadata(
lambda instance=default: instance,
threadsafe=threadsafe,
)
Expand Down Expand Up @@ -681,13 +689,6 @@ def dispatch(self, event: Event) -> Iterator[None]:
finally:
self.__debug(event)

def _metadata[**P, T](
self,
wrapped: Callable[P, T],
threadsafe: bool | None = None,
) -> InjectMetadata[P, T]:
return InjectMetadata(wrapped, threadsafe).listen(self)

def _iter_locators(self) -> Iterator[Locator]:
for module in self.__modules:
yield from module._iter_locators()
Expand Down
16 changes: 8 additions & 8 deletions injection/testing/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ from injection.loaders import LoadedProfile, ProfileLoader

TEST_PROFILE_NAME: Final[str] = ...

__MODULE: Final[Module] = ...
_test_module: Final[Module] = ...

reserve_scoped_test_slot = __MODULE.reserve_scoped_slot
set_test_constant = __MODULE.set_constant
should_be_test_injectable = __MODULE.should_be_injectable
test_constant = __MODULE.constant
test_injectable = __MODULE.injectable
test_scoped = __MODULE.scoped
test_singleton = __MODULE.singleton
reserve_scoped_test_slot = _test_module.reserve_scoped_slot
set_test_constant = _test_module.set_constant
should_be_test_injectable = _test_module.should_be_injectable
test_constant = _test_module.constant
test_injectable = _test_module.injectable
test_scoped = _test_module.scoped
test_singleton = _test_module.singleton

def load_test_profile(loader: ProfileLoader = ...) -> LoadedProfile:
"""
Expand Down
6 changes: 3 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.