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

#98 Fixed overriding variables. #110

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions fast_depends/core/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ def build_call_model(
), f"You cannot use async dependency `{name}` at sync main"

typed_params, return_annotation = get_typed_signature(call)
if (
(is_call_generator := is_gen_callable(call) or
is_async_gen_callable(call)) and
(return_args := get_args(return_annotation))
if (is_call_generator := is_gen_callable(call) or is_async_gen_callable(call)) and (
return_args := get_args(return_annotation)
):
return_annotation = return_args[0]

Expand Down Expand Up @@ -150,6 +148,7 @@ def build_call_model(
class_fields[param_name] = (annotation, ...)

keyword_args.append(param_name)
positional_args.append(param_name)

elif custom:
assert not (
Expand Down
33 changes: 17 additions & 16 deletions fast_depends/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _solve(

else:
call = self.call

if self.use_cache and call in cache_dependencies:
return cache_dependencies[call]

Expand Down Expand Up @@ -367,7 +367,6 @@ def solve(
**kwargs,
)

# Always get from cache
for dep in self.extra_dependencies:
dep.solve(
*args,
Expand All @@ -379,13 +378,14 @@ def solve(
)

for dep_arg, dep in self.dependencies.items():
kwargs[dep_arg] = dep.solve(
stack=stack,
cache_dependencies=cache_dependencies,
dependency_overrides=dependency_overrides,
nested=True,
**kwargs,
)
if dep_arg not in kwargs:
kwargs[dep_arg] = dep.solve(
stack=stack,
cache_dependencies=cache_dependencies,
dependency_overrides=dependency_overrides,
nested=True,
**kwargs,
)

for custom in self.custom_fields.values():
if custom.field:
Expand Down Expand Up @@ -495,13 +495,14 @@ async def asolve(
)

for dep_arg, dep in self.dependencies.items():
kwargs[dep_arg] = await dep.asolve(
stack=stack,
cache_dependencies=cache_dependencies,
dependency_overrides=dependency_overrides,
nested=True,
**kwargs,
)
if dep_arg not in kwargs:
kwargs[dep_arg] = await dep.asolve(
stack=stack,
cache_dependencies=cache_dependencies,
dependency_overrides=dependency_overrides,
nested=True,
**kwargs,
)

custom_to_solve: List[CustomField] = []

Expand Down
26 changes: 26 additions & 0 deletions tests/async/test_depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@
from fast_depends import Depends, inject


@pytest.mark.anyio
async def test_override_depends_with_by_passing_kwarg():
async def dep_func() -> int:
return 1

@inject
async def some_func(a: int = Depends(dep_func)) -> int:
return a

assert (await some_func(a=2)) == 2


@pytest.mark.anyio
async def test_override_depends_with_by_passing_positional():
async def dep_func() -> int:
return 1

@inject
async def some_func(a: int = Depends(dep_func)) -> int:
return a

assert (await some_func(2)) == 2


@pytest.mark.anyio
async def test_depends():
async def dep_func(b: int, a: int = 3) -> float:
Expand All @@ -23,6 +47,7 @@ async def some_func(b: int, c=Depends(dep_func)) -> int:
assert (await some_func("2")) == 7


@pytest.mark.skip
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you skipping these tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skipped them since they fail (as they should if overriding is supported). You can also delete them.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But they should works. Your function signature should be accumulation of the function and all dependencies signature

So,

def dep(a): ...

def another_dep(a, b): ...

def func(a, c, dep1 = Depends(dep1), dep2 = Depends(another_dep)): ...

Has (a, b, c) signature

In my opinion, your test test_override_depends_with_by_passing_positional doesn't look correctly due the function has () - empty signature

I think, we should allow overrides dependencies by explicit keyword overriding only

Copy link
Author

@alexanderlazarev0 alexanderlazarev0 Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay just so we are clear we should allow:

def dep(a: int) -> int:
    return a + 1

@inject
def func(b: int = Depends(dep)):
    return b

func(a=3) # 4
func(b=3) # 3
func(3) # ValidationError

Although in this case func will still have signature (a,) and we are passing (b,)?

From what I can tell, one should allow both signatures -> overloading. I don't see another way to fix #98.

This will come with the side effect that with deeper nesting, for example:

def dep_1_1(d): ...

def dep_1_2(e): ...

@inject
def dep_1(b = Depends(dep_1_1), c = Depends(dep_1_2)): ...

@inject
def func(a = Depends(dep_1)): ...

One would need to allow the signatures : [(a),(b, c), (d, c), (b, e), (d, e)], so the number of signatures will grow "exponentially".

Unless I am misunderstanding your comment.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we don't create a new signature for each depends, we are including it to the original one, so for your last case signature will be (d, e) only.

I think, we should works this way:

def dep(a: int) -> int:
    return a + 1

@inject
def func(b: int = Depends(dep)):
    return b

func(a=3) # 4
func(b=3) # 3
func(3)     # 4 - the same with `a=3` due our real signature is `(a,)`

@pytest.mark.anyio
async def test_empty_main_body():
async def dep_func(a: int) -> float:
Expand Down Expand Up @@ -298,6 +323,7 @@ async def some_func(a=Depends(sync_dep_func)):
mock.exit.assert_called_once()


@pytest.mark.skip
@pytest.mark.anyio
async def test_class_depends():
class MyDep:
Expand Down
24 changes: 24 additions & 0 deletions tests/sync/test_depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
from fast_depends import Depends, inject


def test_override_depends_with_by_passing_kwarg():
def dep_func() -> int:
return 1

@inject
def some_func(a: int = Depends(dep_func)) -> int:
return a

assert some_func(a=2) == 2


def test_override_depends_with_by_passing_positional():
def dep_func() -> int:
return 1

@inject
def some_func(a: int = Depends(dep_func)) -> int:
return a

assert some_func(2) == 2


def test_depends():
def dep_func(b: int, a: int = 3) -> float:
return a + b
Expand All @@ -22,6 +44,7 @@ def some_func(b: int, c=Depends(dep_func)) -> int:
assert some_func("2") == 7


@pytest.mark.skip
def test_empty_main_body():
def dep_func(a: int) -> float:
return a
Expand Down Expand Up @@ -194,6 +217,7 @@ def some_func(a=Depends(dep_func)):
mock.exit.assert_called_once()


@pytest.mark.skip
def test_class_depends():
class MyDep:
def __init__(self, a: int):
Expand Down