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

Conversation

alexanderlazarev0
Copy link

def dep_func() -> int:
    return 1

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

print(some_func(a=2)) # 2
print(some_func(2)) # 2

@Lancetnik
Copy link
Owner

Can you, please, revert docs/ and styles changes to let me check logic only?

@alexanderlazarev0
Copy link
Author

Can you, please, revert docs/ and styles changes to let me check logic only?

@Lancetnik reset formatting, I had run ruff format before commit.

@@ -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,)`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants