fastapi / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.
https://typer.tiangolo.com/
MIT License
15.66k stars 666 forks source link

✨ Upgrade `autocompletion` functionality for compatibility with `shell_complete` #1006

Open bckohan opened 4 weeks ago

bckohan commented 4 weeks ago

Cf issue: #949

Note, this includes (and supersedes) the changes from #974 and implements these suggestions https://github.com/fastapi/typer/issues/949#issuecomment-2345047182, with a few differences.

I attempted to push these changes to #974 but was asked to open a new PR here instead.

This PR does 3 things:

  1. Allows autocompletion functions to accept a click.core.Parameter argument. I think this is necessary because without it you can't define generic completer functions that don't know what CLI options or arguments they may be attached to and that need to alter their behavior based on how click has processed the arguments before the incomplete argument. Concrete example: you have an option or argument that accepts multiple values but you do not wish those values to be repeated.

  2. Allows CompletionItems to be returned in addition to strings and 2-tuples from autocompletion functions.

  3. Fixes arg: List[str] parameter to be passed what it used to be given in click 7 (i.e. the raw command line string minus the script and incomplete string). In click 7 this parameter was being set to the arguments passed in the environment variables via the completion scripts installed on the system that are invoked when you hit tab. These parameters do not show up in ctx.args or sys.argv. Click has a general purpose obj attribute on the context that is available for downstream use. This PR sets that object to a dictionary in the completer classes and attaches the args from the environment variables to it. If obj is already defined it'll just not do that and nothing will break accept the args will be passed as an empty list to the autocompletion functions.

I think numbers 1 and 2 are important. Number 3 is mostly for backwards compatibility but I don't think its that critical - I just wanted to show that there is a way to do it using public Click 8 interfaces.

I did not update the docs but would be happy to if ya'll are inclined to accept this PR.

github-actions[bot] commented 4 weeks ago

πŸ“ Docs preview for commit 249b70ea3e3ebb5d86320332fce9b1e8587da1f9 at: https://6ae37f21.typertiangolo.pages.dev

Modified Pages

github-actions[bot] commented 4 weeks ago

πŸ“ Docs preview for commit c5aab0adb8a626412720a49c1a0eb8dcc4cf68d6 at: https://9a5f770f.typertiangolo.pages.dev

Modified Pages

svlandeg commented 3 days ago

Thanks for the PR! We'll get this reviewed and get back to you πŸ™

github-actions[bot] commented 3 days ago

πŸ“ Docs preview for commit 340c459249cdbc89fb642aa506f74b0bf03c63f8 at: https://985aa99d.typertiangolo.pages.dev

Modified Pages

svlandeg commented 3 days ago

@bckohan: the tests are failing because there isn't a 100% code coverage with the new commits. Could you look into this? πŸ™