From 8bda78a9334665bbde79d651607868bd5674263d Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Mon, 22 Apr 2024 16:21:20 +0200 Subject: [PATCH 1/3] feat: Try to inject parameter value if default value is None --- src/in_n_out/_store.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/in_n_out/_store.py b/src/in_n_out/_store.py index fee18fd..ff30f09 100644 --- a/src/in_n_out/_store.py +++ b/src/in_n_out/_store.py @@ -778,7 +778,7 @@ def _exec(*args: P.args, **kwargs: P.kwargs) -> R: # first, get and call the provider functions for each parameter type: _injected_names: set[str] = set() for param in sig.parameters.values(): - if param.name not in bound.arguments: + if param.name not in bound.arguments or bound.arguments[param.name] is None: provided = self.provide(param.annotation) if provided is not None or is_optional(param.annotation): logger.debug( From 43bbeba194f43550d6a5b4fc9f49e70f8f3927fe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Apr 2024 14:26:44 +0000 Subject: [PATCH 2/3] style(pre-commit.ci): auto fixes [...] --- src/in_n_out/_store.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/in_n_out/_store.py b/src/in_n_out/_store.py index ff30f09..0006bf7 100644 --- a/src/in_n_out/_store.py +++ b/src/in_n_out/_store.py @@ -778,7 +778,10 @@ def _exec(*args: P.args, **kwargs: P.kwargs) -> R: # first, get and call the provider functions for each parameter type: _injected_names: set[str] = set() for param in sig.parameters.values(): - if param.name not in bound.arguments or bound.arguments[param.name] is None: + if ( + param.name not in bound.arguments + or bound.arguments[param.name] is None + ): provided = self.provide(param.annotation) if provided is not None or is_optional(param.annotation): logger.debug( From ba046bfbcff03e4ea6a082b7ff3ef7644931ec92 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Mon, 22 Apr 2024 14:34:26 -0400 Subject: [PATCH 3/3] test: add test --- tests/test_injection.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_injection.py b/tests/test_injection.py index f0fd052..74b44e8 100644 --- a/tests/test_injection.py +++ b/tests/test_injection.py @@ -296,3 +296,18 @@ def f(i: Optional[Thing]) -> Optional[Thing]: thing = Thing() with register(providers={Optional[Thing]: lambda: thing}): assert inject(f)() is thing + + +def test_inject_into_optional_with_default() -> None: + class Thing: ... + + def f(i: Optional[Thing] = None) -> Optional[Thing]: + return i + + thing = Thing() + with register(providers={Optional[Thing]: lambda: thing}): + assert inject(f)() is thing + with register(providers={Thing: lambda: thing}): + assert inject(f)() is thing + + assert inject(f)() is None