python-injection 0.17.1__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.
@@ -17,8 +17,7 @@ class LazyInstance[T]:
17
17
  default: T = NotImplemented,
18
18
  module: Module | None = None,
19
19
  ) -> None:
20
- module = module or mod()
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,
@@ -48,7 +48,7 @@ class Injectable[T](Protocol):
48
48
 
49
49
 
50
50
  @dataclass(repr=False, eq=False, frozen=True, slots=True)
51
- class SimpleInjectable[T](Injectable[T]):
51
+ class TransientInjectable[T](Injectable[T]):
52
52
  factory: Caller[..., T]
53
53
 
54
54
  async def aget_instance(self) -> T:
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] = SimpleInjectable,
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(self, wrapped, /, threadsafe=False): # type: ignore[no-untyped-def]
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(self, cls, default=None): # type: ignore[no-untyped-def]
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(self, cls, default=None): # type: ignore[no-untyped-def]
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(self, cls, default=None, *, cache=False): # type: ignore[no-untyped-def]
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(self, cls, default=None, *, cache=False): # type: ignore[no-untyped-def]
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=...): # type: ignore[no-untyped-def]
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
@@ -18,25 +18,23 @@ class FastAPIInject:
18
18
  default: T = NotImplemented,
19
19
  module: Module | None = None,
20
20
  ) -> Any:
21
- module = module or mod()
22
- lazy_instance = module.aget_lazy_instance(cls, default)
21
+ ainstance = (module or mod()).aget_lazy_instance(cls, default)
23
22
 
24
- async def getter() -> T:
25
- return await lazy_instance
23
+ async def dependency() -> T:
24
+ return await ainstance
26
25
 
27
- return Depends(getter, use_cache=False)
26
+ class_name = getattr(cls, "__name__", str(cls))
27
+ dependency.__name__ = f"inject({class_name})"
28
+ return Depends(dependency, use_cache=False)
28
29
 
29
30
  def __getitem__(self, params: Any, /) -> Any:
30
- if not isinstance(params, tuple):
31
- params = (params,)
32
-
33
- iter_params = iter(params)
31
+ iter_params = iter(params if isinstance(params, tuple) else (params,))
34
32
  cls = next(iter_params)
35
33
  return Annotated[cls, self(cls), *iter_params]
36
34
 
37
35
 
38
36
  if TYPE_CHECKING:
39
- type Inject[T, *Args] = Annotated[T, Depends(), *Args]
37
+ type Inject[T, *Metadata] = Annotated[T, Depends(...), *Metadata]
40
38
 
41
39
  else:
42
40
  Inject = FastAPIInject()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-injection
3
- Version: 0.17.1
3
+ Version: 0.17.2
4
4
  Summary: Fast and easy dependency injection framework.
5
5
  Project-URL: Repository, https://github.com/100nm/python-injection
6
6
  Author: remimd
@@ -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=7fSHlgAqmgR_Uta8KocBapOt1Xyj2dI7RY9ZdoStTzw,726
8
- injection/_core/injectables.py,sha256=yeW_AHCZSsxyiN1oT_E9MLc1DcYA37XlmQGAkUc4eR8,6189
9
- injection/_core/module.py,sha256=GNDnYbD7oXVCS_CH_w7Io-rbWZxMConoOMNy1XJKotY,31575
10
- injection/_core/scope.py,sha256=J6O0fgaz7IstYDx1hjRPaZUNIzIS_cHc29fp-qIrUA0,8291
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=1TSVd5zOdpugk4PtWK13t4V-CsfERBlt6ZwG_ML2kuk,1013
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.1.dist-info/METADATA,sha256=KHngttE3jix1NZK_YgGmDSZOd6JXDWMb0UygpY5tRpM,3199
24
- python_injection-0.17.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
25
- python_injection-0.17.1.dist-info/RECORD,,
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,,