python-injection 0.13.0__py3-none-any.whl → 0.13.1__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
injection/__init__.pyi CHANGED
@@ -46,7 +46,13 @@ class Injectable[T](Protocol):
46
46
  def get_instance(self) -> T: ...
47
47
 
48
48
  class LazyInstance[T]:
49
- def __init__(self, cls: _InputType[T], module: Module = ...) -> None: ...
49
+ def __init__(
50
+ self,
51
+ cls: _InputType[T],
52
+ /,
53
+ default: T = ...,
54
+ module: Module = ...,
55
+ ) -> None: ...
50
56
  @overload
51
57
  def __get__(self, instance: object, owner: type | None = ...) -> T: ...
52
58
  @overload
@@ -10,9 +10,15 @@ class LazyInstance[T]:
10
10
 
11
11
  __value: Invertible[T]
12
12
 
13
- def __init__(self, cls: InputType[T], module: Module | None = None) -> None:
13
+ def __init__(
14
+ self,
15
+ cls: InputType[T],
16
+ /,
17
+ default: T = NotImplemented,
18
+ module: Module | None = None,
19
+ ) -> None:
14
20
  module = module or mod()
15
- self.__value = module.get_lazy_instance(cls, default=NotImplemented)
21
+ self.__value = module.get_lazy_instance(cls, default)
16
22
 
17
23
  def __get__(
18
24
  self,
injection/_core/module.py CHANGED
@@ -445,7 +445,7 @@ class Module(Broker, EventListener):
445
445
  mode: Mode | ModeStr = Mode.get_default(),
446
446
  ) -> Any:
447
447
  def decorator(
448
- wp: Callable[P, T]
448
+ wrapped: Callable[P, T]
449
449
  | Callable[P, Awaitable[T]]
450
450
  | Callable[P, Iterator[T]]
451
451
  | Callable[P, AsyncIterator[T]],
@@ -463,19 +463,19 @@ class Module(Broker, EventListener):
463
463
  | Callable[P, AsyncContextManager[T]]
464
464
  )
465
465
 
466
- if isasyncgenfunction(wp):
467
- hint = get_yield_hint(wp)
466
+ if isasyncgenfunction(wrapped):
467
+ hint = get_yield_hint(wrapped)
468
468
  injectable_class = AsyncCMScopedInjectable
469
- wrapper = asynccontextmanager(wp)
469
+ wrapper = asynccontextmanager(wrapped)
470
470
 
471
- elif isgeneratorfunction(wp):
472
- hint = get_yield_hint(wp)
471
+ elif isgeneratorfunction(wrapped):
472
+ hint = get_yield_hint(wrapped)
473
473
  injectable_class = CMScopedInjectable
474
- wrapper = contextmanager(wp)
474
+ wrapper = contextmanager(wrapped)
475
475
 
476
476
  else:
477
477
  injectable_class = SimpleScopedInjectable
478
- hint = wrapper = wp # type: ignore[assignment]
478
+ hint = wrapper = wrapped # type: ignore[assignment]
479
479
 
480
480
  hints = on if hint is None else (hint, on)
481
481
  self.injectable(
@@ -486,7 +486,7 @@ class Module(Broker, EventListener):
486
486
  on=hints,
487
487
  mode=mode,
488
488
  )
489
- return wp
489
+ return wrapped
490
490
 
491
491
  return decorator
492
492
 
injection/_core/scope.py CHANGED
@@ -3,14 +3,7 @@ from __future__ import annotations
3
3
  from abc import ABC, abstractmethod
4
4
  from collections import defaultdict
5
5
  from collections.abc import AsyncIterator, Iterator, MutableMapping
6
- from contextlib import (
7
- AsyncContextDecorator,
8
- AsyncExitStack,
9
- ContextDecorator,
10
- ExitStack,
11
- asynccontextmanager,
12
- contextmanager,
13
- )
6
+ from contextlib import AsyncExitStack, ExitStack, asynccontextmanager, contextmanager
14
7
  from contextvars import ContextVar
15
8
  from dataclasses import dataclass, field
16
9
  from types import TracebackType
@@ -19,6 +12,7 @@ from typing import (
19
12
  AsyncContextManager,
20
13
  ContextManager,
21
14
  Final,
15
+ NoReturn,
22
16
  Protocol,
23
17
  Self,
24
18
  runtime_checkable,
@@ -163,7 +157,7 @@ class BaseScope[T](Scope, ABC):
163
157
  )
164
158
 
165
159
 
166
- class AsyncScope(AsyncContextDecorator, BaseScope[AsyncExitStack]):
160
+ class AsyncScope(BaseScope[AsyncExitStack]):
167
161
  __slots__ = ()
168
162
 
169
163
  def __init__(self) -> None:
@@ -188,7 +182,7 @@ class AsyncScope(AsyncContextDecorator, BaseScope[AsyncExitStack]):
188
182
  return self.delegate.enter_context(context_manager)
189
183
 
190
184
 
191
- class SyncScope(ContextDecorator, BaseScope[ExitStack]):
185
+ class SyncScope(BaseScope[ExitStack]):
192
186
  __slots__ = ()
193
187
 
194
188
  def __init__(self) -> None:
@@ -206,7 +200,7 @@ class SyncScope(ContextDecorator, BaseScope[ExitStack]):
206
200
  ) -> Any:
207
201
  return self.delegate.__exit__(exc_type, exc_value, traceback)
208
202
 
209
- async def aenter[T](self, context_manager: AsyncContextManager[T]) -> T:
203
+ async def aenter[T](self, context_manager: AsyncContextManager[T]) -> NoReturn:
210
204
  raise ScopeError(
211
205
  "Synchronous scope doesn't support asynchronous context manager."
212
206
  )
@@ -1,4 +1,3 @@
1
- from collections.abc import Awaitable
2
1
  from types import GenericAlias
3
2
  from typing import Any, TypeAliasType
4
3
 
@@ -12,27 +11,17 @@ __all__ = ("Inject",)
12
11
  def Inject[T]( # noqa: N802
13
12
  cls: type[T] | TypeAliasType | GenericAlias,
14
13
  /,
14
+ default: T = NotImplemented,
15
15
  module: Module | None = None,
16
16
  ) -> Any:
17
17
  """
18
18
  Declare a FastAPI dependency with `python-injection`.
19
19
  """
20
20
 
21
- dependency: InjectionDependency[T] = InjectionDependency(cls, module or mod())
22
- return Depends(dependency, use_cache=False)
21
+ module = module or mod()
22
+ lazy_instance = module.aget_lazy_instance(cls, default)
23
23
 
24
+ async def getter() -> T:
25
+ return await lazy_instance
24
26
 
25
- class InjectionDependency[T]:
26
- __slots__ = ("__awaitable",)
27
-
28
- __awaitable: Awaitable[T]
29
-
30
- def __init__(
31
- self,
32
- cls: type[T] | TypeAliasType | GenericAlias,
33
- module: Module,
34
- ) -> None:
35
- self.__awaitable = module.aget_lazy_instance(cls, default=NotImplemented)
36
-
37
- async def __call__(self) -> T:
38
- return await self.__awaitable
27
+ return Depends(getter, use_cache=False)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-injection
3
- Version: 0.13.0
3
+ Version: 0.13.1
4
4
  Summary: Fast and easy dependency injection framework.
5
5
  Project-URL: Repository, https://github.com/100nm/python-injection
6
6
  Author: remimd
@@ -1,14 +1,14 @@
1
1
  injection/__init__.py,sha256=X0vIAoN4MDlhR7YIkup1qHgqbOkwZ3PWgdSi7h-udaM,1049
2
- injection/__init__.pyi,sha256=XlDjgy-gLz9LKBER0478b_9UMatwdQ4p0zKpaPAaF9M,9572
2
+ injection/__init__.pyi,sha256=LXBL7_ihiw6NyU94xWGdXkWwxvzRBoLYBSOv88PV2WM,9640
3
3
  injection/exceptions.py,sha256=T__732aXxWWUz0sKc39ySteyolCS5tpqQC0oCnzUF2E,917
4
4
  injection/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  injection/utils.py,sha256=SZBzmVAwRL-kilS2eRt9MCeNAL4zeaj_7Rm_2NLBzg0,3175
6
6
  injection/_core/__init__.py,sha256=XERocCxCZBxPGIaOR37yeiQOZyvjHQ6a4rgRmlkUSuU,1367
7
- injection/_core/descriptors.py,sha256=y1rFTQdCDIMLVQfuQE8ZkTPlVZKgzvwZ2Y20Si05DwM,662
7
+ injection/_core/descriptors.py,sha256=7fSHlgAqmgR_Uta8KocBapOt1Xyj2dI7RY9ZdoStTzw,726
8
8
  injection/_core/hook.py,sha256=Qv505pr3kjOE6UitftlLh9JKX9OCNqZBRtHbFha1gqM,3130
9
9
  injection/_core/injectables.py,sha256=SApYnP6vG3b1qi_KJx6hkJyPoyTmZfooxqPpOsxWSVI,4482
10
- injection/_core/module.py,sha256=MvXxy5Yrb3tOTgD5qCWwZVgMBzHGVlqtcceYA51dLNk,30550
11
- injection/_core/scope.py,sha256=e6SYiiWZuQluGWxEAT2T-2M6Y67r-89FgGG0rM4HP-s,5673
10
+ injection/_core/module.py,sha256=wOoHW_bImPSwoxi3i8_rFXP_HWFJ_S78UuY_5EIilFQ,30595
11
+ injection/_core/scope.py,sha256=TKOoxCmi2FIoXis0irReVNayDTte4KjLrTq8ZDcrOSk,5583
12
12
  injection/_core/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  injection/_core/common/asynchronous.py,sha256=LlYMde_55osS3r8Sc3Fh5lgPp5UWmIfvyBzUeIDmMiM,1698
14
14
  injection/_core/common/event.py,sha256=XjzV8gxtGlGvzZs_ykvoC60qmdpd3RN08Eiqz5QUwes,1236
@@ -17,9 +17,9 @@ injection/_core/common/key.py,sha256=ghkZD-Y8Moz6SEPNgMh3xgsZUjDVq-XYAmXaCu5VuCA
17
17
  injection/_core/common/lazy.py,sha256=ZKx2O9CCFsF9F0SLM4zb7jSLksJUv-FBLCPlWltMN5k,1398
18
18
  injection/_core/common/type.py,sha256=QbBBhJp7i1p6gLzWX0TgofvfG7yDH-gHfEQcssVZeHo,2186
19
19
  injection/integrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
20
- injection/integrations/fastapi.py,sha256=EYjaQ2RpdPFMj6CDQWg3YYk9Y9wujbdZkm63VRhpTt4,908
20
+ injection/integrations/fastapi.py,sha256=YHSs85_3m6TUVtOwUcV157b3UZJQIw_aXWAg199a-YE,594
21
21
  injection/testing/__init__.py,sha256=ALcKuDYNdslmpgqotZzSWrXAW0kNNFUs9nzfO1ZgIGc,783
22
22
  injection/testing/__init__.pyi,sha256=6rv5NOYHEaiKMd82E1IIc8lFlLV9ttY57DLiMqGTXt8,482
23
- python_injection-0.13.0.dist-info/METADATA,sha256=P_YxmXOtCUAkg8V0pnjikcl9B702McvBakiBjv6AGik,2996
24
- python_injection-0.13.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
25
- python_injection-0.13.0.dist-info/RECORD,,
23
+ python_injection-0.13.1.dist-info/METADATA,sha256=y4ls59BE2tC-G8V8chV5dMl4um6zcLtYOejDr03Z3jQ,2996
24
+ python_injection-0.13.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
25
+ python_injection-0.13.1.dist-info/RECORD,,