Skip to content

Commit e8827ae

Browse files
chore: fix rebase error
1 parent a029288 commit e8827ae

3 files changed

Lines changed: 25 additions & 63 deletions

File tree

cmd2/argparse_completer.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,16 @@ def print_help(self, tokens: Sequence[str], file: IO[str] | None = None) -> None
717717

718718
def _choices_to_items(self, arg_state: _ArgumentState) -> list[CompletionItem]:
719719
"""Convert choices from action to list of CompletionItems."""
720+
action_type = arg_state.action.type
721+
if action_type is not None and arg_state.action.choices is None:
722+
if isinstance(action_type, type) and issubclass(action_type, enum.Enum):
723+
return [CompletionItem(str(m.value), display_meta=m.name) for m in action_type]
724+
enum_from_converter = getattr(action_type, '_cmd2_enum_class', None)
725+
if isinstance(enum_from_converter, type) and issubclass(enum_from_converter, enum.Enum):
726+
return [CompletionItem(str(m.value), display_meta=m.name) for m in enum_from_converter]
727+
if getattr(action_type, '__name__', None) == '_parse_bool':
728+
return [CompletionItem(v) for v in ['true', 'false', 'yes', 'no', 'on', 'off', '1', '0']]
729+
720730
if arg_state.action.choices is None:
721731
return []
722732

@@ -733,29 +743,6 @@ def _choices_to_items(self, arg_state: _ArgumentState) -> list[CompletionItem]:
733743
for name, subparser in arg_state.action.choices.items()
734744
]
735745

736-
choices_callable: ChoicesCallable | None = arg_state.action.get_choices_callable() # type: ignore[attr-defined]
737-
if choices_callable is not None:
738-
return choices_callable
739-
740-
# Type inference: auto-complete from action.type when no explicit
741-
# choices or choices_callable is configured.
742-
action_type = arg_state.action.type
743-
if action_type is not None:
744-
if action_type is pathlib.Path or (isinstance(action_type, type) and issubclass(action_type, pathlib.Path)):
745-
from .cmd2 import Cmd
746-
747-
return ChoicesCallable(is_completer=True, to_call=Cmd.path_complete)
748-
749-
if isinstance(action_type, type) and issubclass(action_type, enum.Enum):
750-
return [CompletionItem(str(m.value), display_meta=m.name) for m in action_type]
751-
752-
enum_from_converter = getattr(action_type, '_cmd2_enum_class', None)
753-
if isinstance(enum_from_converter, type) and issubclass(enum_from_converter, enum.Enum):
754-
return [CompletionItem(str(m.value), display_meta=m.name) for m in enum_from_converter]
755-
756-
if getattr(action_type, '__name__', None) == '_parse_bool':
757-
return [CompletionItem(v) for v in ['true', 'false', 'yes', 'no', 'on', 'off', '1', '0']]
758-
759746
# Standard choices
760747
return [
761748
choice if isinstance(choice, CompletionItem) else CompletionItem(choice) for choice in arg_state.action.choices
@@ -817,6 +804,13 @@ def _complete_arg(
817804
)
818805
args.extend([text, line, begidx, endidx])
819806
completions: Completions = completer(*args, **kwargs)
807+
# if is a path type, then use cmd2's path completer
808+
elif arg_state.action.type is pathlib.Path or (
809+
isinstance(arg_state.action.type, type) and issubclass(arg_state.action.type, pathlib.Path)
810+
):
811+
from .cmd2 import Cmd
812+
813+
completions = Cmd.path_complete(self._cmd2_app, text, line, begidx, endidx)
820814

821815
# Otherwise it uses a choices provider or choices list
822816
else:

tests/test_annotated.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,13 @@ def do_broken(self, cmd2_handler, name: 'NonExistentType'): # noqa: F821
297297
def test_choices_provider_overrides_enum_choices(self) -> None:
298298
action = _get_param_action(_func_choices_provider_on_enum)
299299
assert action.choices is None
300-
assert action.get_choices_callable() is not None # type: ignore[attr-defined]
300+
assert action.get_choices_provider() is not None # type: ignore[attr-defined]
301+
assert action.get_completer() is None # type: ignore[attr-defined]
301302

302303
def test_completer_overrides_path_choices(self) -> None:
303304
action = _get_param_action(_func_completer_on_path)
304-
cc = action.get_choices_callable() # type: ignore[attr-defined]
305-
assert cc is not None
306-
assert cc.is_completer is True
305+
assert action.get_choices_provider() is None # type: ignore[attr-defined]
306+
assert action.get_completer() is cmd2.Cmd.path_complete # type: ignore[attr-defined]
307307

308308
def test_dest_param_raises(self) -> None:
309309
with pytest.raises(ValueError, match="dest"):
@@ -851,8 +851,8 @@ def __init__(self) -> None:
851851
super().__init__()
852852
self._items = ["apple", "banana", "cherry"]
853853

854-
def item_choices(self) -> list[str]:
855-
return self._items
854+
def item_choices(self) -> list[cmd2.CompletionItem]:
855+
return [cmd2.CompletionItem(item) for item in self._items]
856856

857857
@cmd2.with_annotated
858858
def do_greet(self, name: str, count: int = 1) -> None:
@@ -987,8 +987,8 @@ def __init__(self) -> None:
987987
super().__init__()
988988
self._sports = ["football", "baseball"]
989989

990-
def sport_choices(self) -> list[str]:
991-
return self._sports
990+
def sport_choices(self) -> list[cmd2.CompletionItem]:
991+
return [cmd2.CompletionItem(sport) for sport in self._sports]
992992

993993
@cmd2.with_annotated
994994
def do_play(self, sport: Annotated[str, Argument(choices_provider=sport_choices)]) -> None:

tests/test_argparse_completer.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,38 +1134,6 @@ def test_complete_command_help_no_tokens(ac_app) -> None:
11341134
assert not completions
11351135

11361136

1137-
def test_complete_for_arg_type_without_name(ac_app) -> None:
1138-
from cmd2.argparse_completer import (
1139-
ArgparseCompleter,
1140-
)
1141-
1142-
parser = Cmd2ArgumentParser()
1143-
parser.add_argument('value')
1144-
action = next(action for action in parser._actions if action.dest == 'value')
1145-
1146-
class TypeWithoutName:
1147-
__name__ = property(lambda self: (_ for _ in ()).throw(AttributeError("__name__")))
1148-
1149-
action.type = TypeWithoutName()
1150-
arg_state = argparse_completer._ArgumentState(action)
1151-
ac = ArgparseCompleter(parser, ac_app)
1152-
1153-
assert ac._get_raw_choices(arg_state) is None
1154-
1155-
1156-
def test_bool_type_completion_values(ac_app) -> None:
1157-
from cmd2.annotated import _parse_bool
1158-
1159-
parser = Cmd2ArgumentParser()
1160-
parser.add_argument("flag", type=_parse_bool)
1161-
ac = argparse_completer.ArgparseCompleter(parser, ac_app)
1162-
action = next(action for action in parser._actions if action.dest == 'flag')
1163-
arg_state = argparse_completer._ArgumentState(action)
1164-
1165-
completions = ac._complete_arg(text="", line="", begidx=0, endidx=0, arg_state=arg_state, consumed_arg_values={})
1166-
assert list(completions.to_strings()) == ["0", "1", "false", "no", "off", "on", "true", "yes"]
1167-
1168-
11691137
@pytest.mark.parametrize(
11701138
('flag', 'expected'),
11711139
[

0 commit comments

Comments
 (0)