Skip to content

Commit 8a296f7

Browse files
committed
wip
1 parent 99489af commit 8a296f7

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/dependency_injector/containers.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ class WiringConfiguration:
3030
packages: List[Any]
3131
from_package: Optional[str]
3232
auto_wire: bool
33+
keep_cache: bool
3334
def __init__(
3435
self,
3536
modules: Optional[Iterable[Any]] = None,
3637
packages: Optional[Iterable[Any]] = None,
3738
from_package: Optional[str] = None,
3839
auto_wire: bool = True,
40+
keep_cache: bool = False,
3941
) -> None: ...
4042

4143
class Container:

src/dependency_injector/containers.pyx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ from .wiring import wire, unwire
2020
class WiringConfiguration:
2121
"""Container wiring configuration."""
2222

23-
def __init__(self, modules=None, packages=None, from_package=None, auto_wire=True):
23+
def __init__(self, modules=None, packages=None, from_package=None, auto_wire=True, keep_cache=False):
2424
self.modules = [*modules] if modules else []
2525
self.packages = [*packages] if packages else []
2626
self.from_package = from_package
2727
self.auto_wire = auto_wire
28+
self.keep_cache = keep_cache
2829

2930
def __deepcopy__(self, memo=None):
30-
return self.__class__(self.modules, self.packages, self.from_package, self.auto_wire)
31+
return self.__class__(self.modules, self.packages, self.from_package, self.auto_wire, self.keep_cache)
3132

3233

3334
class Container:
@@ -258,7 +259,7 @@ class DynamicContainer(Container):
258259
"""Check if auto wiring is needed."""
259260
return self.wiring_config.auto_wire is True
260261

261-
def wire(self, modules=None, packages=None, from_package=None):
262+
def wire(self, modules=None, packages=None, from_package=None, keep_cache=None):
262263
"""Wire container providers with provided packages and modules.
263264
264265
:rtype: None
@@ -289,10 +290,14 @@ class DynamicContainer(Container):
289290
if not modules and not packages:
290291
return
291292

293+
if keep_cache is None:
294+
keep_cache = self.wiring_config.keep_cache
295+
292296
wire(
293297
container=self,
294298
modules=modules,
295299
packages=packages,
300+
keep_cache=keep_cache,
296301
)
297302

298303
if modules:

src/dependency_injector/wiring.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
cast,
2424
)
2525

26+
try:
27+
from functools import cache
28+
except ImportError:
29+
from functools import lru_cache
30+
31+
cache = lru_cache(maxsize=None)
32+
2633

2734
# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/362
2835
if sys.version_info >= (3, 9):
@@ -404,6 +411,7 @@ def wire( # noqa: C901
404411
*,
405412
modules: Optional[Iterable[ModuleType]] = None,
406413
packages: Optional[Iterable[ModuleType]] = None,
414+
keep_cache: bool = False,
407415
) -> None:
408416
"""Wire container providers with provided packages and modules."""
409417
modules = [*modules] if modules else []
@@ -444,6 +452,9 @@ def wire( # noqa: C901
444452
for patched in _patched_registry.get_callables_from_module(module):
445453
_bind_injections(patched, providers_map)
446454

455+
if not keep_cache:
456+
clear_cache()
457+
447458

448459
def unwire( # noqa: C901
449460
*,
@@ -599,6 +610,7 @@ def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
599610
return marker
600611

601612

613+
@cache
602614
def _fetch_reference_injections( # noqa: C901
603615
fn: Callable[..., Any],
604616
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
@@ -1049,3 +1061,8 @@ def _get_members_and_annotated(obj: Any) -> Iterable[Tuple[str, Any]]:
10491061
member = args[1]
10501062
members.append((annotation_name, member))
10511063
return members
1064+
1065+
1066+
def clear_cache() -> None:
1067+
"""Clear all caches used by :func:`wire`."""
1068+
_fetch_reference_injections.cache_clear()

0 commit comments

Comments
 (0)