Replies: 1 comment
-
|
Hi, After I asked, I searched some articles, posts, and I found out it's impossible because Python does not support this grammar not like Javascript: let getPrice = function( price, tax = price * taxRate() ) {
return price + tax;
}I decided to add my dependency method dynamically into an instance like this: authentication = get_authentication_backend() # LRU cached instance
async def get_superuser(
self, # Because it will a method of instance.
current_user: dict = Depends(authentication.get_current_user), # instance.method notation is now available.
conn: AsyncConnection = Depends(engine_connect),
) -> dict:
...
authentication.get_superuser = get_superuser.__get__(authentication)Now I can use method dependency in API function: Depends(authentication.get_superuser)It must be ugly and not an elegant solution, but I am satisfied with authentication methods and dependencies which are living together in a class. Thank you. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I have another question about dependency functions.
The official documentation explains dependency functions and class constructors.
Is it possible to define them as methods of a class?
For example, suppose that I've got source code like this:
I cannot write the code like this because
selfis not defined atself.sub_dependency.Do I have to define all dependencies as functions?
Of course, it is not a FastAPI issue. It's rather Python grammar issue. However, I hope to get your opinion and help.
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions