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

click integration - provide options that translate into a context value #229

Open
RonnyPfannschmidt opened this issue Aug 28, 2024 · 5 comments

Comments

@RonnyPfannschmidt
Copy link

currently the click integration cannot pass options into the context easily

i'd like to be able to do something like the following (possibly with custom scopes)

import click
from dishka import make_container, from_context, Provider, 
from dishka.integrations.click import FromDishka, setup_dishka, dishka_option

UserName = NewType("Userame", str|None)

class Interactor:
   def __init__(self, user: Username):
      self.user = user
   def __call__(self):
       return self.user


class FunProvider(Provider)
   username = from_context(UserName)
   interactor = provide(Interactor)




@click.group()
@click.pass_context
def main(context: click.Context):
    container = make_container(FunProvider)
    setup_dishka(container=container, context=context, auto_inject=True)

@main.command()
@click.option("--count", default=1, help="Number of greetings.")
@dishka_option("--username", default=None, provides=UserName)
def hello(count: int, interactor: FromDishka[Interactor]):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo(f"Hello {interactor()}!")

main()
@Tishka17
Copy link
Collaborator

Currently click integration does not enter context on command. This is done because often there is another framework with its own scopes logic.

Here username looks more like date for interactor, than something related to dependencies. But anyway, we'll think more

@RonnyPfannschmidt
Copy link
Author

this could be summarized as "pass in dependency configuration for the container in some way

i realize that the inject/group hlpers pull together the whole picture

i want to be able to easily compose the Context data for entering

@Tishka17
Copy link
Collaborator

From my current point of view groups is the place for configuration of app+container. Command - is one of the scenarios to trigger. We do not pass parsed data from web framework handlers to container, same is here.

But anyway, it is a place for improvement, though it is not yet clear in which way we should develop

@Tishka17
Copy link
Collaborator

One of the options here is to enter nested scope manually inside command handler. You can pass context to context manager.

If you need, you can create container with scope RUNTIME, so the next scope will be APP

@RonnyPfannschmidt
Copy link
Author

In my case, I'm using custom scope's anyway because it's not a webapp

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

No branches or pull requests

2 participants