question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How can I attach a dependency outside function parameters?

See original GitHub issue

Description

Is it possible to attach a dependency outside function parameters??

Additional context

For example, I want to move this “UserCan” dependency in a decorator somehow, because I would not use the user_id it returns while it must enforce the security policies it implements.

The reason is because linters will warn about unused variables…

@router.get('/{username}', summary='Get user profile details',
            response_model=UserProfileResponse)
async def get_user_profile(username: str,
                           user_id: str = Depends(UserCan('do_ordinary_stuff'))):

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
tiangolocommented, May 16, 2019

This is now implemented in version 0.22.0 🎉

You can do:

@app.get("/items/", dependencies=[Depends(verify_token), Depends(verify_key)])
async def read_items():
    return [{"item": "Foo"}, {"item": "Bar"}]

The new docs are here: https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/


And you can also use it in routers, like:

app.include_router(
    items.router,
    prefix="/items",
    tags=["items"],
    dependencies=[Depends(get_token_header)],
)

The relevant docs are here: https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-prefix-tags-responses-and-dependencies

(This should solve your use case @cjw296 )

7reactions
mths0x5fcommented, May 16, 2019

Oh, gosh, I love you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to share the same parameters between dependencies ...
I'm trying to use the same parameters in my endpoint functions and dependencies. ... But this will work incorrectly. If we pass 'swearing'...
Read more >
Dependencies - First Steps - FastAPI - tiangolo
Let's first focus on the dependency. It is just a function that can take all the same parameters that a path operation function...
Read more >
Dependency Injection and Default Parameters - Kyle Shevlin
Learn how to increase the flexibility and testability of your programs by passing dependencies in as function arguments alongside default ...
Read more >
Dependency Walker Frequently Asked Questions (FAQ)
To do this, start a command prompt. Type "SET _NO_DEBUG_HEAP=1". Then start Dependency Walker from that command line. This should disable the debug...
Read more >
Dependency injection in Android
Have it supplied as a parameter. The app can provide these dependencies when the class is constructed or pass them in to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found