typer icon indicating copy to clipboard operation
typer copied to clipboard

typer.Argument with autocompletion

Open mdantonio opened this issue 3 years ago • 8 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the Typer documentation, with the integrated search.
  • [X] I already searched in Google "How to X in Typer" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to Typer but to Click.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

Not working:
    myparam: str = typer.Argument(
        "",
        shell_complete=my_autocomplete,
    )

Working:
    myparam: str = typer.Option(
        "",
        shell_complete=my_autocomplete,
    ),

Description

I'm trying to enable shell complete on an Argument but it does not work. On the opposite with Options everything is working as expected

I think that the same problem was already in the previous versions of typer (so that it is NOT introduced by typer 0.4.0 & click 8... but i'm not totally sure about that)

Based on tests on click, shell_complete on arguments should work: https://github.com/pallets/click/blob/48cb86d85fc1abfcf1478ee660e42aaa5382fd64/examples/completion/completion.py#L25

in typer I can't find tests with arguments, I only found this with option https://github.com/tiangolo/typer/blob/a1520dcda685220a9a796288f5eaaebd00d68845/tests/assets/compat_click7_8.py#L20

Operating System

Linux

Operating System Details

Ubuntu 20.04

Typer Version

0.4.0

Python Version

Python 3.9.5

Additional Context

click 8.0.1

mdantonio avatar Oct 06 '21 08:10 mdantonio

this (mostly) works for me using typer.Argument('default', autocompletion=my_autocomplete) tho am running into a weird bug with pwsh/win11 vs bash/osx

patricksurry avatar Feb 21 '22 13:02 patricksurry

@patricksurry autocompletion works, but it is deprecated in click

DeprecationWarning: 'autocompletion' is renamed to 'shell_complete'. The old name is deprecated and will be removed in Click 8.1. See the docs about 'Parameter' for information about new behavior.

I'm still unable to make Arguments work with shell_complete.

On the opposite, shell_complete works very well with Options.

This is the situation, at least for me:

autocomplete shell_completion
Option :heavy_check_mark: :heavy_check_mark:
Argument :heavy_check_mark: :heavy_multiplication_x:

mdantonio avatar Feb 27 '22 12:02 mdantonio

which shell are you using with --install-completion? my problem turned out to be a bug with powershell completion

does that mean the typer docs are out of date? they don't seem to mention shell_completion. i wonder if the deprecation warning itself (via logging.warn) might actually mess things up since it could be generating spurious output in the completion execution path (e.g. when the _TYPER... env vars are set)?

patricksurry avatar Feb 27 '22 17:02 patricksurry

I'm using bash (on Ubuntu 20.04). But I assume (maybe I'm wrong) that completion should work on the shell because it works with Options. In case of troubles at shell level I was expecting the completion to don't work at all

You are right about the docs, no mention of shell_complete. shell_complete has been added with typer 0.4.0 to support click 8 (with this PR https://github.com/tiangolo/typer/pull/317)

There is a test with typer.Option + shell_complete https://github.com/tiangolo/typer/blob/a1520dcda685220a9a796288f5eaaebd00d68845/tests/assets/compat_click7_8.py#L20

But nothing with typer.Argument + shell_complete

The lack of documentation may suggest that the support is still not completed. I think that only @tiangolo can clarify

mdantonio avatar Feb 28 '22 05:02 mdantonio

I experience exactly the same behaviour / error: my env: fish 3.3.1 typer: 0.4.1 click: 8.1.2

torstello avatar Apr 20 '22 09:04 torstello

This issue seems to be fixed, at least when using Ubuntu 22.04, env: typer: 0.9.0 click: 8.1.7

CasperWA avatar Dec 12 '23 14:12 CasperWA

This is in fact a very simple bug. The shell_complete parameter is not passed to TyperArgument. See here.

I will submit a PR.

bckohan avatar Jan 24 '24 20:01 bckohan

If anyone has the ability to switch this issue label to a bug that would be appreciated!

It might be a while before my PR is merged so, here's the most minimally invasive monkey patch I could come up with to fix this:


from typer import __version__

if (0, 4, 0) <= tuple(int(v) for v in __version__.split(".")) <= (0, 13, 0):
    from typer import main as typer_main
    from typer.models import ParamMeta

    upstream_get_click_param = typer_main.get_click_param

    def patched_get_click_param(
        param: ParamMeta,
    ) -> t.Tuple[t.Union[click.Argument, click.Option], t.Any]:
        """
        Patch this bug: https://github.com/tiangolo/typer/issues/334
        """
        click_param = upstream_get_click_param(param)
        if isinstance(click_param[0], click.Argument) and getattr(
            param.default, "shell_complete", None
        ):
            click_param[0]._custom_shell_complete = param.default.shell_complete
        return click_param

    typer_main.get_click_param = patched_get_click_param

bckohan avatar Jan 31 '24 19:01 bckohan