From f67ac8c6433bad054b4a84c7b7e1ade362a6fd50 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 14 Nov 2024 03:41:45 -0500 Subject: [PATCH 1/2] Fixed type hint for with_default_category decorator which caused type checkers to mistype (#1381) --- CHANGELOG.md | 5 +++++ cmd2/command_definition.py | 9 ++++++--- docs/conf.py | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3d6de40..8fa1db4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/cmd2/command_definition.py b/cmd2/command_definition.py index 1c1ff0cf..002a1773 100644 --- a/cmd2/command_definition.py +++ b/cmd2/command_definition.py @@ -10,6 +10,7 @@ Mapping, Optional, Type, + TypeVar, ) from .constants import ( @@ -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. @@ -42,7 +45,7 @@ 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 @@ -50,7 +53,7 @@ def with_default_category(category: str, *, heritable: bool = True) -> Callable[ :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) diff --git a/docs/conf.py b/docs/conf.py index 0d31a9d6..110c3684 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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'), From 91897f401664dc573cb8509b29a1f84cefc46b19 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 14 Nov 2024 08:56:55 -0500 Subject: [PATCH 2/2] Update change log for release. --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fa1db4e..57efc7b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ -## 2.5.6 (TBD) +## 2.5.6 (November 14, 2024) * 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`. + a subclass of `CommandSet` as a plain `CommandSet`. ## 2.5.5 (November 13, 2024) * Bug Fixes