python-injection 0.10.8.post0__py3-none-any.whl → 0.10.10__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- injection/__init__.pyi +8 -3
- injection/_core/hook.py +1 -1
- injection/_core/module.py +11 -6
- injection/integrations/fastapi.py +17 -7
- {python_injection-0.10.8.post0.dist-info → python_injection-0.10.10.dist-info}/METADATA +2 -1
- {python_injection-0.10.8.post0.dist-info → python_injection-0.10.10.dist-info}/RECORD +7 -7
- {python_injection-0.10.8.post0.dist-info → python_injection-0.10.10.dist-info}/WHEEL +1 -1
injection/__init__.pyi
CHANGED
@@ -136,18 +136,23 @@ class Module:
|
|
136
136
|
parameter or an exception will be raised.
|
137
137
|
"""
|
138
138
|
|
139
|
-
def get_instance[T
|
139
|
+
def get_instance[T, Default](
|
140
|
+
self,
|
141
|
+
cls: _InputType[T],
|
142
|
+
default: Default | None = ...,
|
143
|
+
) -> T | Default | None:
|
140
144
|
"""
|
141
145
|
Function used to retrieve an instance associated with the type passed in
|
142
146
|
parameter or return `None`.
|
143
147
|
"""
|
144
148
|
|
145
|
-
def get_lazy_instance[T](
|
149
|
+
def get_lazy_instance[T, Default](
|
146
150
|
self,
|
147
151
|
cls: _InputType[T],
|
152
|
+
default: Default | None = ...,
|
148
153
|
*,
|
149
154
|
cache: bool = ...,
|
150
|
-
) -> _Invertible[T | None]:
|
155
|
+
) -> _Invertible[T | Default | None]:
|
151
156
|
"""
|
152
157
|
Function used to retrieve an instance associated with the type passed in
|
153
158
|
parameter or `None`. Return a `Invertible` object. To access the instance
|
injection/_core/hook.py
CHANGED
injection/_core/module.py
CHANGED
@@ -572,22 +572,27 @@ class Module(Broker, EventListener):
|
|
572
572
|
injectable = self[cls]
|
573
573
|
return injectable.get_instance()
|
574
574
|
|
575
|
-
def get_instance[T
|
575
|
+
def get_instance[T, Default](
|
576
|
+
self,
|
577
|
+
cls: InputType[T],
|
578
|
+
default: Default | None = None,
|
579
|
+
) -> T | Default | None:
|
576
580
|
try:
|
577
581
|
return self.find_instance(cls)
|
578
582
|
except KeyError:
|
579
|
-
return
|
583
|
+
return default
|
580
584
|
|
581
|
-
def get_lazy_instance[T](
|
585
|
+
def get_lazy_instance[T, Default](
|
582
586
|
self,
|
583
587
|
cls: InputType[T],
|
588
|
+
default: Default | None = None,
|
584
589
|
*,
|
585
590
|
cache: bool = False,
|
586
|
-
) -> Invertible[T | None]:
|
591
|
+
) -> Invertible[T | Default | None]:
|
587
592
|
if cache:
|
588
|
-
return Lazy(lambda: self.get_instance(cls))
|
593
|
+
return Lazy(lambda: self.get_instance(cls, default))
|
589
594
|
|
590
|
-
function = self.inject(lambda instance=
|
595
|
+
function = self.inject(lambda instance=default: instance)
|
591
596
|
function.__injected__.set_owner(cls)
|
592
597
|
return SimpleInvertible(function)
|
593
598
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
from collections.abc import Callable
|
2
2
|
from types import GenericAlias
|
3
|
-
from typing import Any, TypeAliasType
|
3
|
+
from typing import Any, ClassVar, Self, TypeAliasType
|
4
4
|
|
5
5
|
from injection import Module, mod
|
6
6
|
from injection.exceptions import InjectionError
|
@@ -28,27 +28,37 @@ def Inject[T]( # noqa: N802
|
|
28
28
|
|
29
29
|
|
30
30
|
class InjectionDependency[T]:
|
31
|
-
__slots__ = ("__call__", "__class")
|
31
|
+
__slots__ = ("__call__", "__class", "__module")
|
32
32
|
|
33
33
|
__call__: Callable[[], T]
|
34
34
|
__class: type[T] | TypeAliasType | GenericAlias
|
35
|
+
__module: Module
|
36
|
+
|
37
|
+
__sentinel: ClassVar[object] = object()
|
35
38
|
|
36
39
|
def __init__(self, cls: type[T] | TypeAliasType | GenericAlias, module: Module):
|
37
|
-
lazy_instance = module.get_lazy_instance(cls)
|
40
|
+
lazy_instance = module.get_lazy_instance(cls, default=self.__sentinel)
|
38
41
|
self.__call__ = lambda: self.__ensure(~lazy_instance)
|
39
42
|
self.__class = cls
|
43
|
+
self.__module = module
|
40
44
|
|
41
45
|
def __eq__(self, other: Any) -> bool:
|
42
46
|
if isinstance(other, type(self)):
|
43
|
-
return
|
47
|
+
return self.__key == other.__key
|
44
48
|
|
45
49
|
return NotImplemented
|
46
50
|
|
47
51
|
def __hash__(self) -> int:
|
48
|
-
return hash(
|
52
|
+
return hash(self.__key)
|
53
|
+
|
54
|
+
@property
|
55
|
+
def __key(
|
56
|
+
self,
|
57
|
+
) -> tuple[type[Self], type[T] | TypeAliasType | GenericAlias, Module]:
|
58
|
+
return type(self), self.__class, self.__module
|
49
59
|
|
50
|
-
def __ensure(self, instance: T |
|
51
|
-
if instance is
|
60
|
+
def __ensure(self, instance: T | Any) -> T:
|
61
|
+
if instance is self.__sentinel:
|
52
62
|
raise InjectionError(f"`{self.__class}` is an unknown dependency.")
|
53
63
|
|
54
64
|
return instance
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: python-injection
|
3
|
-
Version: 0.10.
|
3
|
+
Version: 0.10.10
|
4
4
|
Summary: Fast and easy dependency injection framework.
|
5
5
|
Home-page: https://github.com/100nm/python-injection
|
6
6
|
License: MIT
|
@@ -15,6 +15,7 @@ Classifier: Operating System :: OS Independent
|
|
15
15
|
Classifier: Programming Language :: Python
|
16
16
|
Classifier: Programming Language :: Python :: 3
|
17
17
|
Classifier: Programming Language :: Python :: 3.12
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
18
19
|
Classifier: Programming Language :: Python :: 3 :: Only
|
19
20
|
Classifier: Topic :: Software Development :: Libraries
|
20
21
|
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
@@ -1,5 +1,5 @@
|
|
1
1
|
injection/__init__.py,sha256=6zHo40kXRsCruG23gMllEymAQ3crQkE_8Q-wV78iomU,769
|
2
|
-
injection/__init__.pyi,sha256=
|
2
|
+
injection/__init__.pyi,sha256=kYMUV96CusOcz9RvkgQiii0QaKwWz8160qeJ3mrwPgY,7308
|
3
3
|
injection/_core/__init__.py,sha256=VMGLfdu0gYh82mt7zS297rQ7CE_gHVy0gRdI8RY_ZLY,1361
|
4
4
|
injection/_core/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
5
|
injection/_core/common/event.py,sha256=JYwe528g0uzUSo7l-iXjzuziYk4xlO6Cakkm83xkg38,1290
|
@@ -7,16 +7,16 @@ injection/_core/common/invertible.py,sha256=QYXMqLrkAkz_7mq-jEYKtBr1CQ5aqzplP0FG
|
|
7
7
|
injection/_core/common/lazy.py,sha256=kCO1q4S6AdBhsP5RrihBJpgfeR4hxvMqSz1cpCgBdjo,1482
|
8
8
|
injection/_core/common/threading.py,sha256=OXm7L3p8c7O7eSkU-RTR7cobqIGMhuo-7gpDXsWKDNQ,214
|
9
9
|
injection/_core/common/type.py,sha256=TQTD-f_rnAHS0VgfkWxNFU8HAWPvkAktNDQ9_23JLHM,1705
|
10
|
-
injection/_core/hook.py,sha256=
|
11
|
-
injection/_core/module.py,sha256=
|
10
|
+
injection/_core/hook.py,sha256=_TcwhF_DONfcoBz58RxVLeA950Rs8wtZSLGepZwGBRk,3009
|
11
|
+
injection/_core/module.py,sha256=c3YGhgSsZ8Cu5x-xaXHqJZEi3niSwH7HCVu6tj0IU0k,25243
|
12
12
|
injection/exceptions.py,sha256=-5Shs7R5rctQXhpMLfcjiMBCzrtFWxC88qETUIHz57s,692
|
13
13
|
injection/integrations/__init__.py,sha256=NYLcstr4ESdLj326LlDub143z6JGM1z1pCOVWhBXK10,304
|
14
14
|
injection/integrations/blacksheep.py,sha256=yO5gLb_l4W3bNPFt-v2qWIL9R8PNon4JmOxQEHdi-5o,923
|
15
|
-
injection/integrations/fastapi.py,sha256=
|
15
|
+
injection/integrations/fastapi.py,sha256=FUX8u4yq1MNThlqv0g-e9pxh_aG6wEvcw9Yccn9aJL8,1831
|
16
16
|
injection/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
17
17
|
injection/testing/__init__.py,sha256=-C38gmZJwDtLDAWJhqiaosOZWQZwwFa1M34tODcrASs,747
|
18
18
|
injection/testing/__init__.pyi,sha256=6ZXbbS-9ppMdkxd03I6yBNurmR3Xw7sM_qiokibkLeY,386
|
19
19
|
injection/utils.py,sha256=gPcxGIdrGz4irbJXGTYPw33jNy8jg56u_c61eb1MBSE,1971
|
20
|
-
python_injection-0.10.
|
21
|
-
python_injection-0.10.
|
22
|
-
python_injection-0.10.
|
20
|
+
python_injection-0.10.10.dist-info/METADATA,sha256=8zGiqdSQp_K17U5o5PlArBHTT4tkkBpU8e9lQ4qLIx4,3111
|
21
|
+
python_injection-0.10.10.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
22
|
+
python_injection-0.10.10.dist-info/RECORD,,
|