Skip to content

Commit

Permalink
fix (#75): correct main empty body processing (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik authored Feb 27, 2024
1 parent 5d25010 commit ae8f14c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion fast_depends/__about__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""FastDepends - extracted and cleared from HTTP domain FastAPI Dependency Injection System"""

__version__ = "2.4.1"
__version__ = "2.4.2"
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 ae8f14c

Please sign in to comment.