Replies: 3 comments
-
I would prefer not to have something like that as it would tie each value to the type returned by the dependency while it's already tied to the dependable object (the function), so it would add complexity and cognitive burden, to understand how it works in some situations but it's different in others. It would also look like a request body. Nevertheless, you can do: async def get_user() -> User:
return user
app.include_router(
dependencies=[Depends(get_user)],
)
def method_under_router(user: User = Depends(get_user)): and it would do the same. It would not call the dependency twice, as there's a dependency cache in place (that you can disable). With that you exchange the proposed |
Beta Was this translation helpful? Give feedback.
-
I agree with @tiangolo, I think this is likely to quickly become an unmaintainable source of confusion if used heavily in a larger codebase. I personally use this cbv pattern to reduce the need to repeat this sort of dependency logic, though obviously it's a very different change than proposed here. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the comments - I'll close the issue. |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem
Right now we have dependencies injection for API router, which can easily wrap API calls and run some logic before passing it to the method.
Besides that, if we want to access any value generated by the dependency we need to declare it for each path/api call.
The solution you would like
Add optional keyword argument to the Depends class, called name.
In regular usage of Depends it ignores it, but if used on APIRoute it delivers the result of that dependency as that keyword argument to the function.
for example:
I would like feedback before implementing it myself. I got this idea from @jklang on gitter and it sounds like a good feature to have.
Beta Was this translation helpful? Give feedback.
All reactions