Skip to content

Commit b2b888c

Browse files
authored
Overhauled custom types. (#1635)
1 parent 343509e commit b2b888c

17 files changed

Lines changed: 343 additions & 256 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ prompt is displayed.
7777
driven by the `DEFAULT_CATEGORY` class variable (see **Simplified command categorization** in
7878
the Enhancements section below for details).
7979
- Removed `Cmd.undoc_header` since all commands are now considered categorized.
80+
- Renamed `Cmd.cmd_func()` to `Cmd.get_command_func()`.
8081
- Enhancements
8182
- New `cmd2.Cmd` parameters
8283
- **auto_suggest**: (boolean) if `True`, provide fish shell style auto-suggestions. These
@@ -107,6 +108,10 @@ prompt is displayed.
107108
- Individual commands can still be manually moved using the `with_category()` decorator.
108109
- For more details and examples, see the [Help](docs/features/help.md) documentation and the
109110
`examples/default_categories.py` file.
111+
- `CommandSet` is now a generic class, which allows developers to parameterize it with their
112+
specific `cmd2.Cmd` subclass (e.g.,`class MyCommandSet(CommandSet[MyApp]):`). This provides
113+
full type hints and IDE autocompletion for `self._cmd` without needing to override and cast
114+
the property.
110115

111116
## 3.5.0 (April 13, 2026)
112117

cmd2/argparse_completer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
from .exceptions import CompletionError
3636
from .rich_utils import Cmd2SimpleTable
3737
from .types import (
38-
ChoicesProviderUnbound,
39-
CmdOrSet,
40-
CompleterUnbound,
38+
CmdOrSetT,
39+
UnboundChoicesProvider,
40+
UnboundCompleter,
4141
)
4242

4343
if TYPE_CHECKING: # pragma: no cover
@@ -214,7 +214,7 @@ def complete(
214214
endidx: int,
215215
tokens: Sequence[str],
216216
*,
217-
cmd_set: CommandSet | None = None,
217+
cmd_set: CommandSet[Any] | None = None,
218218
) -> Completions:
219219
"""Complete text using argparse metadata.
220220
@@ -469,7 +469,7 @@ def _handle_last_token(
469469
consumed_arg_values: dict[str, list[str]],
470470
used_flags: set[str],
471471
skip_remaining_flags: bool,
472-
cmd_set: CommandSet | None,
472+
cmd_set: CommandSet[Any] | None,
473473
) -> Completions:
474474
"""Perform final completion step handling positionals and flags."""
475475
# Check if we are completing a flag name. This check ignores strings with a length of one, like '-'.
@@ -734,11 +734,11 @@ def _choices_to_items(self, arg_state: _ArgumentState) -> list[CompletionItem]:
734734

735735
def _prepare_callable_params(
736736
self,
737-
to_call: ChoicesProviderUnbound[CmdOrSet] | CompleterUnbound[CmdOrSet],
737+
to_call: UnboundChoicesProvider[CmdOrSetT] | UnboundCompleter[CmdOrSetT],
738738
arg_state: _ArgumentState,
739739
text: str,
740740
consumed_arg_values: dict[str, list[str]],
741-
cmd_set: CommandSet | None,
741+
cmd_set: CommandSet[Any] | None,
742742
) -> tuple[list[Any], dict[str, Any]]:
743743
"""Resolve the instance and arguments required to call a choices/completer function."""
744744
args: list[Any] = []
@@ -769,7 +769,7 @@ def _complete_arg(
769769
arg_state: _ArgumentState,
770770
consumed_arg_values: dict[str, list[str]],
771771
*,
772-
cmd_set: CommandSet | None = None,
772+
cmd_set: CommandSet[Any] | None = None,
773773
) -> Completions:
774774
"""Completion routine for an argparse argument.
775775

cmd2/argparse_custom.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ def get_choices(self) -> Choices:
262262
from .rich_utils import Cmd2RichArgparseConsole
263263
from .styles import Cmd2Style
264264
from .types import (
265-
ChoicesProviderUnbound,
266-
CmdOrSet,
267-
CompleterUnbound,
265+
CmdOrSetT,
266+
UnboundChoicesProvider,
267+
UnboundCompleter,
268268
)
269269

270270
if TYPE_CHECKING: # pragma: no cover
@@ -388,8 +388,8 @@ def _ActionsContainer_add_argument( # noqa: N802
388388
self: argparse._ActionsContainer,
389389
*args: Any,
390390
nargs: int | str | tuple[int] | tuple[int, int] | tuple[int, float] | None = None,
391-
choices_provider: ChoicesProviderUnbound[CmdOrSet] | None = None,
392-
completer: CompleterUnbound[CmdOrSet] | None = None,
391+
choices_provider: UnboundChoicesProvider[CmdOrSetT] | None = None,
392+
completer: UnboundCompleter[CmdOrSetT] | None = None,
393393
suppress_tab_hint: bool = False,
394394
table_columns: Sequence[str | Column] | None = None,
395395
**kwargs: Any,

0 commit comments

Comments
 (0)