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

feat: typer integration #332

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

Slyces
Copy link

@Slyces Slyces commented Jan 7, 2025

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:

  • With Typer, we can take in a typer.Typer app directly to access commands and subcommands
  • Container injection is done through a callback which should run before every command
  • setup_dishka, when using auto_inject=True should be called after the commands have been added to the app

Don't hesitate to provide feedback or ask for modifications.

@Slyces Slyces changed the title feat: provide a typer integration, highly inspired by the click one feat: typer integration Jan 7, 2025
@Tishka17
Copy link
Member

Tishka17 commented Jan 7, 2025

we have moved from tox to nox, please update test run config

@Slyces Slyces force-pushed the feat/typer-integration branch from c7d4f1f to 40ce9b6 Compare January 7, 2025 16:27
@Slyces
Copy link
Author

Slyces commented Jan 7, 2025

I have addressed the comments and ensured that nox runs successfully locally!

tox.ini Outdated Show resolved Hide resolved
Copy link
Contributor

@Lancetnik Lancetnik left a 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

@Slyces
Copy link
Author

Slyces commented Jan 7, 2025

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?

@Lancetnik
Copy link
Contributor

Lancetnik commented Jan 7, 2025

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 wrapper method signature to be a copy of original command with added Context option. You can take a look at signature patching in dishka sources

@Tishka17
Copy link
Member

Tishka17 commented Jan 7, 2025

You can refer to fastapi integration to see how we process arguments when decorating

@Slyces Slyces force-pushed the feat/typer-integration branch from 0607238 to 6543fd1 Compare January 8, 2025 13:46
container = context.meta[CONTAINER_NAME]

req_container = context.with_resource(
container({typer.Context: context}, scope=Scope.REQUEST),
Copy link
Member

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.

Copy link
Author

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?

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants