python-injection 0.17.0__py3-none-any.whl → 0.17.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- injection/_core/descriptors.py +1 -2
- injection/_core/injectables.py +1 -1
- injection/_core/module.py +33 -10
- injection/_core/scope.py +1 -1
- injection/ext/fastapi.py +30 -15
- {python_injection-0.17.0.dist-info → python_injection-0.17.2.dist-info}/METADATA +1 -1
- {python_injection-0.17.0.dist-info → python_injection-0.17.2.dist-info}/RECORD +8 -8
- {python_injection-0.17.0.dist-info → python_injection-0.17.2.dist-info}/WHEEL +0 -0
injection/_core/descriptors.py
CHANGED
@@ -17,8 +17,7 @@ class LazyInstance[T]:
|
|
17
17
|
default: T = NotImplemented,
|
18
18
|
module: Module | None = None,
|
19
19
|
) -> None:
|
20
|
-
|
21
|
-
self.__value = module.get_lazy_instance(cls, default)
|
20
|
+
self.__value = (module or mod()).get_lazy_instance(cls, default)
|
22
21
|
|
23
22
|
def __get__(
|
24
23
|
self,
|
injection/_core/injectables.py
CHANGED
injection/_core/module.py
CHANGED
@@ -66,9 +66,9 @@ from injection._core.injectables import (
|
|
66
66
|
ScopedInjectable,
|
67
67
|
ScopedSlotInjectable,
|
68
68
|
ShouldBeInjectable,
|
69
|
-
SimpleInjectable,
|
70
69
|
SimpleScopedInjectable,
|
71
70
|
SingletonInjectable,
|
71
|
+
TransientInjectable,
|
72
72
|
)
|
73
73
|
from injection._core.slots import SlotKey
|
74
74
|
from injection.exceptions import (
|
@@ -430,7 +430,7 @@ class Module(Broker, EventListener):
|
|
430
430
|
wrapped: Recipe[P, T] | None = None,
|
431
431
|
/,
|
432
432
|
*,
|
433
|
-
cls: InjectableFactory[T] =
|
433
|
+
cls: InjectableFactory[T] = TransientInjectable,
|
434
434
|
ignore_type_hint: bool = False,
|
435
435
|
inject: bool = True,
|
436
436
|
on: TypeInfo[T] = (),
|
@@ -583,7 +583,12 @@ class Module(Broker, EventListener):
|
|
583
583
|
threadsafe: bool = ...,
|
584
584
|
) -> AsyncInjectedFunction[P, T]: ...
|
585
585
|
|
586
|
-
def make_injected_function
|
586
|
+
def make_injected_function[**P, T](
|
587
|
+
self,
|
588
|
+
wrapped: Callable[P, T],
|
589
|
+
/,
|
590
|
+
threadsafe: bool = False,
|
591
|
+
) -> InjectedFunction[P, T]:
|
587
592
|
metadata = InjectMetadata(wrapped, threadsafe)
|
588
593
|
|
589
594
|
@metadata.task
|
@@ -592,7 +597,7 @@ class Module(Broker, EventListener):
|
|
592
597
|
self.add_listener(metadata)
|
593
598
|
|
594
599
|
if iscoroutinefunction(wrapped):
|
595
|
-
return AsyncInjectedFunction(metadata)
|
600
|
+
return AsyncInjectedFunction(metadata) # type: ignore[arg-type, return-value]
|
596
601
|
|
597
602
|
return SyncInjectedFunction(metadata)
|
598
603
|
|
@@ -630,7 +635,11 @@ class Module(Broker, EventListener):
|
|
630
635
|
default: None = ...,
|
631
636
|
) -> T | None: ...
|
632
637
|
|
633
|
-
async def aget_instance
|
638
|
+
async def aget_instance[T, Default](
|
639
|
+
self,
|
640
|
+
cls: InputType[T],
|
641
|
+
default: Default | None = None,
|
642
|
+
) -> T | Default | None:
|
634
643
|
try:
|
635
644
|
return await self.afind_instance(cls)
|
636
645
|
except (KeyError, SkipInjectable):
|
@@ -650,7 +659,11 @@ class Module(Broker, EventListener):
|
|
650
659
|
default: None = ...,
|
651
660
|
) -> T | None: ...
|
652
661
|
|
653
|
-
def get_instance
|
662
|
+
def get_instance[T, Default](
|
663
|
+
self,
|
664
|
+
cls: InputType[T],
|
665
|
+
default: Default | None = None,
|
666
|
+
) -> T | Default | None:
|
654
667
|
try:
|
655
668
|
return self.find_instance(cls)
|
656
669
|
except (KeyError, SkipInjectable):
|
@@ -674,7 +687,13 @@ class Module(Broker, EventListener):
|
|
674
687
|
cache: bool = ...,
|
675
688
|
) -> Awaitable[T | None]: ...
|
676
689
|
|
677
|
-
def aget_lazy_instance
|
690
|
+
def aget_lazy_instance[T, Default](
|
691
|
+
self,
|
692
|
+
cls: InputType[T],
|
693
|
+
default: Default | None = None,
|
694
|
+
*,
|
695
|
+
cache: bool = False,
|
696
|
+
) -> Awaitable[T | Default | None]:
|
678
697
|
if cache:
|
679
698
|
return alazy(lambda: self.aget_instance(cls, default))
|
680
699
|
|
@@ -700,7 +719,13 @@ class Module(Broker, EventListener):
|
|
700
719
|
cache: bool = ...,
|
701
720
|
) -> Invertible[T | None]: ...
|
702
721
|
|
703
|
-
def get_lazy_instance
|
722
|
+
def get_lazy_instance[T, Default](
|
723
|
+
self,
|
724
|
+
cls: InputType[T],
|
725
|
+
default: Default | None = None,
|
726
|
+
*,
|
727
|
+
cache: bool = False,
|
728
|
+
) -> Invertible[T | Default | None]:
|
704
729
|
if cache:
|
705
730
|
return lazy(lambda: self.get_instance(cls, default))
|
706
731
|
|
@@ -790,8 +815,6 @@ class Module(Broker, EventListener):
|
|
790
815
|
|
791
816
|
self.unlock().init_modules(*modules)
|
792
817
|
|
793
|
-
del module, modules
|
794
|
-
|
795
818
|
@contextmanager
|
796
819
|
def cleaner() -> Iterator[Self]:
|
797
820
|
yield self
|
injection/_core/scope.py
CHANGED
@@ -163,7 +163,7 @@ def get_scope(name: str, default: EllipsisType = ...) -> Scope: ...
|
|
163
163
|
def get_scope[T](name: str, default: T) -> Scope | T: ...
|
164
164
|
|
165
165
|
|
166
|
-
def get_scope(name, default
|
166
|
+
def get_scope[T](name: str, default: T | EllipsisType = ...) -> Scope | T:
|
167
167
|
for states in (__CONTEXTUAL_SCOPES, __SHARED_SCOPES):
|
168
168
|
state = states.get(name)
|
169
169
|
if state and (scope := state.get_scope()):
|
injection/ext/fastapi.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
from types import GenericAlias
|
2
|
-
from typing import Any, TypeAliasType
|
2
|
+
from typing import TYPE_CHECKING, Annotated, Any, TypeAliasType
|
3
3
|
|
4
4
|
from fastapi import Depends
|
5
5
|
|
@@ -8,20 +8,35 @@ from injection import Module, mod
|
|
8
8
|
__all__ = ("Inject",)
|
9
9
|
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
/,
|
14
|
-
default: T = NotImplemented,
|
15
|
-
module: Module | None = None,
|
16
|
-
) -> Any:
|
17
|
-
"""
|
18
|
-
Declare a FastAPI dependency with `python-injection`.
|
19
|
-
"""
|
11
|
+
class FastAPIInject:
|
12
|
+
__slots__ = ()
|
20
13
|
|
21
|
-
|
22
|
-
|
14
|
+
def __call__[T](
|
15
|
+
self,
|
16
|
+
cls: type[T] | TypeAliasType | GenericAlias,
|
17
|
+
/,
|
18
|
+
default: T = NotImplemented,
|
19
|
+
module: Module | None = None,
|
20
|
+
) -> Any:
|
21
|
+
ainstance = (module or mod()).aget_lazy_instance(cls, default)
|
23
22
|
|
24
|
-
|
25
|
-
|
23
|
+
async def dependency() -> T:
|
24
|
+
return await ainstance
|
26
25
|
|
27
|
-
|
26
|
+
class_name = getattr(cls, "__name__", str(cls))
|
27
|
+
dependency.__name__ = f"inject({class_name})"
|
28
|
+
return Depends(dependency, use_cache=False)
|
29
|
+
|
30
|
+
def __getitem__(self, params: Any, /) -> Any:
|
31
|
+
iter_params = iter(params if isinstance(params, tuple) else (params,))
|
32
|
+
cls = next(iter_params)
|
33
|
+
return Annotated[cls, self(cls), *iter_params]
|
34
|
+
|
35
|
+
|
36
|
+
if TYPE_CHECKING:
|
37
|
+
type Inject[T, *Metadata] = Annotated[T, Depends(...), *Metadata]
|
38
|
+
|
39
|
+
else:
|
40
|
+
Inject = FastAPIInject()
|
41
|
+
|
42
|
+
del FastAPIInject
|
@@ -4,10 +4,10 @@ injection/exceptions.py,sha256=v57yMujiq6H_zwwn30A8UYEZX9R9k-bY8FnsdaimPM4,1025
|
|
4
4
|
injection/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
5
|
injection/utils.py,sha256=bLIVA_3N3mTEQ3kGV4YzrWEnokHxUGqWYNKPggOOnpg,4065
|
6
6
|
injection/_core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
7
|
-
injection/_core/descriptors.py,sha256=
|
8
|
-
injection/_core/injectables.py,sha256=
|
9
|
-
injection/_core/module.py,sha256=
|
10
|
-
injection/_core/scope.py,sha256=
|
7
|
+
injection/_core/descriptors.py,sha256=jH0pyIlPurMmU4yXr-HKS_7BJ-9d0XUvEx4pQre3QeI,704
|
8
|
+
injection/_core/injectables.py,sha256=Rg1nxDkbcpeX4ELohrNVMguPhN36SNQuD0JKfyfL6bI,6192
|
9
|
+
injection/_core/module.py,sha256=7jTp3dHHQqJ_b95iHHMp9TKRF794Msgla7UfBsLysV8,31988
|
10
|
+
injection/_core/scope.py,sha256=OBzVY1mUApryqIZKQtwHz7wiuY13MfouyaHp50DpWeQ,8300
|
11
11
|
injection/_core/slots.py,sha256=g9TG6CbqRzCsjg01iPyfRtTTUCJnnJOwcj9mJabH0dc,37
|
12
12
|
injection/_core/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
13
13
|
injection/_core/common/asynchronous.py,sha256=QeS2Lc4gEBFvTA_snOWfme5mTL4BFZWqZ8EzJwOdVos,1816
|
@@ -17,9 +17,9 @@ injection/_core/common/key.py,sha256=ghkZD-Y8Moz6SEPNgMh3xgsZUjDVq-XYAmXaCu5VuCA
|
|
17
17
|
injection/_core/common/lazy.py,sha256=6xh5h0lmaNvl32V0WoX4VCTsNJ3zUJdWVqpLJ_YeIIU,1363
|
18
18
|
injection/_core/common/type.py,sha256=SCDtmBv9qFvEf5o5tTgCuwMDfuo1fgjSW0bUqA8ACis,2251
|
19
19
|
injection/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
20
|
-
injection/ext/fastapi.py,sha256=
|
20
|
+
injection/ext/fastapi.py,sha256=Kl-gei0zjFL_oeoOqAOnj7St3Z_5MZlxcKSmuR4g1is,1082
|
21
21
|
injection/testing/__init__.py,sha256=SiImXDd0-DO1a8S5nbUQRtgDX8iaU_nHcp8DdqwtD2M,896
|
22
22
|
injection/testing/__init__.pyi,sha256=iOii0i9F5n7znltGeGQYI2KXC_if9SAogLh1h03yx-0,540
|
23
|
-
python_injection-0.17.
|
24
|
-
python_injection-0.17.
|
25
|
-
python_injection-0.17.
|
23
|
+
python_injection-0.17.2.dist-info/METADATA,sha256=lJqNL0UMAVNRNxj6CodPgVZuCGVoU3VjZThGAGHn6c0,3199
|
24
|
+
python_injection-0.17.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
25
|
+
python_injection-0.17.2.dist-info/RECORD,,
|
File without changes
|