Skip to content

Commit

Permalink
chore: new error message (#122)
Browse files Browse the repository at this point in the history
* chore: new error message

* fix test
  • Loading branch information
tlambert03 authored Nov 25, 2024
1 parent 1855d56 commit ad21a89
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions src/in_n_out/_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}):
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit ad21a89

Please sign in to comment.