Skip to content

Fixed type hint for with_default_category decorator. #1381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.5.6 (TBD)
* Bug Fixes
* Fixed type hint for `with_default_category` decorator which caused type checkers to mistype
a decorated subclass of `CommandSet` and a plain `CommandSet`.

## 2.5.5 (November 13, 2024)
* Bug Fixes
* Fixed type hints for passing a class method to `with_argparser` and `as_subcommand_to`.
Expand Down
9 changes: 6 additions & 3 deletions cmd2/command_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Mapping,
Optional,
Type,
TypeVar,
)

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

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

def with_default_category(category: str, *, heritable: bool = True) -> Callable[[Type['CommandSet']], Type['CommandSet']]:

def with_default_category(category: str, *, heritable: bool = True) -> Callable[[CommandSetType], CommandSetType]:
"""
Decorator that applies a category to all ``do_*`` command methods in a class that do not already
have a category specified.
Expand All @@ -42,15 +45,15 @@ def with_default_category(category: str, *, heritable: bool = True) -> Callable[
override the default category.
If `heritable` is set to False, then only the commands declared locally to this CommandSet will be placed in the
specified category. Dynamically created commands, and commands declared in sub-classes will not receive this
specified category. Dynamically created commands and commands declared in sub-classes will not receive this
category.
:param category: category to put all uncategorized commands in
:param heritable: Flag whether this default category should apply to sub-classes. Defaults to True
:return: decorator function
"""

def decorate_class(cls: Type[CommandSet]) -> Type[CommandSet]:
def decorate_class(cls: CommandSetType) -> CommandSetType:
if heritable:
setattr(cls, CLASS_ATTR_DEFAULT_HELP_CATEGORY, category)

Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
nitpick_ignore = [
('py:class', 'ArgparseCommandFunc'),
('py:class', 'argparse._SubParsersAction'),
('py:class', 'cmd2.command_definition.CommandSetType'),
('py:class', 'cmd2.decorators.CommandParent'),
('py:class', 'cmd2.decorators.CommandParentType'),
('py:class', 'cmd2.utils._T'),
Expand Down
Loading