python-injection 0.10.5.post0__py3-none-any.whl → 0.10.6__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.
@@ -1,5 +1,6 @@
1
1
  from collections.abc import Callable
2
- from typing import Any
2
+ from types import GenericAlias
3
+ from typing import Any, TypeAliasType
3
4
 
4
5
  from injection import Module, mod
5
6
  from injection.exceptions import InjectionError
@@ -11,27 +12,41 @@ if _is_installed("fastapi", __name__):
11
12
  from fastapi import Depends
12
13
 
13
14
 
14
- def Inject[T](cls: type[T] | Any, /, module: Module | None = None) -> Any: # noqa: N802
15
+ def Inject[T]( # noqa: N802
16
+ cls: type[T] | TypeAliasType | GenericAlias,
17
+ /,
18
+ module: Module | None = None,
19
+ *,
20
+ scoped: bool = True,
21
+ ) -> Any:
15
22
  """
16
23
  Declare a FastAPI dependency with `python-injection`.
17
24
  """
18
25
 
19
26
  dependency: InjectionDependency[T] = InjectionDependency(cls, module or mod())
20
- return Depends(dependency)
27
+ return Depends(dependency, use_cache=scoped)
21
28
 
22
29
 
23
30
  class InjectionDependency[T]:
24
- __slots__ = ("__call__",)
31
+ __slots__ = ("__call__", "__class")
25
32
 
26
33
  __call__: Callable[[], T]
34
+ __class: type[T] | TypeAliasType | GenericAlias
27
35
 
28
- def __init__(self, cls: type[T] | Any, module: Module):
36
+ def __init__(self, cls: type[T] | TypeAliasType | GenericAlias, module: Module):
29
37
  lazy_instance = module.get_lazy_instance(cls)
30
- self.__call__ = lambda: self.__ensure(~lazy_instance, cls)
38
+ self.__call__ = lambda: self.__ensure(~lazy_instance)
39
+ self.__class = cls
31
40
 
32
- @staticmethod
33
- def __ensure[_T](instance: _T | None, cls: type[_T] | Any) -> _T:
41
+ def __eq__(self, other: Any) -> bool:
42
+ cls = type(self)
43
+ return isinstance(other, cls) and hash(self) == hash(other)
44
+
45
+ def __hash__(self) -> int:
46
+ return hash((self.__class,))
47
+
48
+ def __ensure(self, instance: T | None) -> T:
34
49
  if instance is None:
35
- raise InjectionError(f"`{cls}` is an unknown dependency.")
50
+ raise InjectionError(f"`{self.__class}` is an unknown dependency.")
36
51
 
37
52
  return instance
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-injection
3
- Version: 0.10.5.post0
3
+ Version: 0.10.6
4
4
  Summary: Fast and easy dependency injection framework.
5
5
  Home-page: https://github.com/100nm/python-injection
6
6
  License: MIT
@@ -12,11 +12,11 @@ injection/_core/module.py,sha256=IdCOKnHOmY5DpJWOTKDjZvlq26UzRFWJVoOb9wPKTUE,253
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=5vocISPsQqJjD4g-QjXMKLa-rHqU1Rpysm2gb4VDI10,1056
15
+ injection/integrations/fastapi.py,sha256=EGkD0dQImmgBAPees9PU0aruB9RCDN3P9J3Xo5eFwUI,1476
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.5.post0.dist-info/METADATA,sha256=p7ZSxYfPxLi7BRZ8a91T3qBxBjYEtx58vgURPUjI3oI,3065
21
- python_injection-0.10.5.post0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
22
- python_injection-0.10.5.post0.dist-info/RECORD,,
20
+ python_injection-0.10.6.dist-info/METADATA,sha256=HhdV3ujOk2Q2ZjURfwfTRl4DEs_Y9SVhFI01wkjcz5w,3059
21
+ python_injection-0.10.6.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
22
+ python_injection-0.10.6.dist-info/RECORD,,