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

make_pass_decorator strips argument #786

Closed
ericfrederich opened this issue May 15, 2017 · 1 comment · Fixed by #787
Closed

make_pass_decorator strips argument #786

ericfrederich opened this issue May 15, 2017 · 1 comment · Fixed by #787

Comments

@ericfrederich
Copy link
Contributor

The inner most wrapper of make_pass_decorator invokes a function with *args[1:] which strips an argument.

A smaller example could be made but the following is the invoking other commands example from the docs modified to also take in a custom config object.

The only difference between dist_working and dist_broken is the order of the decorators and consequently the parameters they take.

In my opinion they should both be valid and work. What happens in dist_broken is, like the title says, the make_pass_decorator wrapper invokes the function with *args[1:] which removes the context object put there by the previous decorator.

import click


class MyConfig(object):
    def __init__(self, verbose):
        self.verbose = verbose


pass_config = click.make_pass_decorator(MyConfig)


@click.group()
@click.option('--verbose', '-v', is_flag=True)
@click.pass_context
def cli(ctx, verbose):
    ctx.obj = MyConfig(verbose=verbose)


@cli.command()
@click.option('--count', default=1)
def test(count):
    click.echo('Count: %d' % count)


@cli.command('dist-working')
@click.option('--count', default=1)
@pass_config
@click.pass_context
def dist_working(ctx, cfg, count):
    if cfg.verbose:
        click.echo('about to forward')
    ctx.forward(test)
    if cfg.verbose:
        click.echo('about to invoke')
    ctx.invoke(test, count=42)


@cli.command('dist-broken')
@click.option('--count', default=1)
@click.pass_context
@pass_config
def dist_broken(cfg, ctx, count):
    if cfg.verbose:
        click.echo('about to forward')
    ctx.forward(test)
    if cfg.verbose:
        click.echo('about to invoke')
    ctx.invoke(test, count=42)
@ericfrederich
Copy link
Contributor Author

There is a fix for this. Can we consider this a bug and get the fix merged?

sirosen pushed a commit to ericfrederich/click that referenced this issue Aug 11, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants