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 bug #115 #117

Merged
merged 2 commits into from
Jul 24, 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
6 changes: 3 additions & 3 deletions fast_depends/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ def __init__(
self.keyword_args = tuple(keyword_args or ())
self.positional_args = tuple(positional_args or ())
self.response_model = response_model

self.use_cache = use_cache
self.cast = cast
self.is_async = (
Expand All @@ -187,7 +186,6 @@ def __init__(
self.sorted_dependencies = tuple(
(i, len(i.sorted_dependencies)) for i in sorted_dep if i.use_cache
)

for name in chain(self.dependencies.keys(), self.custom_fields.keys()):
params.pop(name, None)
self.params = params
Expand Down Expand Up @@ -261,8 +259,10 @@ def _solve(

else:
keyword_args = self.keyword_args + self.positional_args

for arg in keyword_args:
if not self.cast and arg in self.params:
kw[arg] = self.params[arg][1]

if not args:
break

Expand Down
50 changes: 31 additions & 19 deletions tests/async/test_depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def another_dep_func(b: int, a: int = 3) -> dict: # pragma: no cover

@inject
async def some_func(
b: int, c=Depends(dep_func), d=Depends(another_dep_func)
b: int, c=Depends(dep_func), d=Depends(another_dep_func)
) -> int: # pragma: no cover
assert c is None
return b
Expand Down Expand Up @@ -108,17 +108,17 @@ async def dep_func(a):

@inject
async def some_func(
a: int,
b: int,
c: "Annotated[int, Depends(dep_func)]",
a: int,
b: int,
c: "Annotated[int, Depends(dep_func)]",
) -> float:
assert isinstance(c, int)
return a + b + c

@inject
async def another_func(
a: int,
c: "Annotated[int, Depends(dep_func)]",
a: int,
c: "Annotated[int, Depends(dep_func)]",
):
return a + c

Expand All @@ -133,17 +133,17 @@ async def adep_func(a):

@inject
async def some_func(
a: int,
b: int,
c: Annotated["float", Depends(adep_func)],
a: int,
b: int,
c: Annotated["float", Depends(adep_func)],
) -> float:
assert isinstance(c, float)
return a + b + c

@inject
async def another_func(
a: int,
c: Annotated["float", Depends(adep_func)],
a: int,
c: Annotated["float", Depends(adep_func)],
):
return a + c

Expand Down Expand Up @@ -184,8 +184,8 @@ async def dep_func(a=Depends(nested_dep_func, use_cache=False)):

@inject
async def some_func(
a=Depends(dep_func, use_cache=False),
b=Depends(nested_dep_func, use_cache=False),
a=Depends(dep_func, use_cache=False),
b=Depends(nested_dep_func, use_cache=False),
):
assert a is b
return a + b
Expand Down Expand Up @@ -361,9 +361,9 @@ async def get_logger() -> logging.Logger:

@inject
async def some_func(
b,
a: A = Depends(dep, cast=False),
logger: logging.Logger = Depends(get_logger, cast=False),
b,
a: A = Depends(dep, cast=False),
logger: logging.Logger = Depends(get_logger, cast=False),
):
assert a.a == 1
assert logger
Expand All @@ -386,9 +386,9 @@ async def get_logger() -> logging.Logger:

@inject(cast=False)
async def some_func(
b: str,
a: A = Depends(dep),
logger: logging.Logger = Depends(get_logger),
b: str,
a: A = Depends(dep),
logger: logging.Logger = Depends(get_logger),
) -> str:
assert a.a == 1
assert logger
Expand Down Expand Up @@ -449,3 +449,15 @@ async def func(a=Depends(partial(dep, 10))): # noqa D008
return a

assert await func() == 10


@pytest.mark.anyio
async def test_default_key_value():
async def dep(a: str = "a"):
return a

@inject(cast=False)
async def func(a=Depends(dep)):
return a

assert await func() == "a"
11 changes: 11 additions & 0 deletions tests/sync/test_depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,14 @@ def func(a=Depends(partial(dep, 10))): # noqa: B008
return a

assert func() == 10


def test_default_key_value():
def dep(a: str = "a"):
return a

@inject(cast=False)
def func(a=Depends(dep)):
return a

assert func() == "a"