Skip to content

Commit f5b6378

Browse files
authored
feat: ✨ Custom default value for get_instance and get_lazy_instance
1 parent cc5bdf2 commit f5b6378

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

injection/__init__.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,23 @@ class Module:
136136
parameter or an exception will be raised.
137137
"""
138138

139-
def get_instance[T](self, cls: _InputType[T]) -> T | None:
139+
def get_instance[T, Default](
140+
self,
141+
cls: _InputType[T],
142+
default: Default | None = ...,
143+
) -> T | Default | None:
140144
"""
141145
Function used to retrieve an instance associated with the type passed in
142146
parameter or return `None`.
143147
"""
144148

145-
def get_lazy_instance[T](
149+
def get_lazy_instance[T, Default](
146150
self,
147151
cls: _InputType[T],
152+
default: Default | None = ...,
148153
*,
149154
cache: bool = ...,
150-
) -> _Invertible[T | None]:
155+
) -> _Invertible[T | Default | None]:
151156
"""
152157
Function used to retrieve an instance associated with the type passed in
153158
parameter or `None`. Return a `Invertible` object. To access the instance

injection/_core/module.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -572,22 +572,27 @@ def find_instance[T](self, cls: InputType[T]) -> T:
572572
injectable = self[cls]
573573
return injectable.get_instance()
574574

575-
def get_instance[T](self, cls: InputType[T]) -> T | None:
575+
def get_instance[T, Default](
576+
self,
577+
cls: InputType[T],
578+
default: Default | None = None,
579+
) -> T | Default | None:
576580
try:
577581
return self.find_instance(cls)
578582
except KeyError:
579-
return None
583+
return default
580584

581-
def get_lazy_instance[T](
585+
def get_lazy_instance[T, Default](
582586
self,
583587
cls: InputType[T],
588+
default: Default | None = None,
584589
*,
585590
cache: bool = False,
586-
) -> Invertible[T | None]:
591+
) -> Invertible[T | Default | None]:
587592
if cache:
588-
return Lazy(lambda: self.get_instance(cls))
593+
return Lazy(lambda: self.get_instance(cls, default))
589594

590-
function = self.inject(lambda instance=None: instance)
595+
function = self.inject(lambda instance=default: instance)
591596
function.__injected__.set_owner(cls)
592597
return SimpleInvertible(function)
593598

injection/integrations/fastapi.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections.abc import Callable
22
from types import GenericAlias
3-
from typing import Any, TypeAliasType
3+
from typing import Any, ClassVar, TypeAliasType
44

55
from injection import Module, mod
66
from injection.exceptions import InjectionError
@@ -33,8 +33,10 @@ class InjectionDependency[T]:
3333
__call__: Callable[[], T]
3434
__class: type[T] | TypeAliasType | GenericAlias
3535

36+
__sentinel: ClassVar[object] = object()
37+
3638
def __init__(self, cls: type[T] | TypeAliasType | GenericAlias, module: Module):
37-
lazy_instance = module.get_lazy_instance(cls)
39+
lazy_instance = module.get_lazy_instance(cls, default=self.__sentinel)
3840
self.__call__ = lambda: self.__ensure(~lazy_instance)
3941
self.__class = cls
4042

@@ -47,8 +49,8 @@ def __eq__(self, other: Any) -> bool:
4749
def __hash__(self) -> int:
4850
return hash((self.__class,))
4951

50-
def __ensure(self, instance: T | None) -> T:
51-
if instance is None:
52+
def __ensure(self, instance: T | Any) -> T:
53+
if instance is self.__sentinel:
5254
raise InjectionError(f"`{self.__class}` is an unknown dependency.")
5355

5456
return instance

0 commit comments

Comments
 (0)