summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2014-10-18 00:07:08 (GMT)
committerR David Murray <rdmurray@bitdance.com>2014-10-18 00:07:08 (GMT)
commitaf26c15110b76195e62a06d17e39176d42c0511c (patch)
treed585a5eafde4ac6b2f0ffc1394547633adf1746e
parent10229a450f82b5582abe3116bbc8cb73daba3e2f (diff)
downloadcpython-af26c15110b76195e62a06d17e39176d42c0511c.zip
cpython-af26c15110b76195e62a06d17e39176d42c0511c.tar.gz
cpython-af26c15110b76195e62a06d17e39176d42c0511c.tar.bz2
#9351: set_defaults on subparser is no longer ignored if set on parent.
Before, if a default was set on the parent parser, any default for that variable set via set_defaults on a subparser would be ignored. Now the subparser set_defaults is honored. Patch by Jyrki Pullianinen.
-rw-r--r--Lib/argparse.py9
-rw-r--r--Lib/test/test_argparse.py7
-rw-r--r--Misc/NEWS3
3 files changed, 18 insertions, 1 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index b5bb19a..1b233b8 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1089,7 +1089,14 @@ class _SubParsersAction(Action):
# parse all the remaining options into the namespace
# store any unrecognized options on the object, so that the top
# level parser can decide what to do with them
- namespace, arg_strings = parser.parse_known_args(arg_strings, namespace)
+
+ # In case this subparser defines new defaults, we parse them
+ # in a new namespace object and then update the original
+ # namespace for the relevant parts.
+ subnamespace, arg_strings = parser.parse_known_args(arg_strings, None)
+ for key, value in vars(subnamespace).items():
+ setattr(namespace, key, value)
+
if arg_strings:
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 0df66ad..b74b622 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2726,6 +2726,13 @@ class TestSetDefaults(TestCase):
parser = ErrorRaisingArgumentParser(parents=[parent])
self.assertEqual(NS(x='foo'), parser.parse_args([]))
+ def test_set_defaults_on_parent_and_subparser(self):
+ parser = argparse.ArgumentParser()
+ xparser = parser.add_subparsers().add_parser('X')
+ parser.set_defaults(foo=1)
+ xparser.set_defaults(foo=2)
+ self.assertEqual(NS(foo=2), parser.parse_args(['X']))
+
def test_set_defaults_same_as_add_argument(self):
parser = ErrorRaisingArgumentParser()
parser.set_defaults(w='W', x='X', y='Y', z='Z')
diff --git a/Misc/NEWS b/Misc/NEWS
index 0b7330e..1455d2c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@ Core and Builtins
Library
-------
+- Issue #9351: Defaults set with set_defaults on an argparse subparser
+ are no longer ignored when also set on the parent parser.
+
- Issue #20421: Add a .version() method to SSL sockets exposing the actual
protocol version in use.