Skip to content

Commit 8f8ec35

Browse files
Czakipre-commit-ci[bot]tlambert03
authored
feat: Try to inject parameter value if default value is None (#110)
* feat: Try to inject parameter value if default value is None * style(pre-commit.ci): auto fixes [...] * test: add test --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Talley Lambert <[email protected]>
1 parent 190cdb9 commit 8f8ec35

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/in_n_out/_store.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,10 @@ def _exec(*args: P.args, **kwargs: P.kwargs) -> R:
778778
# first, get and call the provider functions for each parameter type:
779779
_injected_names: set[str] = set()
780780
for param in sig.parameters.values():
781-
if param.name not in bound.arguments:
781+
if (
782+
param.name not in bound.arguments
783+
or bound.arguments[param.name] is None
784+
):
782785
provided = self.provide(param.annotation)
783786
if provided is not None or is_optional(param.annotation):
784787
logger.debug(

tests/test_injection.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,18 @@ def f(i: Optional[Thing]) -> Optional[Thing]:
296296
thing = Thing()
297297
with register(providers={Optional[Thing]: lambda: thing}):
298298
assert inject(f)() is thing
299+
300+
301+
def test_inject_into_optional_with_default() -> None:
302+
class Thing: ...
303+
304+
def f(i: Optional[Thing] = None) -> Optional[Thing]:
305+
return i
306+
307+
thing = Thing()
308+
with register(providers={Optional[Thing]: lambda: thing}):
309+
assert inject(f)() is thing
310+
with register(providers={Thing: lambda: thing}):
311+
assert inject(f)() is thing
312+
313+
assert inject(f)() is None

0 commit comments

Comments
 (0)