mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
[3.13] gh-116850: Fix argparse for namespaces with not directly writable dict (GH-124667) (GH-124757)
It now always uses setattr() instead of setting the dict item to modify
the namespace. This allows to use a class as a namespace.
(cherry picked from commit 95e92ef6c7
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
597b6211ab
commit
11d4b54b88
3 changed files with 16 additions and 1 deletions
|
@ -2365,6 +2365,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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue