kodemore / kink

Dependency injection container made for Python
MIT License
397 stars 25 forks source link

QUESTION. Does kink do partial injections? #52

Closed 4l1fe closed 5 months ago

4l1fe commented 7 months ago

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 commented 5 months ago

@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.