FastDepends icon indicating copy to clipboard operation
FastDepends copied to clipboard

Overridden arguments not considered

Open reritom opened this issue 1 year ago • 1 comments

Perhaps related to the comment on here about the library not being a real dependency injection system https://github.com/Lancetnik/FastDepends/issues/65

Using version 2.4.3

When passing an explicit argument for a parameter defined as a dependency, I would expect the explicit argument to be used and the dependency to be overridden/ignored, but it doesn't seem to be the case.

from fast_depends import inject, Depends

def my_dependency():
    return "foo"

@inject
def my_function(dep = Depends(my_dependency)):
    print(dep)

my_function()
# foo

my_function(dep="bar")
# foo

reritom avatar Jun 04 '24 17:06 reritom

The reason this doesn't work seems to be due to the fact that this is supported:

def test_class_depends():
    class MyDep:
        def __init__(self, a: int):
            self.a = a

    @inject
    def some_func(a=Depends(MyDep)):
        assert isinstance(a, MyDep)
        assert a.a == 3
        return a

    some_func(3)

Ref

So also this works:

from fast_depends import Depends, inject

def my_dep(a: int):
    return a + 5

@inject
def some_func(a=Depends(my_dep)):
    return a

some_func(3) # 8

xelandernt avatar Jul 09 '24 01:07 xelandernt

Seems working fine in 3.0.0 Please, reopen the Issue if the problem still exist

Lancetnik avatar Oct 12 '25 13:10 Lancetnik