Skip to content

Commit f67ac8c

Browse files
authored
Fixed type hint for with_default_category decorator which caused type checkers to mistype (#1381)
1 parent 5d2a0e9 commit f67ac8c

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.5.6 (TBD)
2+
* Bug Fixes
3+
* Fixed type hint for `with_default_category` decorator which caused type checkers to mistype
4+
a decorated subclass of `CommandSet` and a plain `CommandSet`.
5+
16
## 2.5.5 (November 13, 2024)
27
* Bug Fixes
38
* Fixed type hints for passing a class method to `with_argparser` and `as_subcommand_to`.

cmd2/command_definition.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Mapping,
1111
Optional,
1212
Type,
13+
TypeVar,
1314
)
1415

1516
from .constants import (
@@ -30,8 +31,10 @@
3031
#: Further refinements are needed to define the input parameters
3132
CommandFunc = Callable[..., Optional[bool]]
3233

34+
CommandSetType = TypeVar('CommandSetType', bound=Type['CommandSet'])
3335

34-
def with_default_category(category: str, *, heritable: bool = True) -> Callable[[Type['CommandSet']], Type['CommandSet']]:
36+
37+
def with_default_category(category: str, *, heritable: bool = True) -> Callable[[CommandSetType], CommandSetType]:
3538
"""
3639
Decorator that applies a category to all ``do_*`` command methods in a class that do not already
3740
have a category specified.
@@ -42,15 +45,15 @@ def with_default_category(category: str, *, heritable: bool = True) -> Callable[
4245
override the default category.
4346
4447
If `heritable` is set to False, then only the commands declared locally to this CommandSet will be placed in the
45-
specified category. Dynamically created commands, and commands declared in sub-classes will not receive this
48+
specified category. Dynamically created commands and commands declared in sub-classes will not receive this
4649
category.
4750
4851
:param category: category to put all uncategorized commands in
4952
:param heritable: Flag whether this default category should apply to sub-classes. Defaults to True
5053
:return: decorator function
5154
"""
5255

53-
def decorate_class(cls: Type[CommandSet]) -> Type[CommandSet]:
56+
def decorate_class(cls: CommandSetType) -> CommandSetType:
5457
if heritable:
5558
setattr(cls, CLASS_ATTR_DEFAULT_HELP_CATEGORY, category)
5659

docs/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
nitpick_ignore = [
185185
('py:class', 'ArgparseCommandFunc'),
186186
('py:class', 'argparse._SubParsersAction'),
187+
('py:class', 'cmd2.command_definition.CommandSetType'),
187188
('py:class', 'cmd2.decorators.CommandParent'),
188189
('py:class', 'cmd2.decorators.CommandParentType'),
189190
('py:class', 'cmd2.utils._T'),

0 commit comments

Comments
 (0)