pallets / pallets/click

get_parameter_source does not work with invoke or forward

Open
#2,753 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

parsing
Dominant language
Python
Stars
17.7k
Forks
2.3k
Avg merge
1d 30m
Merged PRs (30d)
18

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the sample with Click 8.1.7 and compare get_parameter_source during direct calls, forward, and invoke. Trace the Context get_parameter_source, forward, and invoke entry points to determine how parameter sources are resolved; done means the invoked commands report the expected source, with tests covering both paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.