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

typer.Argument with autocompletion #334

Closed
7 tasks done
mdantonio opened this issue Oct 6, 2021 · 10 comments · Fixed by #737
Closed
7 tasks done

typer.Argument with autocompletion #334

mdantonio opened this issue Oct 6, 2021 · 10 comments · Fixed by #737
Labels
bug Something isn't working

Comments

@mdantonio
Copy link

First Check

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

Commit to Help

  • 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 mdantonio added the question Question or problem label Oct 6, 2021
@patricksurry
Copy link
Contributor

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

@mdantonio
Copy link
Author

@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 ✔️ ✔️
Argument ✔️ ✖️

@patricksurry
Copy link
Contributor

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)?

@mdantonio
Copy link
Author

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 #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

@torstello
Copy link

torstello commented Apr 20, 2022

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

@CasperWA
Copy link

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

@bckohan
Copy link
Contributor

bckohan commented Jan 24, 2024

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
Copy link
Contributor

bckohan commented Jan 31, 2024

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

@svlandeg svlandeg added bug Something isn't working and removed question Question or problem labels Apr 19, 2024
@svlandeg svlandeg linked a pull request Apr 19, 2024 that will close this issue
@svlandeg
Copy link
Member

svlandeg commented Sep 5, 2024

Hi @mdantonio, @patricksurry, @torstello and @bckohan! I would like to draw your attention to #949 and #974.

In summary, we've decided to stick to autocompletion as the name of the parameter, and to not support shell_complete going forward (it wasn't really functional or documented anyway).

I just wanted to check with you whether that works for you, or whether there are use-cases you can't cover with the original autocompletion functionality.

@mdantonio
Copy link
Author

makes sense, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants