-
Notifications
You must be signed in to change notification settings - Fork 51
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
feat: typer integration #332
base: develop
Are you sure you want to change the base?
Conversation
we have moved from |
c7d4f1f
to
40ce9b6
Compare
I have addressed the comments and ensured that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we should pass typer.Context
to dishka context as well to provide users with an ability use CLI options and flags in factories.
Just smth like we do for another integrations - https://github.com/reagento/dishka/blob/develop/src/dishka/integrations/fastapi.py#L64
I'm not 100% sure on what we could achieve that way, would you have an example? Would it be accessing the flags of a callback or a higher level command? |
You should wrap the original handler by your custom decorator with context like this: from functools import wraps
from typer import Typer, Context
app = Typer()
def inject(func):
def wrapper(ctx: Context):
# we should pass this `ctx` object to dishka context
print(ctx.args)
return func()
return wrapper
@app.command()
@inject
def main():
pass
if __name__ == "__main__":
app() But, you should patch |
You can refer to fastapi integration to see how we process arguments when decorating |
0607238
to
6543fd1
Compare
container = context.meta[CONTAINER_NAME] | ||
|
||
req_container = context.with_resource( | ||
container({typer.Context: context}, scope=Scope.REQUEST), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think we should do it. I treat typer
as well as click
as framework which actually just runs app as a whole: inside click/typer handlers there can be other frameworks started (like fastapi, etc.)
So, from my point of view, expected behavior for typer handler is to have Scope.APP, not REQUEST. Let't keep the logic we have for click.
I see the problem now: you cannot pass Context
because APP-scoped container is already created. My fault, let's rollback and ignore this requirement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you go back to typer.Context
not being available to providers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. We cannot pass it here, so let it be user's problem.
Integration With Typer
Following #329 requesting whether we could contribute an integration with Typer, here is an initial contribution.
My implementation is taking the click integration as a basis, with two main differences:
Typer
, we can take in atyper.Typer
app directly to access commands and subcommandssetup_dishka
, when usingauto_inject=True
should be called after the commands have been added to the appDon't hesitate to provide feedback or ask for modifications.