Skip to content

Commit

Permalink
fix (#75): correct main empty body processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik committed Feb 27, 2024
1 parent 5d25010 commit a1016aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
14 changes: 9 additions & 5 deletions fast_depends/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,15 @@ def _solve(
keyword_args = self.keyword_args

else:
keyword_args = self.keyword_args + self.positional_args
for arg in self.keyword_args:
keyword_args = set(self.keyword_args + self.positional_args)
for arg in keyword_args - set(self.dependencies.keys()):
if args:
kw[arg], args = args[0], args[1:]
else:
break

solved_kw: Dict[str, Any]
solved_kw = yield (), kw, call
solved_kw = yield args, kw, call

args_: Sequence[Any]
if self.cast:
Expand Down Expand Up @@ -347,14 +347,15 @@ def solve(
**kwargs,
)
try:
_, kwargs, _ = next(cast_gen)
args, kwargs, _ = next(cast_gen)
except StopIteration as e:
cached_value: T = e.value
return cached_value

# Heat cache and solve extra dependencies
for dep, _ in self.sorted_dependencies:
dep.solve(
*args,
stack=stack,
cache_dependencies=cache_dependencies,
dependency_overrides=dependency_overrides,
Expand All @@ -365,6 +366,7 @@ def solve(
# Always get from cache
for dep in self.extra_dependencies:
dep.solve(
*args,
stack=stack,
cache_dependencies=cache_dependencies,
dependency_overrides=dependency_overrides,
Expand Down Expand Up @@ -447,7 +449,7 @@ async def asolve(
**kwargs,
)
try:
_, kwargs, _ = next(cast_gen)
args, kwargs, _ = next(cast_gen)
except StopIteration as e:
cached_value: T = e.value
return cached_value
Expand All @@ -459,6 +461,7 @@ async def asolve(
for dep, subdep in self.sorted_dependencies:
solve = partial(
dep.asolve,
*args,
stack=stack,
cache_dependencies=cache_dependencies,
dependency_overrides=dependency_overrides,
Expand All @@ -479,6 +482,7 @@ async def asolve(
# Always get from cache
for dep in self.extra_dependencies:
await dep.asolve(
*args,
stack=stack,
cache_dependencies=cache_dependencies,
dependency_overrides=dependency_overrides,
Expand Down
13 changes: 13 additions & 0 deletions tests/async/test_depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ async def some_func(b: int, c=Depends(dep_func)) -> int:
assert (await some_func("2")) == 7


@pytest.mark.anyio
async def test_empty_main_body():
async def dep_func(a: int) -> float:
return a

@inject
async def some_func(c=Depends(dep_func)):
assert isinstance(c, float)
assert c == 1.0

await some_func("1")


@pytest.mark.anyio
async def test_sync_depends():
def sync_dep_func(a: int) -> float:
Expand Down
12 changes: 12 additions & 0 deletions tests/sync/test_depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def some_func(b: int, c=Depends(dep_func)) -> int:
assert some_func("2") == 7


def test_empty_main_body():
def dep_func(a: int) -> float:
return a

@inject
def some_func(c=Depends(dep_func)):
assert isinstance(c, float)
assert c == 1.0

some_func("1")


def test_depends_error():
def dep_func(b: dict, a: int = 3) -> float: # pragma: no cover
return a + b
Expand Down

0 comments on commit a1016aa

Please sign in to comment.