python-injection 0.10.8.post0__py3-none-any.whl → 0.10.9__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/__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](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:
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
@@ -34,7 +34,7 @@ class Hook[**P, T]:
34
34
  return iter(self.__functions)
35
35
 
36
36
  def add(self, *functions: HookFunction[P, T]) -> Self:
37
- self.__functions.extend(functions)
37
+ self.__functions.extend(reversed(functions))
38
38
  return self
39
39
 
40
40
  @classmethod
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](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:
576
580
  try:
577
581
  return self.find_instance(cls)
578
582
  except KeyError:
579
- return None
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=None: 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, TypeAliasType
4
4
 
5
5
  from injection import Module, mod
6
6
  from injection.exceptions import InjectionError
@@ -33,8 +33,10 @@ class InjectionDependency[T]:
33
33
  __call__: Callable[[], T]
34
34
  __class: type[T] | TypeAliasType | GenericAlias
35
35
 
36
+ __sentinel: ClassVar[object] = object()
37
+
36
38
  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)
38
40
  self.__call__ = lambda: self.__ensure(~lazy_instance)
39
41
  self.__class = cls
40
42
 
@@ -47,8 +49,8 @@ class InjectionDependency[T]:
47
49
  def __hash__(self) -> int:
48
50
  return hash((self.__class,))
49
51
 
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:
52
54
  raise InjectionError(f"`{self.__class}` is an unknown dependency.")
53
55
 
54
56
  return instance
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-injection
3
- Version: 0.10.8.post0
3
+ Version: 0.10.9
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=U0QxTtcDon_eY_AqDxRx5n-ui4QDm8DGolxtdjOVGbg,7169
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=p9pC1zb9tDZykHs5HGM5VpRxWyvuajC45vilvkvatkY,2999
11
- injection/_core/module.py,sha256=ckiLJR3oMf5V2Iy4q0Ywz9vnqMpL7nzViKS-aDBUDLU,25087
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=GT0z_oBAW0bgUePRhb7v89BjB6AZqggggFcn6EJTdSA,1501
15
+ injection/integrations/fastapi.py,sha256=eg5m6pYJiblQvxJ8sVDgz-jH9MSrWju0wEMRmr4cVD8,1591
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.8.post0.dist-info/METADATA,sha256=OwL5RHT0OAuDz_i5veAW6pCacRAB13r_XGH9n45cPGs,3065
21
- python_injection-0.10.8.post0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
22
- python_injection-0.10.8.post0.dist-info/RECORD,,
20
+ python_injection-0.10.9.dist-info/METADATA,sha256=js6fr8O78PiY6UNGN2iG1ZsoFKm-jnklJHgTWM2QSKA,3110
21
+ python_injection-0.10.9.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
22
+ python_injection-0.10.9.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.0
2
+ Generator: poetry-core 1.9.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any