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
313 stars 14 forks source link

Dependency overrides don't work as expected #138

Open reritom opened 1 month ago

reritom commented 1 month ago

When using dependency_provider.dependency_overrides, the override seems to overwrite the function in the dependency, but still passes the new function the resolved dependencies of the old function. I would expect the old functions dependencies to be ignored in the overridden context.

from fast_depends import Depends, dependency_provider, inject

def get_parent_dep():
    return 1

def get_dep(parent_dep=Depends(get_parent_dep)):
    return parent_dep + 1

@inject
def my_func(dep=Depends(get_dep)):
    print(dep + 1)

my_func() # Normal, as expected

dependency_provider.dependency_overrides[get_dep] = lambda: 100

my_func() # Error as my override lambda is being passed a dependency from the old get_dep function
(.venv) $ python test.py 
3
Traceback (most recent call last):
  File "main.py", line 21, in <module>
    my_func()
  File "/.venv/lib/python3.9/site-packages/fast_depends/use.py", line 169, in injected_wrapper
    r = real_model.solve(
  File ".venv/lib/python3.9/site-packages/fast_depends/core/model.py", line 370, in solve
    dep.solve(
  File ".venv/lib/python3.9/site-packages/fast_depends/core/model.py", line 416, in solve
    response = call(*final_args, **final_kwargs)
TypeError: <lambda>() got an unexpected keyword argument 'parent_dep'

Using version 2.4.11

Lancetnik commented 1 month ago

Thank you for the reporting! I know about this problem and it fixed already in 3.0.0 Please, wait a bit