Before checking how to provide custom completions, let's check again how it works.
After installing completion for your own Python package (or using the typer command), when you use your CLI program and start adding a CLI option with -- an then hit TAB, your shell will show you the available CLI options (the same for CLI arguments, etc).
To check it quickly without creating a new Python package, use the typer command.
Then let's create small example program:
importtyperfromtyping_extensionsimportAnnotatedapp=typer.Typer()@app.command()defmain(name:Annotated[str,typer.Option(help="The name to say hi to.")]="World"):print(f"Hello {name}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtyperapp=typer.Typer()@app.command()defmain(name:str=typer.Option("World",help="The name to say hi to.")):print(f"Hello {name}")if__name__=="__main__":app()
And let's try it with the typer command to get completion:
fast →💬 Hit the TAB key in your keyboard below where you see the: [TAB]typer ./main.py [TAB][TAB] 💬 Depending on your terminal/shell you will get some completion like this ✨run -- Run the provided Typer app. utils -- Extra utility commands for Typer apps.
💬 Then try with "run" and --typer ./main.py run --[TAB][TAB] 💬 You will get completion for --name, depending on your terminal it will look something like this--name -- The name to say hi to.
💬 And you can run it as if it was with Python directlytyper ./main.py run --name Camila Hello Camila
Right now we get completion for the CLI option names, but not for the values.
We can provide completion for the values creating an autocompletion function, similar to the callback functions from CLI Option Callback and Context:
importtyperfromtyping_extensionsimportAnnotateddefcomplete_name():return["Camila","Carlos","Sebastian"]app=typer.Typer()@app.command()defmain(name:Annotated[str,typer.Option(help="The name to say hi to.",autocompletion=complete_name)]="World",):print(f"Hello {name}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtyperdefcomplete_name():return["Camila","Carlos","Sebastian"]app=typer.Typer()@app.command()defmain(name:str=typer.Option("World",help="The name to say hi to.",autocompletion=complete_name),):print(f"Hello {name}")if__name__=="__main__":app()
We return a list of strings from the complete_name() function.
And then we get those values when using completion:
fast →typer ./main.py run --name [TAB][TAB] 💬 We get the values returned from the function 🎉Camila Carlos Sebastian
Right now, we always return those values, even if users start typing Sebast and then hit TAB, they will also get the completion for Camila and Carlos (depending on the shell), while we should only get completion for Sebastian.
But we can fix that so that it always works correctly.
Modify the complete_name() function to receive a parameter of type str, it will contain the incomplete value.
Then we can check and return only the values that start with the incomplete value from the command line:
importtyperfromtyping_extensionsimportAnnotatedvalid_names=["Camila","Carlos","Sebastian"]defcomplete_name(incomplete:str):completion=[]fornameinvalid_names:ifname.startswith(incomplete):completion.append(name)returncompletionapp=typer.Typer()@app.command()defmain(name:Annotated[str,typer.Option(help="The name to say hi to.",autocompletion=complete_name)]="World",):print(f"Hello {name}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtypervalid_names=["Camila","Carlos","Sebastian"]defcomplete_name(incomplete:str):completion=[]fornameinvalid_names:ifname.startswith(incomplete):completion.append(name)returncompletionapp=typer.Typer()@app.command()defmain(name:str=typer.Option("World",help="The name to say hi to.",autocompletion=complete_name),):print(f"Hello {name}")if__name__=="__main__":app()
Now let's try it:
fast →typer ./main.py run --name Ca[TAB][TAB] 💬 We get the values returned from the function that start with Ca 🎉Camila Carlos
But some shells (Zsh, Fish, PowerShell) are capable of showing extra help text for completion.
We can provide that extra help text so that those shells can show it.
In the complete_name() function, instead of providing one str per completion element, we provide a tuple with 2 items. The first item is the actual completion string, and the second item is the help text.
So, in the end, we return a list of tuples of str:
importtyperfromtyping_extensionsimportAnnotatedvalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]defcomplete_name(incomplete:str):completion=[]forname,help_textinvalid_completion_items:ifname.startswith(incomplete):completion_item=(name,help_text)completion.append(completion_item)returncompletionapp=typer.Typer()@app.command()defmain(name:Annotated[str,typer.Option(help="The name to say hi to.",autocompletion=complete_name)]="World",):print(f"Hello {name}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtypervalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]defcomplete_name(incomplete:str):completion=[]forname,help_textinvalid_completion_items:ifname.startswith(incomplete):completion_item=(name,help_text)completion.append(completion_item)returncompletionapp=typer.Typer()@app.command()defmain(name:str=typer.Option("World",help="The name to say hi to.",autocompletion=complete_name),):print(f"Hello {name}")if__name__=="__main__":app()
Tip
If you want to have help text for each item, make sure each item in the list is a tuple. Not a list.
Click checks specifically for a tuple when extracting the help text.
So in the end, the return will be a list (or other iterable) of tuples of 2 str.
Info
The help text will be visible in Zsh, Fish, and PowerShell.
Bash doesn't support showing the help text, but completion will still work the same.
If you have a shell like Zsh, it would look like:
fast →typer ./main.py run --name [TAB][TAB] 💬 We get the completion items with their help text 🎉Camila -- The reader of books. Carlos -- The writer of scripts. Sebastian -- The type hints guy.
Instead of creating and returning a list with values (str or tuple), we can use yield with each value that we want in the completion.
That way our function will be a generator that Typer (actually Click) can iterate:
importtyperfromtyping_extensionsimportAnnotatedvalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]defcomplete_name(incomplete:str):forname,help_textinvalid_completion_items:ifname.startswith(incomplete):yield(name,help_text)app=typer.Typer()@app.command()defmain(name:Annotated[str,typer.Option(help="The name to say hi to.",autocompletion=complete_name)]="World",):print(f"Hello {name}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtypervalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]defcomplete_name(incomplete:str):forname,help_textinvalid_completion_items:ifname.startswith(incomplete):yield(name,help_text)app=typer.Typer()@app.command()defmain(name:str=typer.Option("World",help="The name to say hi to.",autocompletion=complete_name),):print(f"Hello {name}")if__name__=="__main__":app()
That simplifies our code a bit and works the same.
Tip
If all the yield part seems complex for you, don't worry, you can just use the version with the list above.
In the end, that's just to save us a couple of lines of code.
Info
The function can use yield, so it doesn't have to return strictly a list, it just has to be iterable.
But each of the elements for completion has to be a str or a tuple (when containing a help text).
Let's say that now we want to modify the program to be able to "say hi" to multiple people at the same time.
So, we will allow multiple --nameCLI options.
Tip
You will learn more about CLI parameters with multiple values later in the tutorial.
So, for now, take this as a sneak peek 😉.
For this we use a List of str:
fromtypingimportListimporttyperfromtyping_extensionsimportAnnotatedapp=typer.Typer()@app.command()defmain(name:Annotated[List[str],typer.Option(help="The name to say hi to.")]=["World"],):foreach_nameinname:print(f"Hello {each_name}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
fromtypingimportListimporttyperapp=typer.Typer()@app.command()defmain(name:List[str]=typer.Option(["World"],help="The name to say hi to.")):foreach_nameinname:print(f"Hello {each_name}")if__name__=="__main__":app()
And then we can use it like:
fast →typer ./main.py run --name Camila --name Sebastian Hello Camila Hello Sebastian
And the same way as before, we want to provide completion for those names. But we don't want to provide the same names for completion if they were already given in previous parameters.
For that, we will access and use the "Context". When you create a Typer application it uses Click underneath. And every Click application has a special object called a "Context" that is normally hidden.
But you can access the context by declaring a function parameter of type typer.Context.
And from that context you can get the current values for each parameter.
fromtypingimportListimporttyperfromtyping_extensionsimportAnnotatedvalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]defcomplete_name(ctx:typer.Context,incomplete:str):names=ctx.params.get("name")or[]forname,help_textinvalid_completion_items:ifname.startswith(incomplete)andnamenotinnames:yield(name,help_text)app=typer.Typer()@app.command()defmain(name:Annotated[List[str],typer.Option(help="The name to say hi to.",autocompletion=complete_name),]=["World"],):forninname:print(f"Hello {n}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
fromtypingimportListimporttypervalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]defcomplete_name(ctx:typer.Context,incomplete:str):names=ctx.params.get("name")or[]forname,help_textinvalid_completion_items:ifname.startswith(incomplete)andnamenotinnames:yield(name,help_text)app=typer.Typer()@app.command()defmain(name:List[str]=typer.Option(["World"],help="The name to say hi to.",autocompletion=complete_name),):forninname:print(f"Hello {n}")if__name__=="__main__":app()
We are getting the names already provided with --name in the command line before this completion was triggered.
If there's no --name in the command line, it will be None, so we use or [] to make sure we have a list (even if empty) to check its contents later.
Then, when we have a completion candidate, we check if each name was already provided with --name by checking if it's in that list of names with name not in names.
And then we yield each item that has not been used yet.
Check it:
fast →typer ./main.py run --name [TAB][TAB] 💬 The first time we trigger completion, we get all the namesCamila -- The reader of books. Carlos -- The writer of scripts. Sebastian -- The type hints guy.
💬 Add a name and trigger completion againtyper ./main.py run --name Sebastian --name Ca[TAB][TAB] 💬 Now we get completion only for the names we haven't used 🎉Camila -- The reader of books. Carlos -- The writer of scripts.
💬 And if we add another of the available names:typer ./main.py run --name Sebastian --name Camila --name [TAB][TAB] 💬 We get completion for the only available oneCarlos -- The writer of scripts.
It's quite possible that if there's only one option left, your shell will complete it right away instead of showing the option with the help text, to save you more typing.
You can also get the raw CLI parameters, just a list of str with everything passed in the command line before the incomplete value.
For example, something like ["typer", "main.py", "run", "--name"].
Tip
This would be for advanced scenarios, in most use cases you would be better off using the context.
But it's still possible if you need it.
As a simple example, let's show it on the screen before completion.
Because completion is based on the output printed by your program (handled internally by Typer), during completion we can't just print something else as we normally do.
The completion system only reads from "standard output", so, printing to "standard error" won't break completion. 🚀
You can print to "standard error" with a RichConsole(stderr=True).
Using stderr=True tells Rich that the output should be shown in "standard error".
fromtypingimportListimporttyperfromrich.consoleimportConsolefromtyping_extensionsimportAnnotatedvalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]err_console=Console(stderr=True)defcomplete_name(args:List[str],incomplete:str):err_console.print(f"{args}")forname,help_textinvalid_completion_items:ifname.startswith(incomplete):yield(name,help_text)app=typer.Typer()@app.command()defmain(name:Annotated[List[str],typer.Option(help="The name to say hi to.",autocompletion=complete_name),]=["World"],):forninname:print(f"Hello {n}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
fromtypingimportListimporttyperfromrich.consoleimportConsolevalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]err_console=Console(stderr=True)defcomplete_name(args:List[str],incomplete:str):err_console.print(f"{args}")forname,help_textinvalid_completion_items:ifname.startswith(incomplete):yield(name,help_text)app=typer.Typer()@app.command()defmain(name:List[str]=typer.Option(["World"],help="The name to say hi to.",autocompletion=complete_name),):forninname:print(f"Hello {n}")if__name__=="__main__":app()
Info
If you can't install and use Rich, you can also use print(lastname, file=sys.stderr) or typer.echo("some text", err=True) instead.
We get all the CLI parameters as a raw list of str by declaring a parameter with type List[str], here it's named args.
Tip
Here we name the list of all the raw CLI parametersargs because that's the convention with Click.
But it doesn't contain only CLI arguments, it has everything, including CLI options and values, as a raw list of str.
And then we just print it to "standard error".
fast →typer ./main.py run --name [TAB][TAB] 💬 First we see the raw CLI parameters['./main.py', 'run', '--name']
💬 And then we see the actual completionCamila -- The reader of books. Carlos -- The writer of scripts. Sebastian -- The type hints guy.
Of course, you can declare everything if you need it, the context, the raw CLI parameters, and the incomplete str:
fromtypingimportListimporttyperfromrich.consoleimportConsolefromtyping_extensionsimportAnnotatedvalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]err_console=Console(stderr=True)defcomplete_name(ctx:typer.Context,args:List[str],incomplete:str):err_console.print(f"{args}")names=ctx.params.get("name")or[]forname,help_textinvalid_completion_items:ifname.startswith(incomplete)andnamenotinnames:yield(name,help_text)app=typer.Typer()@app.command()defmain(name:Annotated[List[str],typer.Option(help="The name to say hi to.",autocompletion=complete_name),]=["World"],):forninname:print(f"Hello {n}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
fromtypingimportListimporttyperfromrich.consoleimportConsolevalid_completion_items=[("Camila","The reader of books."),("Carlos","The writer of scripts."),("Sebastian","The type hints guy."),]err_console=Console(stderr=True)defcomplete_name(ctx:typer.Context,args:List[str],incomplete:str):err_console.print(f"{args}")names=ctx.params.get("name")or[]forname,help_textinvalid_completion_items:ifname.startswith(incomplete)andnamenotinnames:yield(name,help_text)app=typer.Typer()@app.command()defmain(name:List[str]=typer.Option(["World"],help="The name to say hi to.",autocompletion=complete_name),):forninname:print(f"Hello {n}")if__name__=="__main__":app()
Check it:
fast →typer ./main.py run --name [TAB][TAB] 💬 First we see the raw CLI parameters['./main.py', 'run', '--name']
💬 And then we see the actual completionCamila -- The reader of books. Carlos -- The writer of scripts. Sebastian -- The type hints guy.
typer ./main.py run --name Sebastian --name Ca[TAB][TAB] 💬 Again, we see the raw CLI parameters['./main.py', 'run', '--name', 'Sebastian', '--name']
💬 And then we see the rest of the valid completion itemsCamila -- The reader of books. Carlos -- The writer of scripts.