python-injection 0.10.5.post0__tar.gz → 0.10.6__tar.gz
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.
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/PKG-INFO +1 -1
- python_injection-0.10.6/injection/integrations/fastapi.py +52 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/pyproject.toml +1 -1
- python_injection-0.10.5.post0/injection/integrations/fastapi.py +0 -37
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/README.md +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/__init__.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/__init__.pyi +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/__init__.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/__init__.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/event.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/invertible.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/lazy.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/threading.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/type.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/hook.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/module.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/exceptions.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/integrations/__init__.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/integrations/blacksheep.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/py.typed +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/testing/__init__.py +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/testing/__init__.pyi +0 -0
- {python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/utils.py +0 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
from collections.abc import Callable
|
2
|
+
from types import GenericAlias
|
3
|
+
from typing import Any, TypeAliasType
|
4
|
+
|
5
|
+
from injection import Module, mod
|
6
|
+
from injection.exceptions import InjectionError
|
7
|
+
from injection.integrations import _is_installed
|
8
|
+
|
9
|
+
__all__ = ("Inject",)
|
10
|
+
|
11
|
+
if _is_installed("fastapi", __name__):
|
12
|
+
from fastapi import Depends
|
13
|
+
|
14
|
+
|
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:
|
22
|
+
"""
|
23
|
+
Declare a FastAPI dependency with `python-injection`.
|
24
|
+
"""
|
25
|
+
|
26
|
+
dependency: InjectionDependency[T] = InjectionDependency(cls, module or mod())
|
27
|
+
return Depends(dependency, use_cache=scoped)
|
28
|
+
|
29
|
+
|
30
|
+
class InjectionDependency[T]:
|
31
|
+
__slots__ = ("__call__", "__class")
|
32
|
+
|
33
|
+
__call__: Callable[[], T]
|
34
|
+
__class: type[T] | TypeAliasType | GenericAlias
|
35
|
+
|
36
|
+
def __init__(self, cls: type[T] | TypeAliasType | GenericAlias, module: Module):
|
37
|
+
lazy_instance = module.get_lazy_instance(cls)
|
38
|
+
self.__call__ = lambda: self.__ensure(~lazy_instance)
|
39
|
+
self.__class = cls
|
40
|
+
|
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:
|
49
|
+
if instance is None:
|
50
|
+
raise InjectionError(f"`{self.__class}` is an unknown dependency.")
|
51
|
+
|
52
|
+
return instance
|
@@ -1,37 +0,0 @@
|
|
1
|
-
from collections.abc import Callable
|
2
|
-
from typing import Any
|
3
|
-
|
4
|
-
from injection import Module, mod
|
5
|
-
from injection.exceptions import InjectionError
|
6
|
-
from injection.integrations import _is_installed
|
7
|
-
|
8
|
-
__all__ = ("Inject",)
|
9
|
-
|
10
|
-
if _is_installed("fastapi", __name__):
|
11
|
-
from fastapi import Depends
|
12
|
-
|
13
|
-
|
14
|
-
def Inject[T](cls: type[T] | Any, /, module: Module | None = None) -> Any: # noqa: N802
|
15
|
-
"""
|
16
|
-
Declare a FastAPI dependency with `python-injection`.
|
17
|
-
"""
|
18
|
-
|
19
|
-
dependency: InjectionDependency[T] = InjectionDependency(cls, module or mod())
|
20
|
-
return Depends(dependency)
|
21
|
-
|
22
|
-
|
23
|
-
class InjectionDependency[T]:
|
24
|
-
__slots__ = ("__call__",)
|
25
|
-
|
26
|
-
__call__: Callable[[], T]
|
27
|
-
|
28
|
-
def __init__(self, cls: type[T] | Any, module: Module):
|
29
|
-
lazy_instance = module.get_lazy_instance(cls)
|
30
|
-
self.__call__ = lambda: self.__ensure(~lazy_instance, cls)
|
31
|
-
|
32
|
-
@staticmethod
|
33
|
-
def __ensure[_T](instance: _T | None, cls: type[_T] | Any) -> _T:
|
34
|
-
if instance is None:
|
35
|
-
raise InjectionError(f"`{cls}` is an unknown dependency.")
|
36
|
-
|
37
|
-
return instance
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/__init__.py
RENAMED
File without changes
|
File without changes
|
{python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/invertible.py
RENAMED
File without changes
|
File without changes
|
{python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/_core/common/threading.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/integrations/__init__.py
RENAMED
File without changes
|
{python_injection-0.10.5.post0 → python_injection-0.10.6}/injection/integrations/blacksheep.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|