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

keyword arguments resolved to None instead of default values when using inject(cast=False) #115

Closed
marcodlk opened this issue Jul 19, 2024 · 3 comments · Fixed by #117
Closed
Labels
bug Something isn't working

Comments

@marcodlk
Copy link

Hi, first of all - thanks for creating fastdepends. Big fan!

Recently, ran into some unexpected behavior which does not appear intended. When inject(cast=False) is used, keyword arguments are resolved to None instead of the default value. For example, in the code below, get_hello() returns "hello, None" instead of "hello, world".

from fast_depends import Depends, inject

def get_world(value: str = "world"):
    return value

@inject(cast=False)
def get_hello(world: str = Depends(get_world)):
    return f"hello, {world}"

print(get_hello())  # Output: hello, None

When inject(cast=True) is used, the default values are used as expected.

The source of the issue appears to be the following lines in core/build.py:

if param.kind is param.KEYWORD_ONLY:
keyword_args.append(param_name)
elif param_name not in ("args", "kwargs"):
positional_args.append(param_name)

In this case, param.kind is param.POSITIONAL_OR_KEYWORD, which is not param.KEYWORD_ONLY, so the param_name gets added to the positional_args list instead of keyword_args.

Is this intended behavior? It was unexpected to me and feels like an inconsistency.

@Lancetnik Lancetnik added the bug Something isn't working label Jul 19, 2024
@Lancetnik
Copy link
Owner

@marcodlk thank you for the Issue! I think, this is the bug and we should fix it. Unfortunatelly, I am too busy with FastStream now, but planning to make FastDepends sprint next month

@marcodlk
Copy link
Author

Sounds good. Not too much rush - thanks to your flexible design, I can just use a wrap_model function to make necessary patch in the meantime :)

@marcodlk
Copy link
Author

Ok it will be a bit trickier to patch than I thought. I believe the following line will need to be changed...

kwargs_ = {arg: solved_kw.get(arg) for arg in keyword_args}

...to something like this:

kwargs_ = {arg: solved_kw[arg] for arg in keyword_args if arg in solved_kw}

In the current logic, the solved_kw does not contain the value, so the params in keyword_args get value set to None and the None is injected as the keyword arg in the function call, overwriting the default.

Cooh12 added a commit to Cooh12/FastDepends that referenced this issue Jul 24, 2024
@Cooh12 Cooh12 mentioned this issue Jul 24, 2024
Lancetnik pushed a commit that referenced this issue Jul 24, 2024
Lancetnik added a commit that referenced this issue Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants