Skip to content

Commit

Permalink
Fix overriding injections in sync functions. (#110)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander <e1634240@student.tuwien.ac.at>
  • Loading branch information
alexanderlazarev0 and Alexander authored Oct 29, 2024
1 parent 661d76d commit 905ed04
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions tests/test_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ def test_sync_injection(
assert fixture_one == 1


def test_wrong_sync_injection() -> None:
def test_overriden_sync_injection() -> None:
@inject
def inner(
_: container.SimpleFactory = Provide[container.DIContainer.simple_factory],
) -> None:
) -> container.SimpleFactory:
"""Do nothing."""
return _

with pytest.raises(RuntimeError, match="Injected arguments must not be redefined"):
inner(_=container.SimpleFactory(dep1="1", dep2=2))
factory = container.SimpleFactory(dep1="1", dep2=2)
with pytest.warns(RuntimeWarning, match="Expected injection, but nothing found. Remove @inject decorator."):
assert inner(_=factory) == factory


def test_sync_empty_injection() -> None:
Expand Down
8 changes: 4 additions & 4 deletions that_depends/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def _inject_to_sync(
@functools.wraps(func)
def inner(*args: P.args, **kwargs: P.kwargs) -> T:
injected = False
for field_name, field_value in signature.parameters.items():
for i, (field_name, field_value) in enumerate(signature.parameters.items()):
if i < len(args):
continue
if not isinstance(field_value.default, AbstractProvider):
continue
if field_name in kwargs:
msg = f"Injected arguments must not be redefined, {field_name=}"
raise RuntimeError(msg)

continue
kwargs[field_name] = field_value.default.sync_resolve()
injected = True

Expand Down

0 comments on commit 905ed04

Please sign in to comment.