diff options
-rw-r--r-- | Lib/argparse.py | 37 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 79 | ||||
-rw-r--r-- | Misc/NEWS | 2 |
3 files changed, 39 insertions, 79 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index 87d0cef..ab3ec4b 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1579,7 +1579,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): usage=None, description=None, epilog=None, - version=None, parents=[], formatter_class=HelpFormatter, prefix_chars='-', @@ -1588,14 +1587,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): conflict_handler='error', add_help=True): - if version is not None: - import warnings - warnings.warn( - """The "version" argument to ArgumentParser is deprecated. """ - """Please use """ - """"add_argument(..., action='version', version="N", ...)" """ - """instead""", DeprecationWarning) - superinit = super(ArgumentParser, self).__init__ superinit(description=description, prefix_chars=prefix_chars, @@ -1609,7 +1600,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): self.prog = prog self.usage = usage self.epilog = epilog - self.version = version self.formatter_class = formatter_class self.fromfile_prefix_chars = fromfile_prefix_chars self.add_help = add_help @@ -1624,7 +1614,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): return string self.register('type', None, identity) - # add help and version arguments if necessary + # add help argument if necessary # (using explicit default to override global argument_default) default_prefix = '-' if '-' in prefix_chars else prefix_chars[0] if self.add_help: @@ -1632,12 +1622,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): default_prefix+'h', default_prefix*2+'help', action='help', default=SUPPRESS, help=_('show this help message and exit')) - if self.version: - self.add_argument( - default_prefix+'v', default_prefix*2+'version', - action='version', default=SUPPRESS, - version=self.version, - help=_("show program's version number and exit")) # add parent arguments and defaults for parent in parents: @@ -1657,7 +1641,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): 'prog', 'usage', 'description', - 'version', 'formatter_class', 'conflict_handler', 'add_help', @@ -2320,16 +2303,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): # determine help from format above return formatter.format_help() - def format_version(self): - import warnings - warnings.warn( - 'The format_version method is deprecated -- the "version" ' - 'argument to ArgumentParser is no longer supported.', - DeprecationWarning) - formatter = self._get_formatter() - formatter.add_text(self.version) - return formatter.format_help() - def _get_formatter(self): return self.formatter_class(prog=self.prog) @@ -2346,14 +2319,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): file = _sys.stdout self._print_message(self.format_help(), file) - def print_version(self, file=None): - import warnings - warnings.warn( - 'The print_version method is deprecated -- the "version" ' - 'argument to ArgumentParser is no longer supported.', - DeprecationWarning) - self._print_message(self.format_version(), file) - def _print_message(self, message, file=None): if message: if file is None: diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index f456709..e69cc40 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -1323,20 +1323,21 @@ class TestParserDefaultSuppress(ParserTestCase): class TestParserDefault42(ParserTestCase): """Test actions with a parser-level default of 42""" - parser_signature = Sig(argument_default=42, version='1.0') + parser_signature = Sig(argument_default=42) argument_signatures = [ + Sig('--version', action='version', version='1.0'), Sig('foo', nargs='?'), Sig('bar', nargs='*'), Sig('--baz', action='store_true'), ] failures = ['-x'] successes = [ - ('', NS(foo=42, bar=42, baz=42)), - ('a', NS(foo='a', bar=42, baz=42)), - ('a b', NS(foo='a', bar=['b'], baz=42)), - ('--baz', NS(foo=42, bar=42, baz=True)), - ('a --baz', NS(foo='a', bar=42, baz=True)), - ('--baz a b', NS(foo='a', bar=['b'], baz=True)), + ('', NS(foo=42, bar=42, baz=42, version=42)), + ('a', NS(foo='a', bar=42, baz=42, version=42)), + ('a b', NS(foo='a', bar=['b'], baz=42, version=42)), + ('--baz', NS(foo=42, bar=42, baz=True, version=42)), + ('a --baz', NS(foo='a', bar=42, baz=True, version=42)), + ('--baz a b', NS(foo='a', bar=['b'], baz=True, version=42)), ] @@ -2896,10 +2897,9 @@ class TestHelpFormattingMetaclass(type): parser_text = sfile.getvalue() self._test(tester, parser_text) - # add tests for {format,print}_{usage,help,version} + # add tests for {format,print}_{usage,help} for func_suffix, std_name in [('usage', 'stdout'), - ('help', 'stdout'), - ('version', 'stderr')]: + ('help', 'stdout')]: AddTests(cls, func_suffix, std_name) bases = TestCase, @@ -2910,8 +2910,9 @@ class TestHelpBiggerOptionals(HelpTestCase): """Make sure that argument help aligns when options are longer""" parser_signature = Sig(prog='PROG', description='DESCRIPTION', - epilog='EPILOG', version='0.1') + epilog='EPILOG') argument_signatures = [ + Sig('-v', '--version', action='version', version='0.1'), Sig('-x', action='store_true', help='X HELP'), Sig('--y', help='Y HELP'), Sig('foo', help='FOO HELP'), @@ -2946,8 +2947,9 @@ class TestHelpBiggerOptionalGroups(HelpTestCase): """Make sure that argument help aligns when options are longer""" parser_signature = Sig(prog='PROG', description='DESCRIPTION', - epilog='EPILOG', version='0.1') + epilog='EPILOG') argument_signatures = [ + Sig('-v', '--version', action='version', version='0.1'), Sig('-x', action='store_true', help='X HELP'), Sig('--y', help='Y HELP'), Sig('foo', help='FOO HELP'), @@ -3114,9 +3116,9 @@ HHAAHHH class TestHelpWrappingLongNames(HelpTestCase): """Make sure that text after long names starts on the next line""" - parser_signature = Sig(usage='USAGE', description= 'D D' * 30, - version='V V'*30) + parser_signature = Sig(usage='USAGE', description= 'D D' * 30) argument_signatures = [ + Sig('-v', '--version', action='version', version='V V' * 30), Sig('-x', metavar='X' * 25, help='XH XH' * 20), Sig('y', metavar='y' * 25, help='YH YH' * 20), ] @@ -3719,8 +3721,9 @@ class TestHelpNoHelpOptional(HelpTestCase): class TestHelpVersionOptional(HelpTestCase): """Test that the --version argument can be suppressed help messages""" - parser_signature = Sig(prog='PROG', version='1.0') + parser_signature = Sig(prog='PROG') argument_signatures = [ + Sig('-v', '--version', action='version', version='1.0'), Sig('--foo', help='foo help'), Sig('spam', help='spam help'), ] @@ -3953,8 +3956,8 @@ class TestHelpVersionAction(HelpTestCase): class TestHelpSubparsersOrdering(HelpTestCase): """Test ordering of subcommands in help matches the code""" parser_signature = Sig(prog='PROG', - description='display some subcommands', - version='0.1') + description='display some subcommands') + argument_signatures = [Sig('-v', '--version', action='version', version='0.1')] subparsers_signatures = [Sig(name=name) for name in ('a', 'b', 'c', 'd', 'e')] @@ -3982,8 +3985,8 @@ class TestHelpSubparsersOrdering(HelpTestCase): class TestHelpSubparsersWithHelpOrdering(HelpTestCase): """Test ordering of subcommands in help matches the code""" parser_signature = Sig(prog='PROG', - description='display some subcommands', - version='0.1') + description='display some subcommands') + argument_signatures = [Sig('-v', '--version', action='version', version='0.1')] subcommand_data = (('a', 'a subcommand help'), ('b', 'b subcommand help'), @@ -4280,32 +4283,28 @@ class TestOptionalsHelpVersionActions(TestCase): parser.format_help(), self._get_error(parser.parse_args, args_str.split()).stdout) - def assertPrintVersionExit(self, parser, args_str): - self.assertEqual( - parser.format_version(), - self._get_error(parser.parse_args, args_str.split()).stderr) - def assertArgumentParserError(self, parser, *args): self.assertRaises(ArgumentParserError, parser.parse_args, args) def test_version(self): - parser = ErrorRaisingArgumentParser(version='1.0') + parser = ErrorRaisingArgumentParser() + parser.add_argument('-v', '--version', action='version', version='1.0') self.assertPrintHelpExit(parser, '-h') self.assertPrintHelpExit(parser, '--help') - self.assertPrintVersionExit(parser, '-v') - self.assertPrintVersionExit(parser, '--version') + self.assertRaises(AttributeError, getattr, parser, 'format_version') def test_version_format(self): - parser = ErrorRaisingArgumentParser(prog='PPP', version='%(prog)s 3.5') + parser = ErrorRaisingArgumentParser(prog='PPP') + parser.add_argument('-v', '--version', action='version', version='%(prog)s 3.5') msg = self._get_error(parser.parse_args, ['-v']).stderr self.assertEqual('PPP 3.5\n', msg) def test_version_no_help(self): - parser = ErrorRaisingArgumentParser(add_help=False, version='1.0') + parser = ErrorRaisingArgumentParser(add_help=False) + parser.add_argument('-v', '--version', action='version', version='1.0') self.assertArgumentParserError(parser, '-h') self.assertArgumentParserError(parser, '--help') - self.assertPrintVersionExit(parser, '-v') - self.assertPrintVersionExit(parser, '--version') + self.assertRaises(AttributeError, getattr, parser, 'format_version') def test_version_action(self): parser = ErrorRaisingArgumentParser(prog='XXX') @@ -4325,12 +4324,13 @@ class TestOptionalsHelpVersionActions(TestCase): parser.add_argument('-x', action='help') parser.add_argument('-y', action='version') self.assertPrintHelpExit(parser, '-x') - self.assertPrintVersionExit(parser, '-y') self.assertArgumentParserError(parser, '-v') self.assertArgumentParserError(parser, '--version') + self.assertRaises(AttributeError, getattr, parser, 'format_version') def test_help_version_extra_arguments(self): - parser = ErrorRaisingArgumentParser(version='1.0') + parser = ErrorRaisingArgumentParser() + parser.add_argument('--version', action='version', version='1.0') parser.add_argument('-x', action='store_true') parser.add_argument('y') @@ -4342,8 +4342,7 @@ class TestOptionalsHelpVersionActions(TestCase): format = '%s %%s %s' % (prefix, suffix) self.assertPrintHelpExit(parser, format % '-h') self.assertPrintHelpExit(parser, format % '--help') - self.assertPrintVersionExit(parser, format % '-v') - self.assertPrintVersionExit(parser, format % '--version') + self.assertRaises(AttributeError, getattr, parser, 'format_version') # ====================== @@ -4398,7 +4397,7 @@ class TestStrings(TestCase): parser = argparse.ArgumentParser(prog='PROG') string = ( "ArgumentParser(prog='PROG', usage=None, description=None, " - "version=None, formatter_class=%r, conflict_handler='error', " + "formatter_class=%r, conflict_handler='error', " "add_help=True)" % argparse.HelpFormatter) self.assertStringEqual(parser, string) @@ -4760,13 +4759,7 @@ class TestImportStar(TestCase): self.assertEqual(sorted(items), sorted(argparse.__all__)) def test_main(): - # silence warnings about version argument - these are expected - with support.check_warnings( - ('The "version" argument to ArgumentParser is deprecated.', - DeprecationWarning), - ('The (format|print)_version method is deprecated', - DeprecationWarning)): - support.run_unittest(__name__) + support.run_unittest(__name__) # Remove global references to avoid looking like we have refleaks. RFile.seen = {} WFile.seen = set() @@ -23,6 +23,8 @@ Core and Builtins Library ------- +- Issue #13248: Remove obsolete argument "version" of argparse.ArgumentParser. + - Issue #14814: implement more consistent ordering and sorting behaviour for ipaddress objects |