summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_argparse.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-09-29 08:01:10 (GMT)
committerGitHub <noreply@github.com>2024-09-29 08:01:10 (GMT)
commit95e92ef6c74e973ea13d15180190d0fa2af82fbf (patch)
tree4cc8698de8eb1fc631cbb3d567a6b52ca6d4c37f /Lib/test/test_argparse.py
parentf1a2417b9e2993e584610851ac004c8b0599b323 (diff)
downloadcpython-95e92ef6c74e973ea13d15180190d0fa2af82fbf.zip
cpython-95e92ef6c74e973ea13d15180190d0fa2af82fbf.tar.gz
cpython-95e92ef6c74e973ea13d15180190d0fa2af82fbf.tar.bz2
gh-116850: Fix argparse for namespaces with not directly writable dict (GH-124667)
It now always uses setattr() instead of setting the dict item to modify the namespace. This allows to use a class as a namespace.
Diffstat (limited to 'Lib/test/test_argparse.py')
-rw-r--r--Lib/test/test_argparse.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 91f04fa..1580593 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2344,6 +2344,18 @@ class TestAddSubparsers(TestCase):
(NS(foo=False, bar=0.5, w=7, x='b'), ['-W', '-X', 'Y', 'Z']),
)
+ def test_parse_known_args_to_class_namespace(self):
+ class C:
+ pass
+ self.assertEqual(
+ self.parser.parse_known_args('0.5 1 b -w 7 -p'.split(), namespace=C),
+ (C, ['-p']),
+ )
+ self.assertIs(C.foo, False)
+ self.assertEqual(C.bar, 0.5)
+ self.assertEqual(C.w, 7)
+ self.assertEqual(C.x, 'b')
+
def test_parse_known_args_with_single_dash_option(self):
parser = ErrorRaisingArgumentParser()
parser.add_argument('-k', '--known', action='count', default=0)