Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (#75): correct main empty body processing #76

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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