Skip to content

Commit

Permalink
Merge branch 'master' into async-support
Browse files Browse the repository at this point in the history
# Conflicts:
#	minject/metadata.py
#	minject/registry.py
  • Loading branch information
biniona committed Oct 7, 2024
2 parents 5f655c5 + cb530aa commit 1720159
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
4 changes: 2 additions & 2 deletions minject/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
TypeVar,
)

from .model import DeferredAny, RegistryKey, aresolve_value
from .model import DeferredAny, RegistryKey, aresolve_value, resolve_value
from .types import Kwargs

if TYPE_CHECKING:
Expand Down Expand Up @@ -157,7 +157,7 @@ def _new_object(self) -> T_co:
def _init_object(self, obj: T_co, registry_impl: "Registry") -> None: # type: ignore[misc]
init_kwargs = {}
for name_, value in self._bindings.items():
init_kwargs[name_] = registry_impl._resolve(value)
init_kwargs[name_] = resolve_value(registry_impl, value)

self._cls.__init__(obj, **init_kwargs)

Expand Down
3 changes: 0 additions & 3 deletions minject/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ async def _push_async_context(self, key: Any) -> Any:
).strip()
)

def _resolve(self, value: Resolvable[T]) -> T:
return resolve_value(self, value)

@_synchronized
def close(self) -> None:
"""Close all objects contained in the registry."""
Expand Down
11 changes: 4 additions & 7 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
from collections import Counter
from concurrent.futures import ThreadPoolExecutor, as_completed
from functools import lru_cache
from random import shuffle
from typing import Sequence

Expand Down Expand Up @@ -464,21 +463,19 @@ def test_concurrent_lazy_init(self) -> None:
query_per_class = 2
num_classes = num_queries // query_per_class

@lru_cache(maxsize=None)
def new_type(i):
return type(f"NewType{i}", (), {})
types = [type(f"NewType{i}", (), {}) for i in range(num_classes)]

def lazy_load_object(i):
return self.registry[new_type(i % num_classes)]
return self.registry[types[i % num_classes]]

with ThreadPoolExecutor(max_workers=query_per_class) as executor:
futures = [executor.submit(lazy_load_object, i) for i in range(num_queries)]
results = [future.result() for future in as_completed(futures)]

# group results by object id, and assert that each type is only instantiated once If each type is
# instantiated only once but accessed N times, we would see N objects for each type in the counter.
counter = Counter(map(id, results))
assert all(count == query_per_class for count in counter.values())
for count in Counter(map(id, results)).values():
self.assertEqual(count, query_per_class)


# "Test"/check type hints. These are not meant to be run by the unit test runner, but instead to
Expand Down

0 comments on commit 1720159

Please sign in to comment.