Skip to content

Commit ad8ec9e

Browse files
committed
Updated types for some functions in completion.py.
1 parent 34bfac9 commit ad8ec9e

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

cmd2/argparse_completer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
)
3737
from .command_definition import CommandSet
3838
from .completion import (
39+
Choices,
3940
CompletionItem,
4041
Completions,
4142
all_display_numeric,
@@ -697,9 +698,7 @@ def _get_raw_choices(self, arg_state: _ArgumentState) -> list[CompletionItem] |
697698
]
698699

699700
# Standard choices
700-
return [
701-
choice if isinstance(choice, CompletionItem) else CompletionItem(choice) for choice in arg_state.action.choices
702-
]
701+
return Choices.from_values(arg_state.action.choices)
703702

704703
choices_callable: ChoicesCallable | None = arg_state.action.get_choices_callable() # type: ignore[attr-defined]
705704
return choices_callable

cmd2/completion.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
from collections.abc import (
66
Callable,
7+
Collection,
78
Iterator,
89
Sequence,
910
)
@@ -148,13 +149,13 @@ def __post_init__(self) -> None:
148149
object.__setattr__(self, "items", tuple(unique_items))
149150

150151
@classmethod
151-
def from_values(cls, values: Sequence[str], *, is_sorted: bool = False) -> Self:
152-
"""Create a completion results instance from a sequence of arbitrary objects.
152+
def from_values(cls, values: Iterator[Any], *, is_sorted: bool = False) -> Self:
153+
"""Create a CompletionItem instance from arbitrary objects.
153154
154155
:param values: the raw objects (e.g. strs, ints, Paths) to be converted into CompletionItems.
155156
:param is_sorted: whether the values are already in the desired order.
156157
"""
157-
items = [CompletionItem(value=v) for v in values]
158+
items = [v if isinstance(v, CompletionItem) else CompletionItem(value=v) for v in values]
158159
return cls(items=items, is_sorted=is_sorted)
159160

160161
def to_strings(self) -> tuple[str, ...]:
@@ -244,7 +245,7 @@ class Completions(CompletionResultsBase):
244245
_quote_char: str = ""
245246

246247

247-
def all_display_numeric(items: Sequence[CompletionItem]) -> bool:
248+
def all_display_numeric(items: Collection[CompletionItem]) -> bool:
248249
"""Return True if items is non-empty and every item.display is a numeric string."""
249250
return bool(items) and all(NUMERIC_RE.match(item.display) for item in items)
250251

0 commit comments

Comments
 (0)