Skip to content

Commit

Permalink
Remove unused _resolve_unresolved_provider method
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirVondukr committed Jan 28, 2024
1 parent 2654a4e commit 56327f3
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion aioinject/_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from types import TracebackType
from typing import TYPE_CHECKING, Any, Literal, TypeVar

from aioinject._utils import enter_context_maybe, enter_sync_context_maybe
from aioinject.providers import DependencyLifetime
from aioinject.utils import enter_context_maybe, enter_sync_context_maybe


if TYPE_CHECKING:
Expand Down
File renamed without changes.
26 changes: 7 additions & 19 deletions aioinject/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,18 @@ def __init__(self) -> None:
self._unresolved_providers: list[Provider[Any]] = []
self.type_context: dict[str, type[Any]] = {}

def _resolve_unresolved_provider(self) -> None:
for provider in self._unresolved_providers:
with contextlib.suppress(NameError):
self._register_impl(provider)
self._unresolved_providers.remove(provider)

def _register_impl(self, provider: Provider[Any]) -> None:
def register(
self,
provider: Provider[Any],
) -> None:
provider_type = provider.resolve_type(self.type_context)
if provider_type in self.providers:
msg = f"Provider for type {provider_type} is already registered"
raise ValueError(msg)
self.providers[provider_type] = provider
if klass_name := getattr(provider_type, "__name__", None):
self.type_context[klass_name] = provider_type

def register(
self,
provider: Provider[Any],
) -> None:
try:
self._register_impl(provider)
except NameError:
self._unresolved_providers.append(provider)
self._resolve_unresolved_provider()
self.providers[provider_type] = provider
if class_name := getattr(provider_type, "__name__", None):
self.type_context[class_name] = provider_type

def get_provider(self, type_: type[_T]) -> Provider[_T]:
try:
Expand Down
4 changes: 2 additions & 2 deletions aioinject/ext/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Callable
from typing import TYPE_CHECKING, ParamSpec, TypeVar

from aioinject import decorators, utils
from aioinject import _utils, decorators


if TYPE_CHECKING:
Expand All @@ -20,7 +20,7 @@ def inject(function: Callable[_P, _T]) -> Callable[_P, _T]:
function,
inject_method=decorators.InjectMethod.context,
)
return utils.clear_wrapper(wrapper)
return _utils.clear_wrapper(wrapper)


class AioInjectMiddleware:
Expand Down
4 changes: 2 additions & 2 deletions aioinject/ext/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from litestar.plugins import InitPluginProtocol
from litestar.types import ASGIApp, Receive, Scope, Send

from aioinject import decorators, utils
from aioinject import _utils, decorators


if TYPE_CHECKING:
Expand All @@ -27,7 +27,7 @@ def inject(function: Callable[_P, _T]) -> Callable[_P, _T]:
function,
inject_method=decorators.InjectMethod.context,
)
return utils.clear_wrapper(wrapper)
return _utils.clear_wrapper(wrapper)


class AioInjectMiddleware(MiddlewareProtocol):
Expand Down
4 changes: 2 additions & 2 deletions aioinject/ext/strawberry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from strawberry.extensions import SchemaExtension

from aioinject import decorators, utils
from aioinject import _utils, decorators
from aioinject.context import container_var


Expand All @@ -21,7 +21,7 @@ def inject(function: Callable[_P, _T]) -> Callable[_P, _T]:
function,
inject_method=decorators.InjectMethod.container,
)
return utils.clear_wrapper(wrapper)
return _utils.clear_wrapper(wrapper)


class AioInjectExtension(SchemaExtension):
Expand Down
20 changes: 10 additions & 10 deletions aioinject/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

import typing_extensions

from aioinject.markers import Inject
from aioinject.utils import (
from aioinject._utils import (
_get_type_hints,
is_context_manager_function,
remove_annotation,
)
from aioinject.markers import Inject


_T = TypeVar("_T")
Expand Down Expand Up @@ -169,7 +169,7 @@ class Provider(Protocol[_T]):
impl: Any
lifetime: DependencyLifetime
_cached_dependencies: tuple[Dependency[object], ...]
_cached_type: type[_T] # TODO: I think it is redundant.
_cached_type: type[_T]

This comment has been minimized.

Copy link
@nrbnlulu

nrbnlulu Jan 28, 2024

Contributor

Why do we need this?


async def provide(self, kwargs: Mapping[str, Any]) -> _T:
...
Expand All @@ -190,13 +190,6 @@ def resolve_type(self, context: dict[str, Any] | None = None) -> type[_T]:
self._cached_type = self._resolve_type_impl(context)
return self._cached_type

def type_hints(self, context: dict[str, Any] | None) -> dict[str, Any]:
...

@property
def is_async(self) -> bool:
...

def resolve_dependencies(
self,
context: dict[str, Any] | None = None,
Expand All @@ -209,6 +202,13 @@ def resolve_dependencies(
)
return self._cached_dependencies

def type_hints(self, context: dict[str, Any] | None) -> dict[str, Any]:
...

@property
def is_async(self) -> bool:
...

@functools.cached_property
def is_generator(self) -> bool:
return is_context_manager_function(self.impl)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from aioinject.utils import enter_context_maybe, enter_sync_context_maybe
from aioinject._utils import enter_context_maybe, enter_sync_context_maybe


pytestmark = pytest.mark.anyio
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Annotated

from aioinject import Inject
from aioinject.utils import get_inject_annotations
from aioinject._utils import get_inject_annotations


def test_inject_annotations_returns_all_inject_markers() -> None:
Expand Down

0 comments on commit 56327f3

Please sign in to comment.