Skip to content
Closed
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
22 changes: 21 additions & 1 deletion tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from that_depends import ContextScopes
import typing

import pytest

from that_depends import BaseContainer, ContextScopes, providers
from that_depends.exceptions import TypeNotBoundError
from that_depends.meta import BaseContainerMeta


Expand All @@ -21,3 +26,18 @@ class _Test(metaclass=BaseContainerMeta):
pass

assert _Test.name() == "_Test"


def test_type_provider_cache_invalidates_after_rebinding() -> None:
provider = providers.Object(1).bind(int)

class Container(BaseContainer):
value = provider

assert Container.get_provider_for_type(int) is provider

provider.bind(str)

with pytest.raises(TypeNotBoundError):
Container.get_provider_for_type(int)
assert Container.get_provider_for_type(str) is typing.cast(providers.Object[str], provider)
5 changes: 4 additions & 1 deletion that_depends/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def get_scope(cls) -> ContextScope | None:
"alias",
"default_scope",
"type_provider_cache",
"type_provider_cache_revision",
)

_lock: Lock = Lock()
Expand Down Expand Up @@ -133,8 +134,10 @@ def get_provider_for_type(cls, t: type[T]) -> AbstractProvider[T]:
Provider for the given type.

"""
if not hasattr(cls, "type_provider_cache"):
current_revision = AbstractProvider._get_binding_revision() # noqa: SLF001
if getattr(cls, "type_provider_cache_revision", -1) != current_revision:
cls.type_provider_cache: dict[type[typing.Any], AbstractProvider[typing.Any]] = {}
cls.type_provider_cache_revision = current_revision
if provider := cls.type_provider_cache.get(t):
return typing.cast(AbstractProvider[T], provider)
for provider in cls.get_providers().values():
Expand Down
14 changes: 12 additions & 2 deletions that_depends/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def _resolve_keyword_arguments_sync(
class AbstractProvider(abc.ABC, typing.Generic[T_co]):
"""Base class for all providers."""

_binding_revision: typing.ClassVar[int] = 0
_binding_revision_lock: typing.ClassVar[threading.Lock] = threading.Lock()

def __init__(self) -> None:
"""Create a new provider."""
super().__init__()
Expand All @@ -115,10 +118,17 @@ def bind(self, *types: type, contravariant: bool = False) -> typing_extensions.S
The current provider instance.

"""
self._bindings = set(types)
self._has_contravariant_bindings = contravariant
with AbstractProvider._binding_revision_lock:
self._bindings = set(types)
self._has_contravariant_bindings = contravariant
AbstractProvider._binding_revision += 1
return self

@classmethod
def _get_binding_revision(cls) -> int:
with AbstractProvider._binding_revision_lock:
return AbstractProvider._binding_revision

def _register(self, candidates: typing.Iterable[typing.Any]) -> None:
"""Register current provider as child.

Expand Down
Loading