From ad21a896e9f4563d482ea1f701941808628e3a1e Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sun, 24 Nov 2024 20:52:46 -0500 Subject: [PATCH] chore: new error message (#122) * chore: new error message * fix test --- docs/getting_started.md | 4 ++-- src/in_n_out/_store.py | 12 ++++++------ tests/test_injection.py | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/getting_started.md b/docs/getting_started.md index 0383342..04000a4 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -132,8 +132,8 @@ def give_me_a_string(s: str) -> str: return s give_me_a_string() -# TypeError: After injecting dependencies for NO arguments, -# give_me_a_string() missing 1 required positional argument: 's' +# TypeError: Error calling in-n-out injected function '__main__.give_me_a_string' with kwargs {}. +# See TypeError above for more details. ``` !!!note "function defaults" diff --git a/src/in_n_out/_store.py b/src/in_n_out/_store.py index 2588194..8644c8e 100644 --- a/src/in_n_out/_store.py +++ b/src/in_n_out/_store.py @@ -804,11 +804,6 @@ def _exec(*args: P.args, **kwargs: P.kwargs) -> R: raise # pragma: no cover # likely a required argument is still missing. # show what was injected and raise - _argnames = ( - f"arguments: {_injected_names!r}" - if _injected_names - else "NO arguments" - ) logger.exception(e) for param in sig.parameters.values(): if ( @@ -821,8 +816,13 @@ def _exec(*args: P.args, **kwargs: P.kwargs) -> R: f"{list(self.iter_providers(param.annotation))}" ) + mod_name = getattr(func, "__module__", "") + func_name = getattr(func, "__qualname__", str(func)) + full_name = f"{mod_name}.{func_name}" raise TypeError( - f"After injecting dependencies for {_argnames}, {e}" + f"Error calling in-n-out injected function {full_name!r} with " + f"kwargs {bound.arguments!r}.\n\n" + f"See {type(e).__name__} above for more details." ) from e return result diff --git a/tests/test_injection.py b/tests/test_injection.py index 32af8cb..7e44c17 100644 --- a/tests/test_injection.py +++ b/tests/test_injection.py @@ -69,7 +69,7 @@ def test_injection_missing(): def f(x: int) -> int: return x - with pytest.raises(TypeError, match="After injecting dependencies"): + with pytest.raises(TypeError, match="Error calling in-n-out injected function"): f() assert f(4) == 4 with register(providers={int: lambda: 1}): @@ -192,7 +192,7 @@ def f(x: int) -> None: mock(x) with test_store.register(providers={Optional[int]: lambda: None}): - with pytest.raises(TypeError, match="missing 1 required positional argument"): + with pytest.raises(TypeError, match="Error calling in-n-out injected function"): f() mock.assert_not_called()