Skip to content

Commit 8412a82

Browse files
authored
feat: Add ignore_type_hint to the package API
1 parent 3cf33ab commit 8412a82

File tree

3 files changed

+220
-205
lines changed

3 files changed

+220
-205
lines changed

injection/__init__.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class Module:
182182
/,
183183
*,
184184
cls: _InjectableFactory[Any] = ...,
185+
ignore_type_hint: bool = ...,
185186
inject: bool = ...,
186187
on: _TypeInfo[Any] = ...,
187188
mode: Mode | ModeStr = ...,
@@ -199,6 +200,7 @@ class Module:
199200
/,
200201
*,
201202
cls: _InjectableFactory[Any] = ...,
203+
ignore_type_hint: bool = ...,
202204
inject: bool = ...,
203205
on: _TypeInfo[Any] = ...,
204206
mode: Mode | ModeStr = ...,
@@ -209,6 +211,7 @@ class Module:
209211
wrapped: T,
210212
/,
211213
*,
214+
ignore_type_hint: bool = ...,
212215
inject: bool = ...,
213216
on: _TypeInfo[Any] = ...,
214217
mode: Mode | ModeStr = ...,
@@ -225,6 +228,7 @@ class Module:
225228
wrapped: None = ...,
226229
/,
227230
*,
231+
ignore_type_hint: bool = ...,
228232
inject: bool = ...,
229233
on: _TypeInfo[Any] = ...,
230234
mode: Mode | ModeStr = ...,
@@ -234,6 +238,7 @@ class Module:
234238
scope_name: str,
235239
/,
236240
*,
241+
ignore_type_hint: bool = ...,
237242
inject: bool = ...,
238243
on: _TypeInfo[Any] = ...,
239244
mode: Mode | ModeStr = ...,
@@ -264,6 +269,7 @@ class Module:
264269
wrapped: T,
265270
/,
266271
*,
272+
ignore_type_hint: bool = ...,
267273
on: _TypeInfo[Any] = ...,
268274
mode: Mode | ModeStr = ...,
269275
) -> T:
@@ -279,6 +285,7 @@ class Module:
279285
wrapped: None = ...,
280286
/,
281287
*,
288+
ignore_type_hint: bool = ...,
282289
on: _TypeInfo[Any] = ...,
283290
mode: Mode | ModeStr = ...,
284291
) -> _Decorator: ...

injection/_core/module.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
AsyncIterator,
99
Awaitable,
1010
Callable,
11+
Collection,
1112
Container,
1213
Generator,
1314
Iterable,
@@ -180,7 +181,7 @@ def get_default(cls) -> Priority:
180181
@dataclass(repr=False, eq=False, frozen=True, slots=True)
181182
class _ScopedContext[**P, T]:
182183
cls: type[ScopedInjectable[Any, T]]
183-
hints: TypeInfo[T]
184+
hints: Collection[TypeInfo[T]]
184185
wrapper: Recipe[P, T] | ContextManagerRecipe[P, T]
185186

186187

@@ -266,6 +267,7 @@ def scoped[**P, T](
266267
scope_name: str,
267268
/,
268269
*,
270+
ignore_type_hint: bool = False,
269271
inject: bool = True,
270272
on: TypeInfo[T] = (),
271273
mode: Mode | ModeStr = Mode.get_default(),
@@ -276,21 +278,21 @@ def decorator(
276278
if isasyncgenfunction(wrapped):
277279
ctx = _ScopedContext(
278280
cls=AsyncCMScopedInjectable,
279-
hints=get_yield_hints(wrapped),
281+
hints=() if ignore_type_hint else get_yield_hints(wrapped),
280282
wrapper=asynccontextmanager(wrapped),
281283
)
282284

283285
elif isgeneratorfunction(wrapped):
284286
ctx = _ScopedContext(
285287
cls=CMScopedInjectable,
286-
hints=get_yield_hints(wrapped),
288+
hints=() if ignore_type_hint else get_yield_hints(wrapped),
287289
wrapper=contextmanager(wrapped),
288290
)
289291

290292
else:
291293
ctx = _ScopedContext(
292294
cls=SimpleScopedInjectable,
293-
hints=(wrapped,),
295+
hints=() if ignore_type_hint else (wrapped,),
294296
wrapper=wrapped,
295297
)
296298

@@ -299,7 +301,7 @@ def decorator(
299301
cls=ctx.cls.bind_scope_name(scope_name),
300302
ignore_type_hint=True,
301303
inject=inject,
302-
on=(ctx.hints, on),
304+
on=(*ctx.hints, on),
303305
mode=mode,
304306
)
305307
return wrapped

0 commit comments

Comments
 (0)