1414)
1515from typing import (
1616 Any ,
17- cast ,
1817 Dict ,
1918 List ,
2019 Optional ,
2120 Union ,
21+ cast ,
2222)
2323
2424from . import (
3232 ATTR_NARGS_RANGE ,
3333 ATTR_SUPPRESS_TAB_HINT ,
3434 ChoicesCallable ,
35+ ChoicesProviderFuncBase ,
36+ ChoicesProviderFuncWithTokens ,
3537 CompletionItem ,
3638 generate_range_error ,
3739)
@@ -317,9 +319,11 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
317319 # Handle '--' which tells argparse all remaining arguments are non-flags
318320 elif token == '--' and not skip_remaining_flags :
319321 # Check if there is an unfinished flag
320- if flag_arg_state is not None \
321- and isinstance (flag_arg_state .min , int ) \
322- and flag_arg_state .count < flag_arg_state .min :
322+ if (
323+ flag_arg_state is not None
324+ and isinstance (flag_arg_state .min , int )
325+ and flag_arg_state .count < flag_arg_state .min
326+ ):
323327 raise _UnfinishedFlagError (flag_arg_state )
324328
325329 # Otherwise end the current flag
@@ -332,9 +336,11 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
332336 if _looks_like_flag (token , self ._parser ) and not skip_remaining_flags :
333337
334338 # Check if there is an unfinished flag
335- if flag_arg_state is not None \
336- and isinstance (flag_arg_state .min , int ) \
337- and flag_arg_state .count < flag_arg_state .min :
339+ if (
340+ flag_arg_state is not None
341+ and isinstance (flag_arg_state .min , int )
342+ and flag_arg_state .count < flag_arg_state .min
343+ ):
338344 raise _UnfinishedFlagError (flag_arg_state )
339345
340346 # Reset flag arg state but not positional tracking because flags can be
@@ -438,9 +444,11 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
438444 # the current argument. We will handle the completion of flags that start with only one prefix
439445 # character (-f) at the end.
440446 if _looks_like_flag (text , self ._parser ) and not skip_remaining_flags :
441- if flag_arg_state is not None \
442- and isinstance (flag_arg_state .min , int ) \
443- and flag_arg_state .count < flag_arg_state .min :
447+ if (
448+ flag_arg_state is not None
449+ and isinstance (flag_arg_state .min , int )
450+ and flag_arg_state .count < flag_arg_state .min
451+ ):
444452 raise _UnfinishedFlagError (flag_arg_state )
445453 return self ._complete_flags (text , line , begidx , endidx , matched_flags )
446454
@@ -692,17 +700,23 @@ def _complete_arg(
692700 # Check if the argument uses a specific tab completion function to provide its choices
693701 if isinstance (arg_choices , ChoicesCallable ) and arg_choices .is_completer :
694702 args .extend ([text , line , begidx , endidx ])
695- results = arg_choices .to_call (* args , ** kwargs ) # type: ignore[arg-type]
703+ results = arg_choices .completer (* args , ** kwargs ) # type: ignore[arg-type]
696704
697705 # Otherwise use basic_complete on the choices
698706 else :
699707 # Check if the choices come from a function
700- completion_items : List [str ]
708+ completion_items : List [str ] = []
701709 if isinstance (arg_choices , ChoicesCallable ):
702710 if not arg_choices .is_completer :
703- completion_items = arg_choices .to_call (* args , ** kwargs ) # type: ignore[arg-type]
704- else :
705- completion_items = []
711+ choices_func = arg_choices .choices_provider
712+ if isinstance (choices_func , ChoicesProviderFuncWithTokens ):
713+ completion_items = choices_func (* args , ** kwargs ) # type: ignore[arg-type]
714+ else : # pragma: no cover
715+ # This won't hit because runtime checking doesn't check function argument types and will always
716+ # resolve true above. Mypy, however, does see the difference and gives an error that can't be
717+ # ignored. Mypy issue #5485 discusses this problem
718+ completion_items = choices_func (* args ) # type: ignore[arg-type]
719+ # else case is already covered above
706720 else :
707721 completion_items = arg_choices
708722
0 commit comments