From 5a8b8281f3b6e1b4d71230b69215c10c34eba1aa Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 30 Jan 2025 15:30:26 +0100 Subject: [PATCH 1/3] WIP: TyperPath with custom shell_complete functionality --- typer/main.py | 3 ++- typer/models.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/typer/main.py b/typer/main.py index 8beb61f7a5..fbcedf4d5b 100644 --- a/typer/main.py +++ b/typer/main.py @@ -45,6 +45,7 @@ ParamMeta, Required, TyperInfo, + TyperPath, ) from .utils import get_params_from_function @@ -744,7 +745,7 @@ def get_click_type( or parameter_info.path_type or parameter_info.resolve_path ): - return click.Path( + return TyperPath( exists=parameter_info.exists, file_okay=parameter_info.file_okay, dir_okay=parameter_info.dir_okay, diff --git a/typer/models.py b/typer/models.py index 544e504761..56a79afef7 100644 --- a/typer/models.py +++ b/typer/models.py @@ -531,3 +531,14 @@ def __init__( self.pretty_exceptions_enable = pretty_exceptions_enable self.pretty_exceptions_show_locals = pretty_exceptions_show_locals self.pretty_exceptions_short = pretty_exceptions_short + + +class TyperPath(click.Path): + # Overwrite Click's behaviour to be compatible with Typer's autocompletion system + def shell_complete( + self, ctx: Context, param: click.Parameter, incomplete: str + ) -> list[click.shell_completion.CompletionItem]: + """Return an empty list so that the autocompletion functionality + will work properly from the commandline. + """ + return [] From cf1428b285c6fb3c050639c136f3c6ae41899139 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 30 Jan 2025 16:20:11 +0100 Subject: [PATCH 2/3] fix type of ctx --- typer/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/models.py b/typer/models.py index 56a79afef7..fd79d928d4 100644 --- a/typer/models.py +++ b/typer/models.py @@ -536,7 +536,7 @@ def __init__( class TyperPath(click.Path): # Overwrite Click's behaviour to be compatible with Typer's autocompletion system def shell_complete( - self, ctx: Context, param: click.Parameter, incomplete: str + self, ctx: click.Context, param: click.Parameter, incomplete: str ) -> list[click.shell_completion.CompletionItem]: """Return an empty list so that the autocompletion functionality will work properly from the commandline. From a7596a0948ab7cec1080f0ee22aaec0ef68ad690 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 30 Jan 2025 16:22:01 +0100 Subject: [PATCH 3/3] fix type of list --- typer/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/models.py b/typer/models.py index fd79d928d4..e0bddb965b 100644 --- a/typer/models.py +++ b/typer/models.py @@ -537,7 +537,7 @@ class TyperPath(click.Path): # Overwrite Click's behaviour to be compatible with Typer's autocompletion system def shell_complete( self, ctx: click.Context, param: click.Parameter, incomplete: str - ) -> list[click.shell_completion.CompletionItem]: + ) -> List[click.shell_completion.CompletionItem]: """Return an empty list so that the autocompletion functionality will work properly from the commandline. """