Lancetnik / FastDepends

FastDepends - FastAPI Dependency Injection system extracted from FastAPI and cleared of all HTTP logic. Async and sync modes are both supported.
https://lancetnik.github.io/FastDepends/
MIT License
245 stars 8 forks source link

Overridden arguments not considered #98

Open reritom opened 1 month ago

reritom commented 1 month ago

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
alexanderlazarev0 commented 2 days ago

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