anydi 0.26.8a0__py3-none-any.whl → 0.27.0__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.
- anydi/_context.py +1 -1
- anydi/ext/_utils.py +89 -0
- anydi/ext/fastapi.py +5 -78
- anydi/ext/faststream.py +65 -0
- {anydi-0.26.8a0.dist-info → anydi-0.27.0.dist-info}/METADATA +3 -3
- {anydi-0.26.8a0.dist-info → anydi-0.27.0.dist-info}/RECORD +9 -7
- {anydi-0.26.8a0.dist-info → anydi-0.27.0.dist-info}/LICENSE +0 -0
- {anydi-0.26.8a0.dist-info → anydi-0.27.0.dist-info}/WHEEL +0 -0
- {anydi-0.26.8a0.dist-info → anydi-0.27.0.dist-info}/entry_points.txt +0 -0
anydi/_context.py
CHANGED
|
@@ -251,7 +251,7 @@ class ResourceScopedContext(ScopedContext):
|
|
|
251
251
|
exc_val: The exception instance, if any.
|
|
252
252
|
exc_tb: The traceback, if any.
|
|
253
253
|
"""
|
|
254
|
-
return self._stack.__exit__(exc_type, exc_val, exc_tb)
|
|
254
|
+
return self._stack.__exit__(exc_type, exc_val, exc_tb) # type: ignore[return-value]
|
|
255
255
|
|
|
256
256
|
@abc.abstractmethod
|
|
257
257
|
def start(self) -> None:
|
anydi/ext/_utils.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""AnyDI FastAPI extension."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import inspect
|
|
6
|
+
import logging
|
|
7
|
+
from typing import Any, Callable
|
|
8
|
+
|
|
9
|
+
from typing_extensions import Annotated, get_args, get_origin
|
|
10
|
+
|
|
11
|
+
from anydi import Container
|
|
12
|
+
from anydi._utils import get_full_qualname
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class HasInterface:
|
|
18
|
+
_interface: Any = None
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def interface(self) -> Any:
|
|
22
|
+
if self._interface is None:
|
|
23
|
+
raise TypeError("Interface is not set.")
|
|
24
|
+
return self._interface
|
|
25
|
+
|
|
26
|
+
@interface.setter
|
|
27
|
+
def interface(self, interface: Any) -> None:
|
|
28
|
+
self._interface = interface
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def patch_annotated_parameter(parameter: inspect.Parameter) -> inspect.Parameter:
|
|
32
|
+
"""Patch an annotated parameter to resolve the default value."""
|
|
33
|
+
if not (
|
|
34
|
+
get_origin(parameter.annotation) is Annotated
|
|
35
|
+
and parameter.default is parameter.empty
|
|
36
|
+
):
|
|
37
|
+
return parameter
|
|
38
|
+
|
|
39
|
+
tp_origin, *tp_metadata = get_args(parameter.annotation)
|
|
40
|
+
default = tp_metadata[-1]
|
|
41
|
+
|
|
42
|
+
if not isinstance(default, HasInterface):
|
|
43
|
+
return parameter
|
|
44
|
+
|
|
45
|
+
if (num := len(tp_metadata[:-1])) == 0:
|
|
46
|
+
interface = tp_origin
|
|
47
|
+
elif num == 1:
|
|
48
|
+
interface = Annotated[tp_origin, tp_metadata[0]]
|
|
49
|
+
elif num == 2:
|
|
50
|
+
interface = Annotated[tp_origin, tp_metadata[0], tp_metadata[1]]
|
|
51
|
+
elif num == 3:
|
|
52
|
+
interface = Annotated[
|
|
53
|
+
tp_origin,
|
|
54
|
+
tp_metadata[0],
|
|
55
|
+
tp_metadata[1],
|
|
56
|
+
tp_metadata[2],
|
|
57
|
+
]
|
|
58
|
+
else:
|
|
59
|
+
raise TypeError("Too many annotated arguments.") # pragma: no cover
|
|
60
|
+
return parameter.replace(annotation=interface, default=default)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def patch_call_parameter(
|
|
64
|
+
call: Callable[..., Any], parameter: inspect.Parameter, container: Container
|
|
65
|
+
) -> None:
|
|
66
|
+
"""Patch a parameter to inject dependencies using AnyDI.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
call: The call function.
|
|
70
|
+
parameter: The parameter to patch.
|
|
71
|
+
container: The AnyDI container.
|
|
72
|
+
"""
|
|
73
|
+
parameter = patch_annotated_parameter(parameter)
|
|
74
|
+
|
|
75
|
+
if not isinstance(parameter.default, HasInterface):
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
if not container.strict and not container.is_registered(parameter.annotation):
|
|
79
|
+
logger.debug(
|
|
80
|
+
f"Callable `{get_full_qualname(call)}` injected parameter "
|
|
81
|
+
f"`{parameter.name}` with an annotation of "
|
|
82
|
+
f"`{get_full_qualname(parameter.annotation)}` "
|
|
83
|
+
"is not registered. It will be registered at runtime with the "
|
|
84
|
+
"first call because it is running in non-strict mode."
|
|
85
|
+
)
|
|
86
|
+
else:
|
|
87
|
+
container._validate_injected_parameter(call, parameter) # noqa
|
|
88
|
+
|
|
89
|
+
parameter.default.interface = parameter.annotation
|
anydi/ext/fastapi.py
CHANGED
|
@@ -2,19 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import inspect
|
|
6
5
|
import logging
|
|
7
|
-
from typing import Any,
|
|
6
|
+
from typing import Any, Iterator, cast
|
|
8
7
|
|
|
9
8
|
from fastapi import Depends, FastAPI, params
|
|
10
9
|
from fastapi.dependencies.models import Dependant
|
|
11
10
|
from fastapi.routing import APIRoute
|
|
12
11
|
from starlette.requests import Request
|
|
13
|
-
from typing_extensions import Annotated, get_args, get_origin
|
|
14
12
|
|
|
15
13
|
from anydi import Container
|
|
16
|
-
from anydi._utils import
|
|
14
|
+
from anydi._utils import get_typed_parameters
|
|
17
15
|
|
|
16
|
+
from ._utils import HasInterface, patch_call_parameter
|
|
18
17
|
from .starlette.middleware import RequestScopedMiddleware
|
|
19
18
|
|
|
20
19
|
__all__ = ["RequestScopedMiddleware", "install", "get_container", "Inject"]
|
|
@@ -48,7 +47,7 @@ def install(app: FastAPI, container: Container) -> None:
|
|
|
48
47
|
if not call:
|
|
49
48
|
continue # pragma: no cover
|
|
50
49
|
for parameter in get_typed_parameters(call):
|
|
51
|
-
|
|
50
|
+
patch_call_parameter(call, parameter, container)
|
|
52
51
|
|
|
53
52
|
|
|
54
53
|
def get_container(request: Request) -> Container:
|
|
@@ -63,22 +62,11 @@ def get_container(request: Request) -> Container:
|
|
|
63
62
|
return cast(Container, request.app.state.container)
|
|
64
63
|
|
|
65
64
|
|
|
66
|
-
class Resolver(params.Depends):
|
|
65
|
+
class Resolver(HasInterface, params.Depends):
|
|
67
66
|
"""Parameter dependency class for injecting dependencies using AnyDI."""
|
|
68
67
|
|
|
69
68
|
def __init__(self) -> None:
|
|
70
69
|
super().__init__(dependency=self._dependency, use_cache=True)
|
|
71
|
-
self._interface: Any = None
|
|
72
|
-
|
|
73
|
-
@property
|
|
74
|
-
def interface(self) -> Any:
|
|
75
|
-
if self._interface is None:
|
|
76
|
-
raise TypeError("Interface is not set.")
|
|
77
|
-
return self._interface
|
|
78
|
-
|
|
79
|
-
@interface.setter
|
|
80
|
-
def interface(self, interface: Any) -> None:
|
|
81
|
-
self._interface = interface
|
|
82
70
|
|
|
83
71
|
async def _dependency(self, container: Container = Depends(get_container)) -> Any:
|
|
84
72
|
return await container.aresolve(self.interface)
|
|
@@ -102,64 +90,3 @@ def _iter_dependencies(dependant: Dependant) -> Iterator[Dependant]:
|
|
|
102
90
|
if dependant.dependencies:
|
|
103
91
|
for sub_dependant in dependant.dependencies:
|
|
104
92
|
yield from _iter_dependencies(sub_dependant)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
def _patch_route_parameter(
|
|
108
|
-
call: Callable[..., Any], parameter: inspect.Parameter, container: Container
|
|
109
|
-
) -> None:
|
|
110
|
-
"""Patch a parameter to inject dependencies using AnyDI.
|
|
111
|
-
|
|
112
|
-
Args:
|
|
113
|
-
call: The call function.
|
|
114
|
-
parameter: The parameter to patch.
|
|
115
|
-
container: The AnyDI container.
|
|
116
|
-
"""
|
|
117
|
-
parameter = _patch_annotated_parameter(parameter)
|
|
118
|
-
|
|
119
|
-
if not isinstance(parameter.default, Resolver):
|
|
120
|
-
return None
|
|
121
|
-
|
|
122
|
-
if not container.strict and not container.is_registered(parameter.annotation):
|
|
123
|
-
logger.debug(
|
|
124
|
-
f"Callable `{get_full_qualname(call)}` injected parameter "
|
|
125
|
-
f"`{parameter.name}` with an annotation of "
|
|
126
|
-
f"`{get_full_qualname(parameter.annotation)}` "
|
|
127
|
-
"is not registered. It will be registered at runtime with the "
|
|
128
|
-
"first call because it is running in non-strict mode."
|
|
129
|
-
)
|
|
130
|
-
else:
|
|
131
|
-
container._validate_injected_parameter(call, parameter) # noqa
|
|
132
|
-
|
|
133
|
-
parameter.default.interface = parameter.annotation
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
def _patch_annotated_parameter(parameter: inspect.Parameter) -> inspect.Parameter:
|
|
137
|
-
"""Patch an annotated parameter to resolve the default value."""
|
|
138
|
-
if not (
|
|
139
|
-
get_origin(parameter.annotation) is Annotated
|
|
140
|
-
and parameter.default is parameter.empty
|
|
141
|
-
):
|
|
142
|
-
return parameter
|
|
143
|
-
|
|
144
|
-
tp_origin, *tp_metadata = get_args(parameter.annotation)
|
|
145
|
-
default = tp_metadata[-1]
|
|
146
|
-
|
|
147
|
-
if not isinstance(default, Resolver):
|
|
148
|
-
return parameter
|
|
149
|
-
|
|
150
|
-
if (num := len(tp_metadata[:-1])) == 0:
|
|
151
|
-
interface = tp_origin
|
|
152
|
-
elif num == 1:
|
|
153
|
-
interface = Annotated[tp_origin, tp_metadata[0]]
|
|
154
|
-
elif num == 2:
|
|
155
|
-
interface = Annotated[tp_origin, tp_metadata[0], tp_metadata[1]]
|
|
156
|
-
elif num == 3:
|
|
157
|
-
interface = Annotated[
|
|
158
|
-
tp_origin,
|
|
159
|
-
tp_metadata[0],
|
|
160
|
-
tp_metadata[1],
|
|
161
|
-
tp_metadata[2],
|
|
162
|
-
]
|
|
163
|
-
else:
|
|
164
|
-
raise TypeError("Too many annotated arguments.") # pragma: no cover
|
|
165
|
-
return parameter.replace(annotation=interface, default=default)
|
anydi/ext/faststream.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""AnyDI FastStream extension."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from typing import Any, cast
|
|
7
|
+
|
|
8
|
+
from fast_depends.dependencies import Depends
|
|
9
|
+
from faststream import ContextRepo
|
|
10
|
+
from faststream.broker.core.usecase import BrokerUsecase
|
|
11
|
+
|
|
12
|
+
from anydi import Container
|
|
13
|
+
from anydi._utils import get_typed_parameters
|
|
14
|
+
|
|
15
|
+
from ._utils import HasInterface, patch_call_parameter
|
|
16
|
+
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def install(broker: BrokerUsecase[Any, Any], container: Container) -> None:
|
|
21
|
+
"""Install AnyDI into a FastStream broker.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
broker: The broker.
|
|
25
|
+
container: The container.
|
|
26
|
+
|
|
27
|
+
This function installs the AnyDI container into a FastStream broker by attaching
|
|
28
|
+
it to the broker. It also patches the broker handlers to inject the required
|
|
29
|
+
dependencies using AnyDI.
|
|
30
|
+
"""
|
|
31
|
+
broker._container = container # type: ignore[attr-defined]
|
|
32
|
+
|
|
33
|
+
for handler in _get_broken_handlers(broker):
|
|
34
|
+
call = handler._original_call # noqa
|
|
35
|
+
for parameter in get_typed_parameters(call):
|
|
36
|
+
patch_call_parameter(call, parameter, container)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _get_broken_handlers(broker: BrokerUsecase[Any, Any]) -> list[Any]:
|
|
40
|
+
if hasattr(broker, "handlers"):
|
|
41
|
+
return [handler.calls[0][0] for handler in broker.handlers.values()]
|
|
42
|
+
# faststream > 0.5.0
|
|
43
|
+
return [
|
|
44
|
+
subscriber.calls[0].handler
|
|
45
|
+
for subscriber in broker._subscribers.values() # noqa
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def get_container(broker: BrokerUsecase[Any, Any]) -> Container:
|
|
50
|
+
return cast(Container, getattr(broker, "_container")) # noqa
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Resolver(HasInterface, Depends):
|
|
54
|
+
"""Parameter dependency class for injecting dependencies using AnyDI."""
|
|
55
|
+
|
|
56
|
+
def __init__(self) -> None:
|
|
57
|
+
super().__init__(dependency=self._dependency, use_cache=True, cast=True)
|
|
58
|
+
|
|
59
|
+
async def _dependency(self, context: ContextRepo) -> Any:
|
|
60
|
+
container = get_container(context.get("broker"))
|
|
61
|
+
return await container.aresolve(self.interface)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def Inject() -> Any: # noqa
|
|
65
|
+
return Resolver()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: anydi
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.27.0
|
|
4
4
|
Summary: Dependency Injection library
|
|
5
5
|
Home-page: https://github.com/antonrh/anydi
|
|
6
6
|
License: MIT
|
|
@@ -32,8 +32,8 @@ Provides-Extra: async
|
|
|
32
32
|
Provides-Extra: docs
|
|
33
33
|
Requires-Dist: anyio (>=3.6.2,<4.0.0) ; extra == "async"
|
|
34
34
|
Requires-Dist: mkdocs (>=1.4.2,<2.0.0) ; extra == "docs"
|
|
35
|
-
Requires-Dist: mkdocs-material (>=9.
|
|
36
|
-
Requires-Dist: typing-extensions (>=4.
|
|
35
|
+
Requires-Dist: mkdocs-material (>=9.5.21,<10.0.0) ; extra == "docs"
|
|
36
|
+
Requires-Dist: typing-extensions (>=4.12.1,<5.0.0)
|
|
37
37
|
Project-URL: Repository, https://github.com/antonrh/anydi
|
|
38
38
|
Description-Content-Type: text/markdown
|
|
39
39
|
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
anydi/__init__.py,sha256=aeaBp5vq09sG-e9sqqs9qpUtUIDNfOdFPrlAfE5Ku9E,584
|
|
2
2
|
anydi/_container.py,sha256=-vpiYl2Tx2QK9wl74DiF5qLFRudJ3e78TqEyuyOrthE,29055
|
|
3
|
-
anydi/_context.py,sha256=
|
|
3
|
+
anydi/_context.py,sha256=_hJ1Cf2amgAr4uLvgk2KtGWMo0p7JFM_db1_YbWweHI,11895
|
|
4
4
|
anydi/_logger.py,sha256=UpubJUnW83kffFxkhUlObm2DmZX1Pjqoz9YFKS-JOPg,52
|
|
5
5
|
anydi/_module.py,sha256=E1TfLud_Af-MPB83PxIzHVA1jlDW2FGaRP_il1a6y3Y,3675
|
|
6
6
|
anydi/_scanner.py,sha256=cyEk-K2Q8ssZStq8GrxMeEcCuAZMw-RXrjlgWEevKCs,6667
|
|
7
7
|
anydi/_types.py,sha256=i8xFxz8pmFj7SGqwOwae_P9VtiRie6DVLwfaLibLwhc,3653
|
|
8
8
|
anydi/_utils.py,sha256=TzUZRTNaXu78Uzv9NCNLSM5s9TgEIiXtXgr1HhsfHac,4017
|
|
9
9
|
anydi/ext/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
|
+
anydi/ext/_utils.py,sha256=2kxLPTMM9Ro3s6-knbqYzONlqRB3hMcwZFFRQGHcFUg,2691
|
|
10
11
|
anydi/ext/django/__init__.py,sha256=QI1IABCVgSDTUoh7M9WMECKXwB3xvh04HfQ9TOWw1Mk,223
|
|
11
12
|
anydi/ext/django/_container.py,sha256=cxVoYQG16WP0S_Yv4TnLwuaaT7NVEOhLWO-YdALJUb4,418
|
|
12
13
|
anydi/ext/django/_settings.py,sha256=cKzFBGtPCsexZ2ZcInubBukIebhxzNfa3F0KuwoZYaA,844
|
|
@@ -16,13 +17,14 @@ anydi/ext/django/middleware.py,sha256=iVHWtE829khMY-BXbNNt0g2FrIApKprna7dCG9ObEi
|
|
|
16
17
|
anydi/ext/django/ninja/__init__.py,sha256=kW3grUgWp_nkWSG_-39ADHMrZLGNcj9TsJ9OW8iWWrk,546
|
|
17
18
|
anydi/ext/django/ninja/_operation.py,sha256=wSWa7D73XTVlOibmOciv2l6JHPe1ERZcXrqI8W-oO2w,2696
|
|
18
19
|
anydi/ext/django/ninja/_signature.py,sha256=2cSzKxBIxXLqtwNuH6GSlmjVJFftoGmleWfyk_NVEWw,2207
|
|
19
|
-
anydi/ext/fastapi.py,sha256=
|
|
20
|
+
anydi/ext/fastapi.py,sha256=vhfSyovXuCjvSkx6AiLOTNU975i8wDg72C5fqXQiFLw,2896
|
|
21
|
+
anydi/ext/faststream.py,sha256=L4rkWYIO4ZZuWH-8M8NT6_J0bT0Dz_EWO3B6Oj1iFBI,2024
|
|
20
22
|
anydi/ext/pytest_plugin.py,sha256=3OWphc4nEzla46_8KR7LXtwGns5eol_YlUWfTf4Cr2Q,3952
|
|
21
23
|
anydi/ext/starlette/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
22
24
|
anydi/ext/starlette/middleware.py,sha256=Ni0BQaPjs_Ha6zcLZYYJ3-XkslTCnL9aCSa06rnRDMI,1139
|
|
23
25
|
anydi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
24
|
-
anydi-0.
|
|
25
|
-
anydi-0.
|
|
26
|
-
anydi-0.
|
|
27
|
-
anydi-0.
|
|
28
|
-
anydi-0.
|
|
26
|
+
anydi-0.27.0.dist-info/LICENSE,sha256=V6rU8a8fv6o2jQ-7ODHs0XfDFimot8Q6Km6CylRIDTo,1069
|
|
27
|
+
anydi-0.27.0.dist-info/METADATA,sha256=zazVcW9x_zEq3W2q7We5DMLMuJoNWH1I5JOzVsfTb_g,5111
|
|
28
|
+
anydi-0.27.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
29
|
+
anydi-0.27.0.dist-info/entry_points.txt,sha256=GmQblwzxFg42zva1HyBYJJ7TvrTIcSAGBHmyi3bvsi4,42
|
|
30
|
+
anydi-0.27.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|