click icon indicating copy to clipboard operation
click copied to clipboard

get_parameter_source does not work with invoke or forward

Open bretwalker opened this issue 1 year ago • 1 comments

When invoking one Click command from another using either invoke or forward, get_parameter_source does not appear to work as it does if the command were called directly.

Sample code:

import click

cli = click.Group()

@cli.command()
@click.option('--count', default=1)
@click.pass_context
def test(ctx, count):
    click.echo(f'Count: {count}')
    click.echo(f'ctx.get_parameter_source: {ctx.get_parameter_source("count")}')
    click.echo(f'ctx.parent.get_parameter_source: {ctx.parent.get_parameter_source("count") if ctx.parent else None}')

@cli.command()
@click.option('--count', default=1)
@click.pass_context
def dist(ctx, count):
    click.echo("Calling directly")
    test(standalone_mode=False)
    click.echo("Forwarding")
    ctx.forward(test)
    click.echo("Invoking")
    ctx.invoke(test, count=42)

if __name__ == '__main__':
    dist()

Results:

Calling directly
Count: 1
ctx.get_parameter_source: ParameterSource.DEFAULT
ctx.parent.get_parameter_source: None
Forwarding
Count: 1
ctx.get_parameter_source: None
ctx.parent.get_parameter_source: ParameterSource.DEFAULT
Invoking
Count: 42
ctx.get_parameter_source: None
ctx.parent.get_parameter_source: ParameterSource.DEFAULT

I would expect ctx.get_parameter_source to be ParameterSource.DEFAULT for the calls made using invoke and forward or for the differing behavior to be explicitly mentioned in the get_parameter_source documentation. I ran into this issue when wrapping a dbt command, so I believe I'm not the only one that expects not to have to check a parent's get_parameter_source (dbt source).

Thank you for your help.

Environment:

  • Python version: Python 3.9.17
  • Click version: 8.1.7

bretwalker avatar Jul 15 '24 17:07 bretwalker

Any news on this? I've just run into the issue myself, and it is very confusing to debug and breaks expectations pretty badly.

mathrick avatar Oct 11 '24 22:10 mathrick