Skip to content

Commit

Permalink
Removed Python version check related to argparse parser deep copies s…
Browse files Browse the repository at this point in the history
…ince we only support Python 3.8+ now.
  • Loading branch information
kmvanbrunt committed Sep 13, 2024
1 parent 79b0675 commit ecbe084
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
8 changes: 2 additions & 6 deletions cmd2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

import sys

# For python 3.8 and later
if sys.version_info >= (3, 8):
import importlib.metadata as importlib_metadata
else:
# For everyone else
import importlib_metadata
import importlib.metadata as importlib_metadata

try:
__version__ = importlib_metadata.version(__name__)
except importlib_metadata.PackageNotFoundError: # pragma: no cover
Expand Down
26 changes: 9 additions & 17 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,7 @@ def _build_parser(
elif callable(parser_builder):
parser = parser_builder()
elif isinstance(parser_builder, argparse.ArgumentParser):
if sys.version_info >= (3, 6, 4):
parser = copy.deepcopy(parser_builder)
else: # pragma: no cover
parser = parser_builder
parser = copy.deepcopy(parser_builder)
return parser

def _register_command_parser(self, command: str, command_method: Callable[..., Any]) -> None:
Expand Down Expand Up @@ -780,7 +777,7 @@ def unregister_command_set(self, cmdset: CommandSet) -> None:

methods: List[Tuple[str, Callable[[Any], Any]]] = inspect.getmembers(
cmdset,
predicate=lambda meth: isinstance(meth, Callable) # type: ignore[arg-type, var-annotated]
predicate=lambda meth: isinstance(meth, Callable) # type: ignore[arg-type]
and hasattr(meth, '__name__')
and meth.__name__.startswith(COMMAND_FUNC_PREFIX),
)
Expand Down Expand Up @@ -811,7 +808,7 @@ def unregister_command_set(self, cmdset: CommandSet) -> None:
def _check_uninstallable(self, cmdset: CommandSet) -> None:
methods: List[Tuple[str, Callable[[Any], Any]]] = inspect.getmembers(
cmdset,
predicate=lambda meth: isinstance(meth, Callable) # type: ignore[arg-type, var-annotated]
predicate=lambda meth: isinstance(meth, Callable) # type: ignore[arg-type]
and hasattr(meth, '__name__')
and meth.__name__.startswith(COMMAND_FUNC_PREFIX),
)
Expand Down Expand Up @@ -3328,19 +3325,14 @@ def _cmdloop(self) -> None:
#############################################################

# Top-level parser for alias
@staticmethod
def _build_alias_parser() -> argparse.ArgumentParser:
alias_description = (
"Manage aliases\n" "\n" "An alias is a command that enables replacement of a word by another string."
)
alias_epilog = "See also:\n" " macro"
alias_parser = argparse_custom.DEFAULT_ARGUMENT_PARSER(description=alias_description, epilog=alias_epilog)
alias_subparsers = alias_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND')
alias_subparsers.required = True
return alias_parser
alias_description = "Manage aliases\n" "\n" "An alias is a command that enables replacement of a word by another string."
alias_epilog = "See also:\n" " macro"
alias_parser = argparse_custom.DEFAULT_ARGUMENT_PARSER(description=alias_description, epilog=alias_epilog)
alias_subparsers = alias_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND')
alias_subparsers.required = True

# Preserve quotes since we are passing strings to other commands
@with_argparser(_build_alias_parser, preserve_quotes=True)
@with_argparser(alias_parser, preserve_quotes=True)
def do_alias(self, args: argparse.Namespace) -> None:
"""Manage aliases"""
# Call handler for whatever subcommand was selected
Expand Down
3 changes: 2 additions & 1 deletion cmd2/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ def _set_parser_prog(parser: argparse.ArgumentParser, prog: str) -> None:

# We can break since argparse only allows 1 group of subcommands per level
break
# need to save required args so they can be prepended to the subcommand usage

# Need to save required args so they can be prepended to the subcommand usage
elif action.required:
req_args.append(action.dest)

Expand Down
7 changes: 0 additions & 7 deletions docs/features/argument_processing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ Here's what it looks like::
for i in range(min(repetitions, self.maxrepeats)):
self.poutput(arg)

.. warning::

It is important that each command which uses the ``@with_argparser``
decorator be passed a unique instance of a parser since command-specific
changes could be made to it.


.. note::

The ``@with_argparser`` decorator sets the ``prog`` variable in the argument
Expand Down

0 comments on commit ecbe084

Please sign in to comment.