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

QUESTION. Does kink do partial injections? #52

Closed
4l1fe opened this issue Feb 6, 2024 · 1 comment
Closed

QUESTION. Does kink do partial injections? #52

4l1fe opened this issue Feb 6, 2024 · 1 comment

Comments

@4l1fe
Copy link

4l1fe commented Feb 6, 2024

Hey, very nice and lighweight di library!

I'm trying to inject a positional argument to a descendent decorator while it has more positionals.
The piece of code, it's more likely incorrent, but whatever:

def command(name, description, *scopes):
    
    def wrapped_func(func):
        
        async def wrapped(*args, **kwargs):
            cmd = BotCommand(name, description)
            for scope in scopes:
                SCOPES[scope].append(cmd)
            
            di[BotCommand] = cmd
            result = await func(*args, **kwargs)
            return result
        
        return wrapped
    
    return wrapped_func


@inject
def handler(bot_cmd: BotCommand, *handler_args, handler_class=CommandHandler, **handler_params):
    
    def wrapped_func(func):
        _handler = handler_class(bot_cmd.command, func, *handler_args, **handler_params)
        APP_HANDLERS.append(_handler)

        async def wrapped(*args, **kwargs):
            result = await func(*args, **kwargs)
            return result

        return wrapped

    return wrapped_func


@command('stop', 'Stop a container', BotCommandScopeChat)
@handler(StartStopArgs, filters=filters.User(user_id=1))
async def stop_container(*args):
    print(args)

For now, an error is raised:

raise ExecutionError(
kink.errors.execution_error.ExecutionError: Cannot execute function without required parameters. Did you forget to bind the following parameters: `handler_args`, `handler_params` inside the service `<function handler at 0x7fe16b1b8400>`?

To be honest, i expected that di only injectes the bot_cmd argument and leave the others handler_args, handler_params as is, not check them for beign in a container.
Could it be defined to make a partial check?

@dkraczkowski
Copy link
Contributor

@4l1fe Thanks for the feedback.

No, not really. The entire idea behind the exception is to support autowiring and let people know what is still missing. You would need to call the function and set the arguments in the call (even if they are set to None) to superpass this behaviour.

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